-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclass.cpp
More file actions
471 lines (419 loc) · 13 KB
/
Copy pathclass.cpp
File metadata and controls
471 lines (419 loc) · 13 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
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class Minefield
{
private:
int r,c; //r,c - number of rows,columns
int num_mines; //total number of mines
bool is_mine[128][128]; //true if mine is at (i,j), false otherwise
bool is_swept[128][128]; //true if swept, false otherwise
bool is_q_marked[128][128]; //true if q_marked, false otherwise
bool is_flagged[128][128]; //true if flagged, false otherwise
short number[128][128]; //stores total number of mines in the eight surrounding tiles
char state[128][128]; //stores info which can be revealed to player
bool loss; //becomes true when game is lost, false till then
int num_swept=0; //holds number of swept tiles
int num_flagged=0; //holds number of flags used
//legend
char flag_char = 'F';
char q_mark_char = '?';
char unswept_char = ' ';
char swept_char = 'o';
char mine_char = '*';
void sweep_util(int i, int j,int s) // a utility function which just accepts a zero location and flood fills all the zeros
{
if(i<0||i>=r||j<0||j>=c)
{
return;
}
if(number[i][j]!=0)
{
return;
}
number[i][j]=s; //changing the zeros to s (which helps us to identify the zeros which have opened) s is purely a placeholder
state[i][j] = swept_char; //changing the state array which is printed at the end
sweep_util(i-1,j,s);
sweep_util(i+1,j,s);
sweep_util(i,j-1,s);
sweep_util(i,j+1,s);
sweep_util(i-1,j-1,s);
sweep_util(i+1,j+1,s);
sweep_util(i+1,j-1,s);
sweep_util(i-1,j+1,s);
}
void set_field(int i_ex,int j_ex) //mutator
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
number[i][j] = 0;
is_mine[i][j] = false;
}
}
for (int i = 0; i < num_mines;)
{
int l = rand() % r;
int m = rand() % c;
if (is_mine[l][m] == true || (l == i_ex && m == j_ex))
continue;
else
{
i++;
is_mine[l][m] = true;
number[l][m] = -1;
if (l == 0 && m == 0) // checking top left corner
{
number[1][0] += 1;
number[1][1] += 1;
number[0][1] += 1;
}
else if (l == 0 && m == c - 1) // checking top right corner
{
number[0][c - 2] += 1;
number[1][c - 2] += 1;
number[1][c - 1] += 1;
}
else if (l == r - 1 && m == 0) // checking bottom left corner
{
number[r - 2][0] += 1;
number[r - 2][1] += 1;
number[r - 1][1] += 1;
}
else if (l == r - 1 && m == c - 1) // checking bottom right corner
{
number[r - 1][c - 2] += 1;
number[r - 2][c - 2] += 1;
number[r - 2][c - 1] += 1;
}
else if (l == 0) // checking upper boundary tiles
{
number[l][m - 1] += 1;
number[l][m + 1] += 1;
number[l + 1][m - 1] += 1;
number[l + 1][m] += 1;
number[l + 1][m + 1] += 1;
}
else if (m == 0) // checking left boundary tiles
{
number[l - 1][m] += 1;
number[l - 1][m + 1] += 1;
number[l][m + 1] += 1;
number[l + 1][m] += 1;
number[l + 1][m + 1] += 1;
}
else if (m == c - 1) // checking right boundary tiles
{
number[l - 1][m - 1] += 1;
number[l - 1][m] += 1;
number[l][m - 1] += 1;
number[l + 1][m - 1] += 1;
number[l + 1][m] += 1;
}
else if (l == r - 1) // checking lower boundary tiles
{
number[l - 1][m - 1] += 1;
number[l - 1][m] += 1;
number[l - 1][m + 1] += 1;
number[l][m - 1] += 1;
number[l][m + 1] += 1;
}
else
{
number[l - 1][m - 1] += 1;
number[l - 1][m] += 1;
number[l - 1][m + 1] += 1;
number[l][m - 1] += 1;
number[l][m + 1] += 1;
number[l + 1][m - 1] += 1;
number[l + 1][m] += 1;
number[l + 1][m + 1] += 1;
}
}
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (is_mine[i][j] == true)
{
number[i][j] = -1;
}
}
}
}
public:
Minefield(int rows, int cols, int num, int i_ex, int j_ex) //constructor
{
r=rows;
c=cols;
num_mines=num;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)state[i][j]=unswept_char;
}
set_field(i_ex,j_ex);
}
void flag(int i,int j) //mutator
{
if(!is_swept[i][j])
{
clear(i,j);
num_flagged+=1;
is_flagged[i][j]=true;
state[i][j]=flag_char;
}
}
void q_mark(int i,int j) //mutator
{
if(!is_swept[i][j])
{
clear(i,j);
is_q_marked[i][j]=true;
state[i][j]=q_mark_char;
}
}
void clear(int i,int j) //mutator
{
if(is_flagged[i][j])
{
is_flagged[i][j]=false;
num_flagged-=1;
state[i][j]=unswept_char;
}
if(is_q_marked[i][j])
{
is_q_marked[i][j]=false;
state[i][j]=unswept_char;
}
}
void sweep_from(int i, int j) // the main function which tackles all inputs of position (which are not visited) other that flag and question mark
//assuming that flag and question mark functions are called before hand only and the given position cannot have those two.
{
if(is_mine[i][j])
{
clear(i,j);
state[i][j]=mine_char;
loss=true;
num_swept++;
return;
}
//bomb tackled
// now handling numbers
int s = -9; // placeholder
if(number[i][j]!=0&&number[i][j]!=s)
{
if(state[i][j]==flag_char||state[i][j]==q_mark_char)
clear(i,j);
state[i][j]=(char)(48+number[i][j]);
num_swept++;
return;
}
//now handling only remaining case i.e. 0
sweep_util(i,j,s); //this will only flood fill zeroes
int l,m;
num_swept=0; // making num_swept zero becasue i will be counting it again
//now adding boundary elements of zero flood by checking neighbours of each element. If the neighbouring element is found to be s i.e. visited zero, it's neighbor must be visible
for(l=0;l<r;l++)
{
for(m=0;m<c;m++)
{
bool is_tile_flagged=false;
bool is_tile_q_mark=false;
if(state[l][m]==flag_char)
is_tile_flagged=true;
if(state[l][m]==q_mark_char)
is_tile_q_mark-true;
if(l==0&&m==0) // checking top left corner
{
if((number[1][0]==s||number[1][1]==s||number[0][1]==s)&&number[0][0]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(l==0&&m==c-1) // checking top right corner
{
if((number[0][c-2]==s||number[1][c-2]==s||number[1][c-1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(l==r-1&&m==0) // checking bottom left corner
{
if((number[r-2][0]==s||number[r-2][1]==s||number[r-1][1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(l==r-1&&m==c-1) // checking bottom right corner
{
if((number[r-1][c-2]==s||number[r-2][c-2]==s||number[r-2][c-1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(l==0) // checking upper boundary tiles
{
if((number[l][m-1]==s||number[l][m+1]==s||number[l+1][m-1]==s||number[l+1][m]==s||number[l+1][m+1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(m==0) // checking left boundary tiles
{
if((number[l-1][m]==s||number[l-1][m+1]==s||number[l][m+1]==s||number[l+1][m]==s||number[l+1][m+1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(m==c-1) // checking right boundary tiles
{
if((number[l-1][m-1]==s||number[l-1][m]==s||number[l][m-1]==s||number[l+1][m-1]==s||number[l+1][m]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if(l==r-1) // checking lower boundary tiles
{
if((number[l-1][m-1]==s||number[l-1][m]==s||number[l-1][m+1]==s||number[l][m-1]==s||number[l][m+1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
}
else if((number[l-1][m-1]==s||number[l-1][m]==s||number[l-1][m+1]==s||number[l][m-1]==s||number[l][m+1]==s||number[l+1][m-1]==s||number[l+1][m]==s||number[l+1][m+1]==s)&&number[l][m]!=s)
{
state[l][m]=(char)(48+number[l][m]);
}
if(state[l][m]!=unswept_char&&state[l][m]!=q_mark_char&&state[l][m]!=flag_char)
num_swept++;
if(is_tile_flagged&&state[l][m]!=flag_char || is_tile_q_mark&&state[l][m]!=q_mark_char)
{clear(l,m);
if(number[l][m]==s)
state[l][m]=swept_char;
else
state[l][m]=(char)(48+number[l][m]);}
}//closing m loop
}// closing l loop
} // closed void sweep_from function
bool check_win() //accessor
{
if(num_swept+num_mines==r*c)return true;
else return false;
}
bool check_loss() //accessor
{
return loss;
}
char get_state_of(int i,int j) //accessor
{
return state[i][j];
}
int get_num_flags() //accessor
{
return num_flagged;
}
int get_num_rows() //accessor
{
return r;
}
int get_num_cols() //accessor
{
return c;
}
int get_num_swept()
{
return num_swept;
}
int get_num_of(int i,int j){return number[i][j];}
};
void clear()
{
cout<<"\033[2J\033[1;1H";
}
void print(Minefield m)
{
int r=m.get_num_rows();
int c=m.get_num_cols();
cout<<"\n";
for(int i=0;i<r;i++)
{
if(i==0)
{
cout<<" ";
for(int x=0;x<c;x++)
{
if(x<10) cout<<x<<" ";
else cout<<x<<" ";
}
cout<<"\n";
}
// if(i<10) cout<<" "<<i<<" ";
// else cout<<" "<<i<<" ";
cout<<" ";
for(int j=0;j<c;j++)
{
cout<<"+---";
}
cout<<'+'<<endl;
if(i<10) cout<<" "<<i<<" | ";
else cout<<" "<<i<<" | ";
// cout<<" | ";
for(int j=0;j<c;j++)
{
cout<<m.get_state_of(i,j)<<" | ";
}
cout<<" ";
for(int j=0;j<c;j++)
{
if(m.get_num_of(i,j)>=0)cout<<" ";
cout<<m.get_num_of(i,j)<<" ";
}
cout<<endl;
if(i==r-1) cout<<" ";
}
cout<<" ";
for(int j=0;j<c;j++)
{
cout<<"+---";
}
cout<<"+";
}
int main()
{
clear();
char dummy;
cout<<"choose dimensions:\n";
int r,c;
cin>>r;
cin>>c;
cout<<"\nchoose initial co-ordinates\n";
int i,j;
cin>>i;
cin>>j;
//determine number of mines
Minefield m = Minefield(r,c,(r+c),i,j);
cin>>dummy;
char input;
do
{
clear();
print(m);
cout<<endl;
cout<<"Number of tiles swept = "<<m.get_num_swept()<<endl;
cout<<"Number of flags = "<<m.get_num_flags()<<endl;
cout<<'\n';
cin>>input;
if(input == 'e')break;
cin>>i>>j;
if(input == 'f')m.flag(i,j);
else if(input == 'q')m.q_mark(i,j);
else if(input == 's')m.sweep_from(i,j);
else if(input == 'c')m.clear(i,j);
} while(true);
clear();
cout<<"End of test";
cin>>dummy;
return 0;
}