-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.cpp
254 lines (205 loc) · 6.39 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
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
#include <ncurses.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#include <vector>
//unistd.h doesn't have milisecond sleep so I jut define one
#define msleep(msec) usleep(msec * 1000)
//bruh just testing
using namespace std;
struct pos{
int x;
int y;
};
int main(int argc, char ** argv){
using boost::lexical_cast;
using boost::bad_lexical_cast;
int ch;
int curx, cury;
char Swidth[10], Sheight[10], Smines[10];
int Iwidth, Iheight, Imines;
bool con;
int padding = 5; //how much space is between playing field and status (bar?)
int ranx, rany;
srand(time(0));
//screen init
initscr();
raw();
keypad(stdscr, true);
noecho();
scrollok(stdscr, true);
//end of screen init
printw("Hello, fellow minesweeper, I have a job for you!\n");
printw("So, what are you waiting for? Tell me what height, width and number of mines you want\n");
echo();
do{
try{
mvprintw(2, 0, "Height: ");
getstr(Sheight);
Iheight = boost::lexical_cast<int>(Sheight);
con = true;
}catch(bad_lexical_cast &){
con = false;
printw("nice try dishpit");
}
}while(!con);
do{
try{
mvprintw(3, 0, "Width: ");
getstr(Swidth);
Iwidth = boost::lexical_cast<int>(Swidth);
con = true;
}catch(bad_lexical_cast &){
con = false;
printw("nice try dishpit");
}
}while(!con);
do{
try{
mvprintw(4, 0, "Number of mines: ");
getstr(Smines);
Imines = boost::lexical_cast<int>(Smines);
con = true;
}catch(bad_lexical_cast &){
con = false;
printw("nice try dishpit");
}
}while(!con);
noecho();
clear();
curs_set(0);
mvprintw(0, Iwidth * 2 + padding, "H:%d, W:%d, M:%d", Iheight, Iwidth, Imines);
refresh();
int field[Iwidth][Iheight]; // x, y coords
bool fieldMask[Iwidth][Iheight];
for(int i = 0; i < Iwidth; i++){
fill(field[i], field[i] + Iheight, 0);
}
for(int i = 0; i < Iwidth; i++){
fill(fieldMask[i], fieldMask[i] + Iheight, true); //true is for # (tile is still hidden)
}
//mines prep
if(Imines > (Iwidth * Iheight)){
Imines = (Iwidth * Iheight);
}
for(int i = 0; i < Imines; i++){
ranx = rand() % (Iwidth);
rany = rand() % (Iheight);
if(field[ranx][rany] == -1){
Imines++;
continue;
}
field[ranx][rany] = -1;
if(field[ranx-1][rany] != -1 && ranx != 0){
field[ranx-1][rany] += 1;
}
if(field[ranx+1][rany] != -1 && ranx != (Iwidth - 1)){
field[ranx+1][rany] += 1;
}
if(field[ranx-1][rany-1] != -1 && ranx != 0 && rany != 0){
field[ranx-1][rany-1] += 1;
}
if(field[ranx][rany-1] != -1 && rany != 0){
field[ranx][rany-1] += 1;
}
if(field[ranx+1][rany-1] != -1 && ranx != (Iwidth - 1) && rany != 0){
field[ranx+1][rany-1] += 1;
}
if(field[ranx+1][rany+1] != -1 && ranx != (Iwidth - 1) && rany != (Iheight - 1)){
field[ranx+1][rany+1] += 1;
}
if(field[ranx][rany+1] != -1 && rany != (Iheight - 1)){
field[ranx][rany+1] += 1;
}
if(field[ranx-1][rany+1] != -1 && ranx != 0 && rany != (Iheight - 1)){
field[ranx-1][rany+1] += 1;
}
}
for(int i = 0; i < Iheight; i++){
for(int j = 0; j < Iwidth; j++){
mvprintw(i, (j * 2), "%c", (fieldMask[j][i]) ? '#' : field[j][i] + 48); //;)
refresh();
msleep(50);
}
}
curs_set(2);
curx = (Iwidth / 2) * 2;
cury = Iheight / 2;
move(curx, cury);
while(ch != 27){
move(cury, curx);
ch = getch();
if(ch == 259 || ch == 119){ //up
cury --;
} else if(ch == 258 || ch == 115){ //down
cury ++;
} else if(ch == 260 || ch == 97){ //right
curx -= 2;
} else if(ch == 261 || ch == 100){ //left
curx += 2;
} else if(ch == 10){
curs_set(0);
pos bfsPos;
bfsPos.x = curx/2;
bfsPos.y = cury;
pos currentPos;
vector<pos> queue;
queue.emplace(queue.begin(), bfsPos);
while(!queue.empty()){
currentPos = queue[0];
queue.erase(queue.begin());
if(!fieldMask[currentPos.x][currentPos.y]){continue;}
if(currentPos.x < 0 || currentPos.y < 0 || currentPos.x > Iwidth - 1 || currentPos.y > Iheight - 1){continue;}
fieldMask[currentPos.x][currentPos.y] = false;
mvprintw(currentPos.y, currentPos.x * 2, "%c", field[currentPos.x][currentPos.y] + 48);
if(field[currentPos.x][currentPos.y] == 0){
refresh();
msleep(20);
currentPos.x++;
queue.push_back(currentPos);
currentPos.y++;
queue.push_back(currentPos);
currentPos.x--;
queue.push_back(currentPos);
currentPos.x--;
queue.push_back(currentPos);
currentPos.y--;
queue.push_back(currentPos);
currentPos.y--;
queue.push_back(currentPos);
currentPos.x++;
queue.push_back(currentPos);
currentPos.x++;
queue.push_back(currentPos);
}
}
curs_set(2);
} // space 32
//boundary check ;^p
if(curx < 0){
curx = 0;
}
if(cury < 0){
cury = 0;
}
if(curx >= (Iwidth * 2)){
curx = (Iwidth * 2) - 2;
}
if(cury >= Iheight){
cury = Iheight - 1;
}
refresh();
}
curs_set(0);
mvprintw(0, Iwidth * 2 + padding, "press any key to end program ");
refresh();
ch = getch();
printw("%d", ch);
refresh();
getch();
endwin();
return 0;
}