-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.cpp
212 lines (186 loc) · 8 KB
/
minesweeper.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
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define UNCLICKEDCHAR 35
#define BOMBCHAR 153
#define FLAGCHAR 157
#define CLICKEDBUTZERO 48
#define BOMB 9
#define FLAG 8
#define CORRECT 251
#define WRONG 88
using namespace std;
void SetColor(int value);
void clickPos(int bombgrid[][20],int grid[][20],int N,int x,int y,bool &flaggameover);//main part of the program
int searchValidNeighbours(int bombgrid[][20],int grid[][20],int N,int x,int y); //searches valid neighbors and returns number of bombs
void placeBombs(int bombgrid[][20],int Nbomb,int N); //placing Nbomb bombs on the bombgrid
int GetRandomNumber(int n);
void printGrid(int grid[][20],int N); //printing the minesweeper grid
bool WinCondition(int bombgrid[][20],int grid[][20],int N); //checking if all unbombed location have been clicked
bool displayBombs(int bombgrid[][20],int grid[][20],int N);
int main()
{
char unclicked=UNCLICKEDCHAR,bomb=BOMBCHAR,clickzero=CLICKEDBUTZERO,flag=FLAGCHAR,correct=CORRECT,wrong=WRONG;
int bombgrid[20][20];
bool flaggameover=false;
int grid[20][20]; //grid is the grid seen by the user
int N,Nbomb,currentstatus,numberofturns=0;
int i,j,k,x,y,xflag,yflag;
cout<<"Welcome to MineSweeper! "<<endl<<endl;
cout<<"Instructions:\nFill in the coordinates in 'xcoordinate *space* ycordinate' \n(without the quote marks)\nUse negative coordinates '-x -y' to flag location(x y).\nUse the same to unflag a flagged location.\n\nSymbols:"<<endl;
SetColor(2);cout<<unclicked<<"-->Unclicked part of grid"<<endl;
SetColor(12);cout<<bomb<<"-->Bombs!"<<endl;
SetColor(6);cout<<clickzero<<"-->Indicates no bombs in surroundings. Same as blank pressed tile."<<endl;
SetColor(12);cout<<flag<<"-->Flags!"<<endl;
SetColor(10);cout<<correct<<"-->Correctly flagged bomb"<<endl;
SetColor(12);cout<<wrong<<"-->Incorrectly flagged tile"<<endl;SetColor(7);
cout<<"\nEnter N for NxN grid(max 20):";cin>>N;
if(N>20){cout<<"Invalid N;";return -1;}
cout<<"No. of bombs?:";cin>>Nbomb;
if(Nbomb>(N*N)){cout<<"Invalid number of bombs!"; return -1;} //if bombs> total spaces
for(i=0;i<N;i++)
for(j=0;j<N;j++) //intializing all elements in bombgrid to 0
bombgrid[i][j]=0; //0 represents no bomb, on the grid, 1 represents a bomb
for(i=0;i<N;i++)
for(j=0;j<N;j++) //intializing all elements in grid to -1 (meaning unclicked)
grid[i][j]=-1;
placeBombs(bombgrid,Nbomb,N);
printGrid(grid,N);
while(!WinCondition(bombgrid,grid,N) && !flaggameover) //main loop which keeps iterating till user clicks on bomb, or wins
{
cout<<"\nEnter coordinates to click (x,y)(x is horizontal):";cin>>y>>x;
if(x>(N-1) || y>(N-1))
{
cout<<"\nInvalid Coordinates";
continue;
}
if((x<0 && (y==0||y<0))||((x==0||x<0) && y<0))
{
if(grid[-x][-y]==FLAG){grid[-x][-y]=-1;printGrid(grid,N);}
else{grid[-x][-y]=FLAG;
printGrid(grid,N);} //changing value of that particular location to FLAG
}
else
{
clickPos(bombgrid,grid,N,x,y,flaggameover);
numberofturns++;
printGrid(grid,N);
}
}
if(WinCondition(bombgrid,grid,N))
{
cout<<"\n\nYOU WIN!!\n\nYou did it in "<<numberofturns<<" turns!"<<endl; //the loop ended in two cases, have to check
}
system("PAUSE");
cout<<endl; //if he lost or won
return 0;
}
bool displayBombs(int bombgrid[][20],int grid[][20],int N)
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(bombgrid[i][j]==1){if(grid[i][j]==FLAG){grid[i][j]=CORRECT;}else{grid[i][j]=BOMB;}}
}
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(grid[i][j]==FLAG){grid[i][j]=WRONG;}
}
return true;
}
void clickPos(int bombgrid[][20],int grid[][20],int N,int x,int y,bool &flaggameover)
{
int n,x1=x-1,y1=y-1,x2=x+1,y2=y+1;
n=searchValidNeighbours(bombgrid,grid,N,x,y);
if(grid[x][y]!=-1){return;}
else if(bombgrid[x][y]==1)
{
flaggameover=displayBombs(bombgrid,grid,N);
cout<<"\n\nYou clicked on a BOMB! GAME OVER!!\n";
return;
}
else
{
if(n!=0){grid[x][y]=n;}
else //0 bombs in surroundings
{
grid[x][y]=0;
if(y1>=0) {clickPos(bombgrid,grid,N,x,y1,flaggameover);} //checking if adjacent cells are valid or not and clicking them
if(y1>=0 && x2<N) {clickPos(bombgrid,grid,N,x2,y1,flaggameover);} //if no bombs are present in any of them
if(x2<N) {clickPos(bombgrid,grid,N,x2,y,flaggameover);}
if(y2<N && x2<N) {clickPos(bombgrid,grid,N,x2,y2,flaggameover);}
if(y2<N) {clickPos(bombgrid,grid,N,x,y2,flaggameover);}
if(y2<N && x1>=0) {clickPos(bombgrid,grid,N,x1,y2,flaggameover);}
if(x1>=0) {clickPos(bombgrid,grid,N,x1,y,flaggameover);}
if(x1>=0 && y1>=0) {clickPos(bombgrid,grid,N,x1,y1,flaggameover);}
}
return;
}
}
int searchValidNeighbours(int bombgrid[][20],int grid[][20],int N,int x,int y)
{
int countBomb=0,y1=y-1,y2=y+1,x1=x-1,x2=x+1;
if(y1>=0) {if(bombgrid[x][y1]==1){countBomb++;}}
if(y1>=0 && x2<N) {if(bombgrid[x2][y1]==1){countBomb++;}}
if(x2<N) {if(bombgrid[x2][y]==1){countBomb++;}}
if(y2<N && x2<N) {if(bombgrid[x2][y2]==1){countBomb++;}}
if(y2<N) {if(bombgrid[x][y2]==1){countBomb++;}}
if(y2<N && x1>=0) {if(bombgrid[x1][y2]==1){countBomb++;}}
if(x1>=0) {if(bombgrid[x1][y]==1){countBomb++;}}
if(x1>=0 && y1>=0) {if(bombgrid[x1][y1]==1){countBomb++;}}
return countBomb;
}
void printGrid(int grid[][20],int N)
{
char unclicked=UNCLICKEDCHAR,bomb=BOMBCHAR,clickzero=CLICKEDBUTZERO,flag=FLAGCHAR,correct=CORRECT,wrong=WRONG;
cout<<endl<<" ";
for(int i=0;i<N;i++){SetColor(9);cout<<i;SetColor(7);if(i<10){cout<<" ";}else{cout<<" ";}}
cout<<endl;
for(int i=0;i<N;i++)
{
SetColor(9);cout<<i;SetColor(7);if(i<10){cout<<" ";}else{cout<<" ";}
for(int j=0;j<N;j++)
{
if(grid[i][j]==-1){SetColor(2);cout<<unclicked<<" ";SetColor(7);}
else if (grid[i][j]==0){SetColor(6);cout<<clickzero<<" ";SetColor(7);}
else if (grid[i][j]==BOMB){SetColor(12);cout<<bomb<<" ";SetColor(7);}
else if (grid[i][j]==FLAG){SetColor(12);cout<<flag<<" ";SetColor(7);}
else if (grid[i][j]==CORRECT){SetColor(10);cout<<correct<<" ";SetColor(7);}
else if (grid[i][j]==WRONG){SetColor(12);cout<<wrong<<" ";SetColor(7);}
else {SetColor(15);cout<<grid[i][j]<<" ";SetColor(7);}
}
cout<<endl;
}
}
void SetColor(int value)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), value);
}
bool WinCondition(int bombgrid[][20],int grid[][20],int N)
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(bombgrid[i][j]==0)
{
if(grid[i][j]==-1){return false;}
}
return true;
}
void placeBombs(int bombgrid[][20],int Nbomb,int N)
{
int x,y;
srand(time(0));
for(int i=0;i<Nbomb;)
{
x=GetRandomNumber(N-1);
y=GetRandomNumber(N-1);
if(bombgrid[x][y]==1){continue;}
else{bombgrid[x][y]=1;i++;}
}
}
int GetRandomNumber(int n)
{
return (rand() % (n + 1));
}