-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
185 lines (146 loc) · 5.39 KB
/
main.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
#include <iostream>
#include <random>
#include <cstring>
namespace {
const char mine = '*';
const char zero = '0';
const char space = ' ';
class Minesweeper {
public:
Minesweeper(const size_t width, const size_t height, const int bomb)
: width(width), height(height), mines(bomb), size(width * height),
table(new char[size]), playerTable(new char[size]),
endGame(false), unrevealed(size) { fillTable(); }
void printTable() const {
std::cout <<"\nTable:\n\n ";
for (int i = 1; i <= width; ++i) { std::cout << i << " "; }
std::cout << "\n ";
for (int i = 0; i <= width; ++i) { std::cout << "--"; }
std::cout << "-\n ";
for (int y = 0; y < height; y++) {
std::cout << y+1 << " | ";
for(int x = 0; x < width; x++) { std::cout << playerTable[y*width+x] <<" "; }
std::cout << "|\n";
if (y < 8) std::cout << " ";
}
std::cout << " ";
for (int i = 0; i <= width; ++i) { std::cout << "--"; }
std::cout << "-\n";
}
bool isGameEnd() const { return endGame; }
bool win() const { return unrevealed == mines; }
void reveal(size_t x, size_t y) {
if (!isInside(x, y)) {
std::cout <<"\nInvalid coordinates!\n";
return;
}
size_t index = getCoordinate(x, y);
char *field = &table[index];
if (*field == mine) {
playerTable[index] = mine;
endGame = true;
std::cout <<"\nBOOOM! YOU DIED!\n";
solve();
} else if (*field == '0') {
revealNeighbors(x, y);
} else {
playerTable[index] = *field;
unrevealed--;
}
if (win()) {
endGame = true;
std::cout <<"\nCONGRATS! YOU WON!\n";
solve();
}
printTable();
}
virtual ~Minesweeper() {
delete[] table;
delete[] playerTable;
}
private:
void fillTable() {
memset(table, zero, size);
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<size_t> randIndex(0, size);
unsigned mineCount = 0;
do {
size_t place = randIndex(gen);
if(table[place] != mine) {
table[place] = mine;
setNeighbours(place);
mineCount++;
}
} while (mineCount < mines);
}
void setNeighbours(size_t index) {
size_t x = index%width;
size_t y = (index >= width) ? (index-x)/width : 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (!isInside(x+j, y+i)) continue;
size_t field = getCoordinate(x+j, y+i);
if (isMine(field)) continue;
incrementValue(field);
}
}
}
void incrementValue(size_t index) { table[index] = table[index] - '0' + 1 + '0'; }
bool isMine(size_t index) { return table[index] == mine; }
size_t getCoordinate(size_t x, size_t y){ return y*width+x; }
bool isInside(size_t x, size_t y) { return (x >= 0)&&(x < width)&&(y >= 0)&&(y<width); }
void revealNeighbors(size_t x, size_t y) {
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (!isInside(x+j, y+i)) continue;
size_t neighbor = getCoordinate(x+j, y+i);
if (isMine(neighbor)) continue;
if (table[neighbor] == zero) {
if (playerTable[neighbor] != space) {
playerTable[neighbor] = space;
unrevealed--;
revealNeighbors(x+j, y+i);
}
} else if (playerTable[neighbor] != table[neighbor]) {
playerTable[neighbor] = table[neighbor];
unrevealed--;
}
}
}
}
void solve() {
for (size_t i=0; i<size; i++) {
if (playerTable[i] == space || playerTable[i] == table[i]) continue;
if (table[i] == zero) { playerTable[i] = space; }
else { playerTable[i] = table[i]; }
}
}
const size_t width, height, size;
size_t unrevealed;
const int mines;
bool endGame;
char* table;
char* playerTable;
};
}
int main() {
std::cout <<"\nWelcome!";
try {
Minesweeper ms(10, 10, 15);
std::cout <<"\nYour task is leave only the mines unrevealed!\n";
ms.printTable();
size_t x, y;
do {
std::cout <<"\nx: ";
std::cin >> x;
std::cout <<"y: ";
std::cin >> y;
ms.reveal(x-1, y-1);
} while (!ms.isGameEnd());
} catch (const std::bad_alloc &e) {
std::cerr << "Couldn't allocate enough memory for minesweeper table" << std::endl;
return EXIT_FAILURE;
}
return 0;
}