blob: 327e179e16e56158f6a717b036c3997082fa7dfe (
plain)
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
|
#include "Game.hpp"
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
void Game::initDiff(int diff){
switch(diff){
case 0:
boardWidth = 9;
boardHeight = 9;
mines = 9;
break;
case 1:
boardWidth = 16;
boardHeight = 16;
mines = 12;
break;
case 2:
boardWidth = 30;
boardHeight = 16;
mines = 16;
break;
case 3:
std::cout << "Editing game parameters\n";
break;
}
}
void Game::initBoard(){
for(int y=0; y < boardHeight; y++){
for(int x=0; x < boardWidth; x++){
bombMap[y][x] = false;
revealed[y][x] = false;
tileMap[y][x] = 0;
}
}
int placed = 0;
while(placed < mines){
int x = rand() % boardWidth;
int y = rand() % boardHeight;
if(!bombMap[y][x]){
bombMap[y][x] = true;
placed++;
}
}
}
void Game::updateBoard(){
for(int y=0; y < boardHeight; y++){
for(int x=0; x < boardWidth; x++){
if(!revealed[y][x]) continue;
int bombs = bombCheck(x, y);
tileMap[y][x] = bombs == 0 ? 1 : bombs + 1;
}
}
}
int Game::editDiff(){
std::cout << "Now editing custom difficulty" << std::endl;
std::cout << "Board width: ";
std::cin >> boardWidth;
if(boardWidth > MAX_W){
std::cout << "Board width can't be more than " << MAX_W << " tiles." << std::endl;
return 1;
}
std::cout << "Board height: ";
std::cin >> boardHeight;
if(boardHeight > MAX_H){
std::cout << "Board height can't be more than " << MAX_H << " tiles." << std::endl;
return 1;
}
std::cout << "Mines: ";
std::cin >> mines;
if(mines > 8 || mines > (boardWidth * boardHeight)){
std::cout << "There can't be more than 8 or " << (boardWidth * boardHeight) << "mines." << std::endl;
return 1;
}
return 0;
}
/* Tile map:
0 - unrevealed tile
1 - revealed tile
2 - revealed tile with 1 bomb around
3 - revealed tile with 2 bomb around
4 - revealed tile with 3 bomb around
5 - revealed tile with 4 bomb around
6 - revealed tile with 5 bomb around
7 - revealed tile with 6 bomb around
8 - revealed tile with 7 bomb around
9 - revealed tile with 8 bomb around
10 - flag
11 - bomb (for DEBUGGING)
*/
void Game::displayBoard() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
for(int y=0; y < boardHeight; y++){
for(int x = 0; x < boardWidth; x++){
if(tileMap[y][x] == 0) putchar('#');
else if(tileMap[y][x] == 1) putchar('.');
else putchar('0' + tileMap[y][x] - 1);
}
putchar('\n');
}
}
int Game::bombCheck(int x, int y){
int count = 0;
for(int dy = -1; dy <= 1; dy++){
for(int dx = -1; dx <= 1; dx++){
if(dx == 0 && dy == 0) continue;
int nx = x + dx;
int ny = y + dy;
if(nx >= 0 && nx < boardWidth && ny >= 0 && ny < boardHeight && bombMap[ny][nx]){
count++;
}
}
}
return count;
}
void Game::input(){
std::string cmd;
std::cout << "game> ";
std::cin >> cmd;
if(std::cin.fail()){
std::cout << "Input failed." << std::endl;
return;
}
if(cmd == "q"){
inGame = false;
return;
}
int x, y;
std::cin >> x >> y;
if(x < 0 || x >= boardWidth || y < 0 || y >= boardHeight) return;
if(cmd == "d"){
if(bombMap[y][x]){
std::cout << "BOOM! Game over\n";
inGame = false;
return;
}
if(!revealed[y][x]){
revealed[y][x] = true;
if(bombCheck(x, y) == 0){
for(int dy = -1; dy <= 1; dy++){
for(int dx = -1; dx <= 1; dx++){
int nx = x + dx;
int ny = y + dy;
if(nx >= 0 && nx < boardWidth && ny >= 0 && ny < boardHeight && !revealed[ny][nx]){
revealed[ny][nx] = true;
}
}
}
}
}
}
}
|