-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingo.cpp
More file actions
543 lines (455 loc) · 10.5 KB
/
Bingo.cpp
File metadata and controls
543 lines (455 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<fstream>
#include<string>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
int *SharedData;
//Collection of functions
void showBingo();
void showInstruction();
void printBoard(int arr[5][5], int size);
void arrayInitalization(int arr[5][5], int size);
void display(int arr1[5][5], int arr2[5][5], int size);
void numberFinder(int arr[5][5], int size, int number);
bool checkWin(int arr[5][5], int size);
int countScore(int arr[5][5], int size);
int Totalscore = 0;
//Main Function
int main()
{
//system("color 0a");
bool retake = false;
const int size = 5;
char choice;
float input;
int score;
int gameID , gameID1;
int index, count = 1, turn, row, col, num1, prev;
int arr1[size][size], arr2[size][size];
int shmid = shmget((key_t)8749, sizeof(SharedData),IPC_CREAT | 0666);
if (shmid == -1) {
perror("Failed!");
return 1;
}
// Attach the shared memory segment
SharedData = (int*)shmat(shmid, NULL, 0);
if (SharedData == (int*)(-1)) {
perror("Failed!");
return 1;
}
cout << "\n\tProcess Requirements:\n";
cout << "\nProcess Requirements:\n";
cout << "Memory: 3000MB\n";
cout << "Hard Disk Space: 1500MB\n";
cout << "Number of Cores: 4"<< endl;
cout<<"Available Memory: "<<*SharedData<<endl;
if(*SharedData<3000)
{
cout << "\tInsufficient resources to create a new process." << endl;
do{
}while(*SharedData<3000);
}
cout<<"Memory Allocation Done to This Process!\n";
*SharedData-=3000;
srand(time(0));
//filehandling to store the name of the players
string Name,Name1;
ifstream File1;
ofstream File;
File.open("GameDetails.txt" , ios::app);
cout << "\n\n\n\n\n\n\n\t\t\tEnter The Name Of First Player : ";
getline(cin, Name);
File <<"Player 1 Name is " + Name<<endl;
cout << "\n\n\t\t\tEnter The Name Of Second Player : ";
getline(cin, Name1);
File << "Player 2 Name is " + Name1 << endl << endl;
File.close();
//filehandling to store the score of the winner
ifstream read;
ofstream write;
//filehandling to store the game id of the users
ifstream idRead;
ofstream idWrite;
gameID = rand();
gameID1 = rand();
//play game
while (true)
{
system("cls");
//display menu
showBingo();
showInstruction();
//for input from menu
cout << "Enter Your Choice : ";
cin >> choice;
//play game
if (choice == 's' || choice == 'S')
{
//intialize and display board of the game
display(arr1, arr2, size);
turn = rand() % 2;
if (turn == 0) //player 1 won the toss
{
cout << "PLAYER 1 WON THE TOSS!" << endl;
prev = 1;
}
else {
cout << "PLAYER 2 WON THE TOSS!" << endl;
prev = 0;
}
// filehandling for gameID of player 1
write.open("gameID.txt", ios::app);
write << "Game ID : " << gameID << endl<<endl;
write.close();
// filehandling for gameID
write.open("gameID.txt", ios::app);
write << "Game ID : " << gameID1 << endl;
write.close();
while (true)
{
if (prev == 1) //for player 1
{
//winning check
if (checkWin(arr2, size)) {
write.open("ScoreDetail.txt", ios::app);
write << Name << "'s Score is " << Totalscore << endl;
write.close();
cout << "PLAYER 1 WON THE GAME " << endl;
system("pause");
break;
}
else if (checkWin(arr1, size)) {
write.open("ScoreDetail.txt", ios::app);
write << Name1 << "'s Score is " << Totalscore << endl;
write.close();
cout << "PLAYER 2 WON THE GAME " << endl;
system("pause");
break;
}
while (true)
{
cout << "gameID : " << gameID << endl << endl;
cout << "PLAYER 1 TURN : ";
cin >> input;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (arr1[i][j] == input && arr1[i][j] != 0)
{
retake = false;
break;
}
else
{
retake = true;
}
}
if (retake == false)
{
break;
}
}
if (retake == false)
{
prev = 0;
break;
}
}
}
else // for player 2
{
//winning check
if (checkWin(arr1, size))
{
write.open("ScoreDetail.txt", ios::app);
write << Name << "'s Score is " << Totalscore << endl;
write.close();
cout << "PLAYER 1 WON THE GAME " << endl;
system("pause");
break;
}
else if (checkWin(arr2, size))
{
write.open("ScoreDetail.txt", ios::app);
write << Name1 << "'s Score is " << Totalscore << endl;
write.close();
cout << "PLAYER 2 WON THE GAME " << endl;
system("pause");
break;
}
while (true)
{
cout << "gameID : " << gameID1 << endl << endl;
cout << "PLAYER 2 TURN : ";
cin >> input;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (arr1[i][j] == input && arr1[i][j] != 0)
{
retake = false;
break;
}
else
{
retake = true;
}
}
if (retake == false)
{
break;
}
}
if (retake == false)
{
prev = 1;
break;
}
}
}
//finding number in array1
numberFinder(arr1, size, input);
//finding number in array2
numberFinder(arr2, size, input);
//board printed
system("cls");
if (prev == 0)
{
showBingo();
printBoard(arr1, size);
}
else
{
showBingo();
printBoard(arr2, size);
}
}
}
//for exit
else if (choice == 'E' || choice == 'e')
{
File << Totalscore << endl;
cout << "Thanks For Looking On My Game!\n" << endl;
return 0;
}
//for input value in menu
else
{
cout << "Invalid Input!" << endl;
system("pause");
}
}
*SharedData+=3000;
return 0;
}
void arrayInitalization(int arr[5][5], int size) {
int num;
bool flag = true, present = false;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
{
while (flag == true) {
num = (rand() % 25) + 1;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
{
if (arr[i][j] == num)
{
present = true;
break;
}
else {
present = false;
}
}
if (present == true) {
break;
}
}
if (present == false) {
arr[row][col] = num;
break;
}
}
}
}
}
//Show Bingo Design
void showBingo()
{
cout << "\t\t\t\t\t\t " << "+================+ " << endl;
cout << "\t\t\t\t\t\t " << "| BINGO GAME | " << endl;
cout << "\t\t\t\t\t\t " << "+================+ " << endl;
}
//Show Game Instructions
void showInstruction()
{
system("cls");
cout << "\n\t\t\t\t " << "<------------ MENU ------------->" << endl;
cout << "\n\t\t\t\t " << "Command Task " << endl;
cout << "\n\t\t\t\t " << "S Start Game " << endl;
cout << "\n\t\t\t\t " << "E Exit " << endl;
;
}
// Function for generating game board
void printBoard(int arr[5][5], int size)
{
Totalscore = countScore(arr, size);
cout << "\n\t\t\t\t\t " << " +===========================+ " << endl;
cout << "\t\t\t\t\t " << " SCORE : " << Totalscore << endl;
cout << "\t\t\t\t\t " << " +===========================+ " << endl;
cout << endl;
//generating game's board by using loops
for (int i = 0; i < 5; i++) // using loop for rows
{
cout << "\t\t\t\t\t+=======================================+\n\t\t\t\t";
for (int j = 0; j < 5; j++) // using loop for columns
{
if (arr[i][j] == 0) // conditions to generate board after user input
cout << "\t| " << 0 << " ";
else
cout << "\t| " << arr[i][j] << " ";
}
cout << "\t|" << endl;
}
cout << "\t\t\t\t\t+=======================================+\n\t\t\t\t" << endl;
}
// Number Finder in Board after user input
void numberFinder(int arr[5][5], int size, int number)
{
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (arr[i][j] == number && arr[i][j] != 0)
{
arr[i][j] = 0;
}
}
}
}
// check for winning condition
bool checkWin(int arr[5][5], int size)
{
bool flag = false;
int count = 0;
//column check
int col = 0;
for (int i = 0; i < size; ++i)
{
if (arr[col][i] == arr[col + 1][i] && arr[col + 1][i] == arr[col + 2][i]
&& arr[col + 2][i] == arr[col + 3][i] && arr[col + 3][i] == arr[col + 4][i] && arr[col][i] == 0)
{
count++;
}
}
//row check
for (int i = 0; i < size; ++i)
{
if (arr[i][col] == arr[i][col + 1] && arr[i][col + 1] == arr[i][col + 2]
&& arr[i][col + 2] == arr[i][col + 3] && arr[i][col + 3] == arr[i][col + 4] && arr[i][col] == 0)
{
count++;
}
}
// primary diagonal check
int diagonalCount = 0, antiDiagonal = 0;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (i == j && arr[i][j] == 0)
{
diagonalCount++;
}
if (i + j == size - 1 && arr[i][j] == 0)
{
antiDiagonal++;
}
}
}
if (antiDiagonal == 5)
{
count++;
}
if (diagonalCount == 5)
{
count++;
}
if (count >= 5)
{
return 1;
}
else
{
return 0;
}
}
//initialization and print the board
void display(int arr1[5][5], int arr2[5][5], int size)
{
//initializing array with random number
arrayInitalization(arr1, size);
arrayInitalization(arr2, size);
//board show
showBingo();
printBoard(arr1, size);
printBoard(arr2, size);
}
//Score Count
int countScore(int arr[5][5], int size)
{
int score = 1;
bool flag = false;
int count = 0;
//column check
int col = 0;
for (int i = 0; i < size; ++i)
{
if (arr[col][i] == arr[col + 1][i] && arr[col + 1][i] == arr[col + 2][i]
&& arr[col + 2][i] == arr[col + 3][i] && arr[col + 3][i] == arr[col + 4][i] && arr[col][i] == 0)
{
count++;
}
}
//row check
for (int i = 0; i < size; ++i)
{
if (arr[i][col] == arr[i][col + 1] && arr[i][col + 1] == arr[i][col + 2]
&& arr[i][col + 2] == arr[i][col + 3] && arr[i][col + 3] == arr[i][col + 4] && arr[i][col] == 0)
{
count++;
}
}
// primary diagonal check
int diagonalCount = 0, antiDiagonal = 0;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (i == j && arr[i][j] == 0)
{
diagonalCount++;
}
if (i + j == size - 1 && arr[i][j] == 0)
{
antiDiagonal++;
}
}
}
if (antiDiagonal == 5)
{
count++;
}
if (diagonalCount == 5)
{
count++;
}
return score * count * 10;
}