-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.cpp
128 lines (121 loc) · 3.27 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
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int rem = 0;
bool won = 0;
const int dim = 10;
const int bombs = 10;
char m[dim][dim];
int rndint() {
return rand() % dim;
}
void p(string s) {
cout << s;
}
void c(int w, int z) {
m[w][z] += 2;
rem--;
for (int x = -(w >= 1); x < (w > 8 ? 1 : 2); x++) {
for (int y = -(z >= 1); y < (z > 8 ? 1 : 2); y++) {
if (m[x + w][y + z] & 2 || m[x + w][y + z] & 4) continue;
if (m[x + w][y + z] >> 3 == 0) c(x + w, y + z);
else {
m[x + w][y + z] += 2;
rem--;
}
}
}
}
void r() {
#if defined _WIN32
system("cls");
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
system("clear");
#endif
p(" _) \n");
p(" __ `__ \\ | __ \\ _ \\ __|\\ \\ \\ / _ \\ _ \\ __ \\ _ \\ __| \n");
p(" | | | | | | __/\\__ \\ \\ \\ \\ / __/ __/ | | __/ | \n");
p("_| _| _|_|_| _|\\___|____/ \\_/\\_/ \\___|\\___| .__/ \\___|_| \n");
p(" _| \n");
p("rxy to reveal x,y, fxy to place flag on x,y\n\n 0 1 2 3 4 5 6 7 8 9\n\n");
for (int y = 0; y < dim; y++) {
cout << y << " ";
for (int x = 0; x < dim; x++) {
if (m[x][y] & 4)
p("f ");
else if (m[x][y] & 2)
if (m[x][y] & 1)
p("x ");
else if (m[x][y] >> 3 == 0)
p(" ");
else
cout << ((int)(m[x][y]) >> 3) << " ";
else
p("# ");
}
p("\n");
}
}
int main()
{
srand(time(0));
rem = dim * dim;
memset(m, 0, sizeof(m));
for (int i = 0; i < bombs; i++) {
int w = rndint();
int z = rndint();
if (m[w][z] & 1) {
i--;
continue;
}
m[w][z] = 1;
for (int x = -(w >= 1); x < (w > 8 ? 1 : 2); x++) {
for (int y = -(z >= 1); y < (z > 8 ? 1 : 2); y++) {
if (x == 0 && y == 0) continue;
m[x + w][y + z] = m[x + w][y + z] + 8;
}
}
}
r();
while (1) {
string j;
getline(cin, j);
string g = j.substr(0, 1);
if (g == "r") {
int x = stoi(j.substr(1, 1));
int y = stoi(j.substr(2, 1));
if (m[x][y] & 2 || m[x][y] & 4)continue;
if (m[x][y] & 1) {
m[x][y] += 2;
break;
}
if (m[x][y] >> 3 == 0) {
char(*p)[dim][dim] = &m;
c(x, y);
}
else {
m[x][y] = m[x][y] + 2;
rem--;
}
}
if (g == "f") {
int x = stoi(j.substr(1, 1));
int y = stoi(j.substr(2, 1));
if (m[x][y] & 2) continue;
if (m[x][y] & 4) {
m[x][y] -= 4;
continue;
}
m[x][y] += 4;
}
if (rem == bombs) {
won = 1;
break;
}
r();
}
r();
p((won ? "You win!\n" : "You lose!\n"));
return 0;
}