-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMINESWEEPER.c
835 lines (660 loc) · 14 KB
/
MINESWEEPER.c
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*
PROJECT NAME: MINESWEEPER GAME
DONE BY: VIDISH JOSHI(AU1841019)
*/
/*Please read instructions in 'How to play?' section...
Please read the input syntax to play the game...
A player can use the default username and password which are:
USERNAME: admin
PASSWORD: admin
One can sign in using above values.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include <windows.h>
void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD Cursor_Pos={x,y};
hConsoleOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos);
}
void hidecursor()
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
struct USER_DETAILS
{
char USERNAME[30];
char PASSWORD[30];
} user;
char USERNAME[30];
int SCORE;
int menuoptions()
{
decor_line1();
gotoxy(100,8);
int a;
printf("\n\n\n\t\t\t\t\t\t>>>>>>>>>LOG-IN OPTIONS<<<<<<<<<");
gotoxy(100,11);
printf("\n\n\t\t\t\t\t\t1)SIGN-IN\n");
gotoxy(100,13);
printf("\n\n\t\t\t\t\t\t2)SIGN-UP");
gotoxy(100,15);
printf("\n\n\t\t\t\t\t\tSelect an option...:");
scanf("%d", &a);
return a;
}
int login(char x[])
{
system("color 70");
int e=menuoptions(),flag=0;
char USERNAME1[40],PASSWORD1[40];
char ch='n';
FILE *fp;
if(e==1)
{
do
{
ch='n';
fp=fopen("USER_DETAILS.txt","a+");
system("cls");
decor_line1();
gotoxy(57, 10);
fflush(stdin);
printf("USERNAME: ");
gets(USERNAME1);
gotoxy(57,12);
fflush(stdin);
printf("PASSWORD: ");
gets(PASSWORD1);
strcpy(x,USERNAME1);
while(fread(&user,sizeof(struct USER_DETAILS),1,fp))
{
if(strcmp(user.USERNAME,USERNAME1)==0)
{
if(strcmp(user.PASSWORD,PASSWORD1)==0)
{
gotoxy(57,17);
printf("\n\t\t\t\tAccess Granted");
flag=1;
break;
}
}
}
if(flag==0)
{
gotoxy(57,17);
printf("\n\t\t\t\tAccess denied");
gotoxy(57,18);
printf("\n\t\t\t\tDo you want to retry(y/n)???");
ch=getch();
}
}while(ch=='y');
}
fclose(fp);
if(e==2)
{
system("cls");
FILE *fp1;
fp1=fopen("USER_DETAILS.txt","a+");
decor_line1();
gotoxy(57,10);
fflush(stdin);
printf("USERNAME: ");
gets(user.USERNAME);
gotoxy(57,12);
fflush(stdin);
printf("Password:");
gets(user.PASSWORD);
fwrite(&user,sizeof(struct USER_DETAILS),1,fp1);
gotoxy(54,17);
printf("congratulations your ID has been created");
Sleep(500);
flag=1;
fclose(fp1);
strcpy(x,user.USERNAME);
}
if(flag==1)
{
return 10;
}
else
{
return 0;
}
}
int val_gen(int y, int g, int f)
{
int m;
y=0;
y=rand()%g;
if(y<f)
{
y+=f;
}
return(y);
}
void decor_line1()
{
int i;
gotoxy(24, 4);
for( i=0;i<=80;i++)
{
printf("%c", 178);
}
gotoxy(28, 5);
for(i=0;i<=72;i++)
{
printf("%c", 177);
}
int y=5;
for(i=0;i<=17;i++)
{
gotoxy(24, y);
printf("%c", 178);
y++;
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(25, y);
printf("%c", 178);
y++;
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(26, y);
printf("%c", 177);
y++;
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(27, y);
printf("%c", 177);
y++;
}
gotoxy(24, 23);
for( i=0;i<=80;i++)
{
printf("%c", 178);
}
gotoxy(28, 22);
for(i=0;i<=72;i++)
{
printf("%c", 177);
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(101, y);
printf("%c", 177);
y++;
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(102, y);
printf("%c", 177);
y++;
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(103, y);
printf("%c", 178);
y++;
}
y=5;
for(i=0;i<=17;i++)
{
gotoxy(104, y);
printf("%c", 178);
y++;
}
}
void decor_line2()
{
int i;
gotoxy(10,1);
for(i=0;i<=90;i++)
{
printf("%c", 178);
}
int y=2;
for(i=0;i<=23;i++)
{
gotoxy(10,y);
printf("%c", 178);
y++;
}
y=2;
for(i=0;i<=23;i++)
{
gotoxy(11,y);
printf("%c", 178);
y++;
}
gotoxy(10,26);
for(i=0;i<=90;i++)
{
printf("%c", 178);
}
y=2;
for(i=0;i<=23;i++)
{
gotoxy(100,y);
printf("%c", 178);
y++;
}
y=2;
for(i=0;i<=23;i++)
{
gotoxy(99,y);
printf("%c", 178);
y++;
}
}
int main()
{
srand(time(NULL)); //SEEDING WITH CURRENT TIME
int o;
char x[100];
o=login(x);
printf("%s", x);
if(o!=10)
{
system("cls");
printf("\n\n\n\t\t\t\tYOU HAVE NOT LOGGED-IN SUCCESSFULLY...\n\t\t\t\tYOU CAN'T PLAY");
exit (1);
}
system("cls");
line:
decor_line1();
hidecursor();
gotoxy(28,6);
printf("Player:%s", x);
gotoxy (57,8);
system("color 47");
printf("MINESWEEPER");
Sleep(300);
gotoxy(50, 10);
system("color 57");
printf("1) PLAY");
Sleep(300);
gotoxy(50, 11);
system("color 47");
printf("2) LEVEL");
Sleep(300);
gotoxy(50,12);
system("color 57");
printf("3) HOW TO PLAY?");
Sleep(300);
gotoxy(50,13);
system("color 47");
printf("4) LEADERBOARD");
Sleep(300);
gotoxy(50,14);
system("color 57");
printf("5) QUIT");
gotoxy(45,15);
printf("------------------------------------------\n");
gotoxy(45,16);
printf("------------------------------------------\n");
gotoxy(47, 17);
system("color 57");
printf("Select any option from above..:");
gotoxy(45,18);
printf("------------------------------------------\n");
gotoxy(45,19);
printf("------------------------------------------\n");
gotoxy(79,17);
Sleep(300);
system("color 70");
int a;
scanf("%d", &a);
int n;
if(a==1)
{
system("cls");
decor_line1();
gotoxy(28,6);
printf("Player:%s", x);
gotoxy(50, 5);
printf("\n\n\n\n\n\n\n\n\t\t\t\t\tDo you want to start playing in EASY level...");
printf("\n\t\t\t\t\t1)YES\t\t\t2)NO\n\n\n\t\t\t\t\t\tPlease select an option..");
int c;
scanf("%d", &c);
if(c==1)
{
decor_line1();
system("cls");
n=5;
goto line1;
}
else
{
decor_line1();
system("cls");
printf("Please enter the difficulty level...");
goto line2;
}
}
if(a==2)
{
system("cls");
goto line2;
}
if(a==3)
{
system("cls");
FILE *fp;
fp=fopen("rules.txt", "r"); //USE OF FILE HANDLING......(FILE PRINTING)
char s=0;
do
{
s=getc(fp);
printf("%c", s);
}
while(s!=EOF);
printf("\n\n\t\t\t\tPress 1 to go back to home page...:");
int b;
scanf("%d", &b);
if(b==1)
{
system("cls");
goto line;
}
}
if(a==4)
{
system("cls"); //USE OF FILE HANDLING(fILE PRINTING)
FILE *fp2;
fp2=fopen("USER_SCORE.txt", "r");
char s=0;
do
{
s=getc(fp2);
printf("%c", s);
}
while(s!=EOF);
printf("\n\n\t\t\t\tPress 1 to go back to home page...:");
int b;
scanf("%d", &b);
if(b==1)
{
system("cls");
goto line;
}
}
if(a==5)
{
gotoxy(0,50);
exit(0);
}
//CODE TO CHOOSE THE DIFFICULTY LEVEL OF THE GAME...
line2:
decor_line1();
gotoxy(28,6);
printf("Player:%s", x);
gotoxy(50, 5);
printf("\n\n\n\n\t\t\t\t===========================================================\n");
printf("\t\t\t\tPlease choose which difficulty level do you want to play..:\n");
printf("\t\t\t\t===========================================================");
printf("\n\n\n");
printf("\t\t\t\t\t\tEASY Level - Press 1\n\t\t\t\t\t\tINTERMEDIATE LEVEL - Press 2\n\t\t\t\t\t\tHARD LEVEL - Press 3\n\n\n\t\t\t\t\t\tEnter the number now - ");
int g=0;
scanf("%d", &g);
system("cls");
if(g==1)
{
line1:
g=0;
n=5;
}
if(g==2)
{
n=9;
}
if(g==3)
{
n=12;
}
//CODE TO PRINT THE BOARD ACCORDING TO THE SIZE DETERMINED BY THE ABOVE CODE
char board[n][n], i, j;
int board1[n][n], board2[n][n]; //DECLARING AN EXTRA BOARD TO STORE THE POSITIONS OF MINE
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
board[i][j]='*'; //ASSIGNING ALL THE POSITIONS OF THE ORIGINAL BOARD VALUE * TO PRINT THE BOARD
board1[i][j]=0; //ASSIGNING ALL THE POSITIONS OF THE EXTRA BOARD THE VALUE 0
board2[i][j]=0;
}
}
//PRINTING THE ORIGINAL BOARD
decor_line2();
gotoxy(10,4);
printf("\t\t\t");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %c ", board[i][j]);
}
printf(" ");
printf("\n\t\t\t\t");
}
gotoxy(12,2);
printf("Player:%s", x);
gotoxy(10, 3);
printf("\t\t\t");
for(i=0;i<n;i++)
{
printf(" %d ", i);
}
int u=4;
for(i=0;i<n;i++)
{
gotoxy(9, u);
printf("\t\t\t");
printf(" %d ", i);
u++;
}
//CODE TO DECIDE THE NUMBER OF MINES TO BE KEPT FOR A GIVEN TYPE OF LEVEL OF MATRIX
int m;
if(n==5)
{
m=7;
}
if(n==9)
{
m=21;
}
if(n==12)
{
m=60;
}
//CODE TO GENERATE A RANDOM PLACE FOR MINES TO KEEP
int k, upper=n-1, lower=0, minechecker=0;
while(minechecker<m)
{
int x, y;
l1:
x=val_gen(i, upper, lower);
y=val_gen(j, upper, lower);
if(x==0&&y==0)
{
goto l1;
}
if(board1[x][y]!=1)
{
board1[x][y]=1;
minechecker++;
}
}
//CODE TO MAKE USER ENTER BOX NAME AND CHECK FOR MINES AT THAT BOX
int p, q, r, number, flagcount=0, correctmines=0, score=0, scoredelete=0, flag=m;
FILE *fptr;
fptr=fopen("USER_SCORE.txt","a+");
repeatentry:
system("cls");
decor_line2();
gotoxy(88, 2);
printf("SCORE::%d", score);
gotoxy(88, 3);
printf("FLAGS::%d", flag);
gotoxy(84, 4);
printf("TOTAL MINES::%d", m);
gotoxy(10,4);
printf("\t\t\t");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(board[i][j]=='-')
{
printf(" %d ", board2[i][j]);
}
else
{
printf(" %c ", board[i][j]);
}
}
printf(" ");
printf("\n\t\t\t\t");
}
gotoxy(12,2);
printf("Player:%s", x);
gotoxy(10, 3);
printf("\t\t\t");
for(i=0;i<n;i++)
{
printf(" %d ", i);
}
u=4;
for(i=0;i<n;i++)
{
gotoxy(7, u);
printf("\t\t\t ");
printf(" %d ", i);
u++;
}
gotoxy(6, 18);
printf("\t\t\t\tDO YOU WANT TO EXPLORE A BOX?.....Press 1:");
printf("\n\n\t\t\t\tDO YOU WANT TO FLAG A MINE?.....Press 2:");
printf("\n\n\t\t\t\tENTER THE CO-ORDINATES HERE..:");
scanf("%d %d %d", &p, &q, &r);
if(r==1)
{
if(board1[p][q]==1)
{
system("cls");
decor_line1();
gotoxy(50,12);
printf("SCORE=%d", score);
gotoxy(30,17);
printf("OOPS...\n\t\t\t\t\tGAME-OVER\n\t\t\t\t\tYOU STEPPED ON A MINE....");
fprintf(fptr,"PLAYERNAME: %s ", x);
fprintf(fptr,"PLAYERSORE: %d \n", score);
fclose(fptr);
// if(getch()==32)
// main();
goto end;
}
else
{
number=0;
if(p>0&&board1[p-1][q]==1)
{
number++;
}
if(p>0&&q>0&&board1[p-1][q-1]==1)
{
number++;
}
if(q>0&&board1[p][q-1]==1)
{
number++;
}
if(p<(n-1)&&q>0&&board1[p+1][q-1]==1)
{
number++;
}
if(p<(n-1)&&board1[p+1][q]==1)
{
number++;
}
if(p<(n-1)&&q<(n-1)&&board1[p+1][q+1]==1)
{
number++;
}
if(q<(n-1)&&board1[p][q+1]==1)
{
number++;
}
if(p>0&&q<(n-1)&&board1[p-1][q+1]==1)
{
number++;
}
board2[p][q]=number;
board[p][q]='-';
score=score+5;
system("cls");
// printf("Please enter next number...");
}
}
else if(r==2)
{
flag=flag-1;
if(board1[p][q]==1)
{
correctmines++;
}
else
{
scoredelete++;
}
board[p][q]='M';
flagcount++;
score=score+5;
}
if(flagcount==m)
{
if(correctmines==m)
{
system("cls");
decor_line1();
gotoxy(17,15);
printf("Your Final SCORE is %d", score);
gotoxy(17, 17);
printf("\t\t\tYOU WIN!!!!....\n\n\t\t\t\t\tYOU HAVE LOCATED ALL THE MINES");
goto end;
}
else if(correctmines!=m)
{
system("cls");
decor_line1();
gotoxy(40,12);
printf("Your Final Score is :: %d", score-(5*scoredelete));
gotoxy(27, 17);
printf("\t\t\t\tOOOH :(.....\n\t\t\t\t\tYOU LOSE..\n\t\t\t\t\tYOU DID NOT LOCATE ALL MINES AND HAVE EXHAUSTED ALL FLAGS.");
if(getch()==32)
main();
goto end;
}
}
goto repeatentry;
end:
gotoxy(0,30);
return 0;
}