-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameCode.cpp
782 lines (744 loc) · 14.7 KB
/
GameCode.cpp
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#include<iostream>
#include<fstream>
using namespace std;
void read(char file[], char rar[][15]);
void display(char arr[][15], int s, int tri);
void pause_s(char grid[][15], int score, int tries, char hint, int loop);
void resume();
bool diagnol_c(char grid[][15], char key[], int chk); // Checking to match the word in diaonal direction
bool diagnol_r(char grid[][15], char search[], int chk); //Checking to match the word in diaonal reverse direction
bool diagnol_l(char grid[][15], char search[], int chk);
bool diagnol_rh(char grid[][15], char search[], int chk);
bool diagnol_lh(char grid[][15], char search[], int chk);
void high_check();
void s_high(int score);
bool check_r(char grid[][15], char key[], int chk);
bool check_c(char grid[][15], char key[], int chk);
void reverse(char rev[], int chk);
void easy(int score, int lives, int loop);
void medium(int score, int lives, int loop);
void hard(int score, int lives, int loop);
//all the prototypes which are used
void read(char file[], char rar[][15]) //First step to read the file
{
ifstream fin;
fin.open(file);
for (int row = 0; row< 15; row++)
{
for (int col = 0; col < 15; col++)
{
fin >> rar[row][col];
}
}
}
void display(char arr[][15], int s, int tri) //Displaying the readed file
{
system("color 06");
cout << "\t\t\t WORD SEARCH GAME " << endl;
cout << "\t\t\t------------------------------------------------------------" << endl;
for (int row = 0; row< 15; row++)
{
cout << "\t\t\t|";
for (int col = 0; col < 15; col++)
{
cout << arr[row][col] << " | ";
}
cout << endl;
}
cout << "\t\t\t------------------------------------------------------------" << endl;
cout << "SCORE :" << s << "\t\t\t\t\t\t\t\t\t TRIES :" << tri << endl;
cout << "\t\t\t\t\tBy M.HUSNAIN NAEEM" << endl<<endl;
}
void pause_s(char grid[][15], int score, int tries, char hint, int loop)
{
int x, y;
ofstream fout;
ifstream fin;
fin.open("Pause.txt");
fout.open("Pause.txt");
for (x = 0; x < 15; x++)
{
for (y = 0; y < 15; y++)
{
fout << grid[x][y];
}
cout << endl;
}
fout << score << " " << tries << " " << hint << " " << loop;
fin.close();
fout.close();
}
void resume()
{
int x, y, score, tries, loop;
char hint;
char grid[15][15];
ifstream fin;
char ar[] = { "Pause.txt" };
fin.open(ar);
for (x = 0; x < 15; x++)
{
for (y = 0; y < 15; y++)
{
fin >> grid[x][y];
}
}
fin >> score;
fin >> tries;
fin >> hint;
fin >> loop;
if (hint == 'e')
{
easy(score, tries, loop);
}
if (hint == 'm')
{
medium(score, tries, loop);
}
if (hint == 'h')
{
hard(score, tries, loop);
}
}
bool diagnol_c(char grid[][15], char key[], int chk)
{
int x, y, c;
int match = 1;
bool kkk = false;
for (x = 0; x < 15; x++)
{
if (grid[x][x] == key[0])
{
c = 1;
for (y = x + 1; c != chk; y++)
{
if (grid[y][y] == key[c])
{
match++;
}
c++;
}
if (match == chk&&c == chk)
{
kkk = true;
match = 1;
}
else
match = 1;
}
}
return kkk;
}
void high_check()
{
int hi[5], x;
ifstream fin;
fin.open("Highscore.txt");
for (x = 0; x < 5; x++)
{
fin >> hi[x];
}
system("cls");
system("color 09");
cout << "\t\t\tHE HIGH SCORES ARE GIVEN BELOW" << endl << endl;
for (x = 0; x < 5; x++)
{
cout << "High score is" << hi[x] << endl;
}
fin.close();
}
void s_high(int score)
{
int h_arr[6], x, y, temp;
ifstream fin;
fin.open("Highscore.txt");
for (x = 0; x < 5; x++)
{
fin >> h_arr[x];
}
h_arr[5] = score;
for (x = 0; x < 6; x++)
{
for (y = x + 1; y < 6; y++)
{
if (h_arr[x] < h_arr[y])
{ //HERE WE ENTER ALL RECORD AND THE SORT INTO 5 THEN REMOVE THE LATS ONE
temp = h_arr[x];
h_arr[x] = h_arr[y];
h_arr[y] = temp;
}
}
}
ofstream fout;
fout.open("Highscore.txt");
for (x = 0; x < 5; x++)
{
fout << h_arr[x] << " ";
}
fout.close();
}
bool check_r(char grid[][15], char key[], int chk)
{
int x, y;
int match = 1;
bool kkk = false; //check end the program for the search found or not
int c; //control the loop for the word match
for (x = 0; x < 15; x++)
{
for (y = 0; y < 15; y++)
{
if (key[0] == grid[x][y]) //match first word
{
c = 1;
for (int k = y + 1; c != chk; k++) // mtch the whole wrd nxt
{
if (grid[x][k] == key[c])
{
match++;
}
c++;
}
if (match == chk&&c == chk)
{
kkk = true;
match = 1;
}
else
{
match = 1;
}
}
}
}
return kkk;
}
bool check_c(char grid[][15], char key[], int chk)//SEARCHING IN COLUMN
{
int col, row;
int match = 1;
bool kkk = false; //check end the program for the search found or not
int c; //control the loop for the word match
for (col = 0; col < 15; col++)
{
for (row = 0; row < 15; row++)
{
if (key[0] == grid[row][col]) //match first word
{
c = 1;
for (int k = row + 1; c != chk; k++) // mtch the whole wrd nxt
{
if (grid[k][col] == key[c])
{
match++;
}
c++;
}
if (match == chk&&c == chk)
{
kkk = true;
match = 1;
}
else
match = 1;
}
}
} // SHIFTING ROW AND COLUMNS
return kkk;
}
bool diagnol_r(char grid[][15], char search[], int chk)//clear
{
int j, k, m1, m2, m3, x, y = 0, match = 1, c;
bool kkk = false;
for (x = 1; x < 15; x++)
{
for (j = y, k = x; k < 15; j++, k++)
{
// This funtion chk the the griD in right half diagnols
if (grid[j][k] == search[0])
{
for (m1 = j + 1; m1<15; m1++)
{
c = 1;
for (m2 = m1, m3 = k + 1; c != chk; m2++, m3++)
{
if (search[c] == grid[m2][m3])
{
match++;
}
c++;
}
}
if (match == chk&&c == chk)
{
kkk = true;
match = 1;
}
else
match = 1;
}
}
}
return kkk;
}
bool diagnol_l(char grid[][15], char search[], int chk)
{ // THIS FUNCTION MATCHES THE WORD FROM THE LEFT HALF DIAGOLS
int x, y, j, k, m1, m2, c = 0, match = 1, m3;
y = 0;
bool kkk = false;
for (x = 1; x < 15; x++)
{
for (j = x, k = y; j < 15; j++, k++)
{
if (grid[j][k] == search[0])
{
cout << k;
for (m1 = j + 1; m1<15; m1++)
{
c = 1;
for (m2 = m1, m3 = k + 1; c != chk; m2++, m3++)
{
if (search[c] == grid[m2][m3])
{
match++;
}
c++;
}
}
if (match == chk&&c == chk)
{
kkk = true;
match = 1; //else bhi ad kr dna bad ma ---fazool admi
}
else
match = 1;
}
}
}
return kkk;
}
bool diagnol_lh(char grid[][15], char search[], int chk)
{
int x, y = 0, j, k, m1, m2, c = 0, match = 1;
bool kkk = false;
for (x = 13; x >= 0; x--)
{
for (j = x, k = 0; j >= k; j--, k++)
{
if (grid[k][j] == search[0])
{
c = 1;
for (m1 = x - 1, m2 = k + 1; c != chk; m1--, m2++)
{
if (grid[m2][m1] == search[c])
{
match++;
}
c++;
}
}
}
if (chk == match)
{
kkk = true;
match = 1;
}
else
match = 1;
}
return kkk;
}
bool diagnol_rh(char grid[][15], char search[], int chk)
{
int x, y = 0, j, k, m1, m2, c = 0, match = 1;
bool kkk = false;
for (x = 0; x < 15; x++)
{
for (j = 14, k = x; j >= x; j--, k++)
{
if (grid[k][j] == search[0])
{
c = 1;
for (m1 = j - 1, m2 = x + 1; c != chk; m1--, m2++)
{
if (grid[m2][m1] == search[c])
{
match++;
}
c++;
}
}
}
if (chk == match)
{
kkk = true;
match = 1;
}
else
match = 1;
}
return kkk;
}
void reverse(char rev[], int chk)
{
int x, y;
char arr[10];
for (x = 0; x < chk; x++)
{
arr[x] = rev[x];
}
for (x = 0, y = chk - 1; x<chk, y >= 0; y--, x++)
{
rev[x] = arr[y];
}
cout << rev;
}
int getl(char w_s[], int chk = 0)
{
while (w_s[chk] != '\0')
{
chk++;
}
return chk;
}
bool hrev(char grid[][15], char w_s[], int chk)
{
bool correct = false;
reverse(w_s, chk);
correct = check_c(grid, w_s, chk);
if (correct == false)
{
correct = check_r(grid, w_s, chk);
}
if (correct == false) //all diagnols entries
{
if (correct == false)
{
correct = diagnol_l(grid, w_s, chk);
}
if (correct == false)
{
correct = diagnol_r(grid, w_s, chk);
}
}
if (correct == false) // simple diagnol
{
correct = diagnol_c(grid, w_s, chk);
}
if (correct == false) //In this codition we chk the anti-diagnol entries
{
if (correct == false)
{
correct = diagnol_lh(grid, w_s, chk);
}
if (correct == false)
{
correct = diagnol_rh(grid, w_s, chk);
}
}
return correct;
}
bool ver_w(char word[], int size)
{
ifstream fin;
char check_word[15];
bool kkk = false;
fin.open("dictionary.txt", ios::in);
while (!fin.eof())
{
fin >> check_word;
int size2 = strlen(check_word);
if ((size == 1))
continue;
if (word[0] == check_word[0] && size == size2)
{
for (int i = 0; word[i] != '\0'; i++)
{
if (word[i] != check_word[i])
{
break;
}
}
kkk = true;
break;
}
}
fin.close();
return kkk;
}
void easy(int score, int lives, int loop)
{
bool correct = false,dic=false;
int chk;
char file[10] = { "Board.txt" };
char grid[15][15], w_s[15], achk = NULL;
read(file, grid);
while (lives != 0 && loop >= 0)//Words you want to search
{
system("cls"); //
display(grid, score, lives);
cout << "Enter the word to search :";
cin >> w_s;
if ((w_s[0] == 'p' || w_s[0] == 'P') && w_s[1] == '\0')
{
pause_s(grid, score, lives, 'e', loop);
cout << "Press E to Exit or Press R to Resume........ " << endl;
while (achk != 'E' && achk != 'e' && achk != 'r' && achk != 'R')
{
cin >> achk;
}
if (achk == 'r' || achk == 'R')
{
continue;
}
if (achk == 'E' || achk == 'e')
{
break;
}
}
chk = 0;
chk = getl(w_s, chk);
dic = ver_w(w_s, chk);
if (dic == true)
{
dic = false;
correct = check_c(grid, w_s, chk);
if (correct == false)
{
correct = check_r(grid, w_s, chk);
}
}
if (correct == true)
{
score = score + 10;
loop--;
correct = false;
}
else
{
lives--;
}
}
s_high(score);
system("cls"); //
display(grid, score, lives);
system("pause");
}
void medium(int score, int lives, int loop)
{
bool correct = false,dic=false;
int chk;
char file[10] = { "Board.txt" };
char grid[15][15], w_s[15], achk = NULL;
read(file, grid);
while (lives != 0 && loop >= 0)//Words you want to search
{
system("cls"); //
display(grid, score, lives);
cout << "Enter the word to search :";
cin >> w_s;
if (w_s[0] == 'p' || w_s[0] == 'P')
{
pause_s(grid, score, lives, 'm', loop);
cout << "Press E to Exit or Press R to Resume........ " << endl;
achk = NULL;
while (achk != 'E' && achk != 'e' && achk != 'r' && achk != 'R')
{
cin >> achk;
}
if (achk == 'r' || achk == 'R')
{
continue;
}
if (achk == 'E' || achk == 'e')
{
break;
}
}
chk = 0;
chk = getl(w_s, chk);
dic = ver_w(w_s, chk);
if (dic == true)
{
correct = check_c(grid, w_s, chk);
if (correct == false)
{
correct = check_r(grid, w_s, chk);
}
if (correct == false)
{
reverse(w_s, chk);
correct = check_c(grid, w_s, chk);
if (correct == false)
{
correct = check_r(grid, w_s, chk);
}
}
}
if (correct == true)
{
score = score + 10;
loop--;
correct = false;
}
else
{
lives--;
}
}
s_high(score);
system("cls"); //
display(grid, score, lives);
system("pause");
}
void hard(int score, int lives, int loop)
{
bool correct = false,dic=false;
int chk;
char file[10] = { "Board.txt" };
char grid[15][15], w_s[15], achk = NULL;
read(file, grid);
while (lives != 0 && loop >= 0)//Words you want to search
{
system("cls"); //
display(grid, score, lives);
cout << "Enter the word to search :";
cin >> w_s;
if ((w_s[0] == 'p' || w_s[0] == 'P') && w_s[1] == '\0')
{
pause_s(grid, score, lives, 'h', loop);
cout << "Press E to Exit or Press R to Resume........ " << endl;
achk = NULL;
while (achk != 'E' && achk != 'e' && achk != 'r' && achk != 'R')
{
cin >> achk;
}
if (achk == 'r' || achk == 'R')
{
continue;
}
if (achk == 'E' || achk == 'e')
{
break;
}
}
chk = 0;
chk = getl(w_s, chk);
dic = ver_w(w_s, chk);
if (dic == true)
{
correct = check_c(grid, w_s, chk);
if (correct == false)
{
correct = check_r(grid, w_s, chk);
}
if (correct == false) //all diagnols entries
{
if (correct == false)
{
correct = diagnol_l(grid, w_s, chk);
}
if (correct == false)
{
correct = diagnol_r(grid, w_s, chk);
}
}
if (correct == false) // simple diagnol
{
correct = diagnol_c(grid, w_s, chk);
}
if (correct == false) //In this codition we chk the anti-diagnol entries
{
if (correct == false)
{
correct = diagnol_lh(grid, w_s, chk);
}
if (correct == false)
{
correct = diagnol_rh(grid, w_s, chk);
}
}
if (correct == false) //This condition chk all the diagnol and other values in reverse order
{
correct = hrev(grid, w_s, chk);
}
}
////////////
if (correct == true)
{
score = score + 10;
loop--;
correct = false;
}
else
{
lives--;
}
}
s_high(score);
system("cls"); //
display(grid, score, lives);
system("pause");
}
int main()
{
char choice;
bool kkk = false;
int chk = 0; //chk is for the length of the word
ifstream fin;
cout << "\t\t\t WORD SEARCH GAME " << endl << endl;
cout << "MENUE : \n";
cout << "Press N for new game \nPress R for resume game \nPress L for level selection \nPress H for High score check up \nPress E for exit " << endl;
cin >> choice;
if (choice == 'r' || choice == 'R')
{
resume();
}
if (choice == 'e' || choice == 'E')
{
}
if (choice == 'l' || choice == 'L')
{
int lvl;
cout << "Press 1 for Easy Mode" << endl;
cout << "Press 2 for Medium Mode" << endl;
cout << "Press 3 for Hard Mode" << endl;
cin >> lvl;
if (lvl == 1)
{
easy(0, 3, 5);
}
if (lvl == 2)
{
medium(0, 3, 7);
}
if (lvl == 3)
{
hard(0, 3, 15);
}
}
if (choice == 'h' || choice == 'H')
{
high_check();
}
if (choice == 'N' || choice == 'n')
{
int lvl1;
cout << "PLEASE SELECT THE LEVEL OF THE GAME " << endl << endl;
cout << "Press 1 for Easy Mode" << endl;
cout << "Press 2 for Medium Mode" << endl;
cout << "Press 3 for Hard Mode" << endl;
cin >> lvl1;
if (lvl1 == 1)
{
easy(0, 3, 5);
}
if (lvl1 == 2)
{
medium(0, 3, 7);
}
if (lvl1 == 3)
{
hard(0, 3, 15);
}
}
}