-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (150 loc) · 5.35 KB
/
main.cpp
File metadata and controls
164 lines (150 loc) · 5.35 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
#include <iostream>
#include <vector>
namespace {
class Minesweeper {
public:
Minesweeper(const size_t width, const size_t height)
: width(width), height(height),
table(new char[width * height]), isRevealedTable(new bool[width * height]) {
fillTable();
}
/* In a real implementation there would also be a
* - copy constructor
* - assignment operator
* - move constructor
* - move assignment operator
* We will learn about them later
*/
virtual ~Minesweeper() {
delete[] table;
delete[] isRevealedTable;
}
void countNeighbours() {
for(int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int counter = 0;
if(*(table + i*width+j) != '*'){
for(int k = std::max(0, i-1); k <= std::min((int)height, i+1); k++){
for(int l = std::max(0, j-1); l <= std::min((int)width, j+1); l++){
if(*(table + k*width+l) == '*'){
++counter;
}
}
}
char counterChar = '0' + counter;
*(table + i*width+j) = counterChar;
}
}
}
}
void printTable() const {
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
std::cout << *(table + i*width+j) << " ";
}
std::cout << std::endl;
}
}
void printGameTable() const {
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
if(*(isRevealedTable + i*width+j)){
std::cout << *(table + i*width+j) << " ";
} else {
std::cout << '#' << " ";
}
}
std::cout << std::endl;
}
}
std::vector<int> getUserInput(){
int w, h;
std::vector<int> result;
do {
std::cout << std::endl << "Enter width and height to reveal: " << std::endl;
try {
std::cin >> w >> h;
} catch (std::exception& e){
w = 0;
h = 0;
}
} while (w < 0 || w > width || h < 0 || h > height);
result.push_back(w);
result.push_back(h);
return result;
}
void reveal(const int &w, const int &h){
if(*(table + h*width+w) == '*'){
std::cout << "Boooooom!";
exit(0);
} else {
*(table + h*width+w) = '.';
if(*(isRevealedTable + h*width+w) == false) {
numberOfRevealedCells++;
}
*(isRevealedTable + h*width+w) = true;
for(int k = std::max(0, h-1); k <= std::min((int)height-1, h+1); k++){
for(int l = std::max(0, w-1); l <= std::min((int)width-1, w+1); l++){
std::cout << k << l << std::endl;
if(*(table + k*width+l) == '0') {
reveal(l, k);
}
if(*(isRevealedTable + k*width+l) == false) {
numberOfRevealedCells++;
}
*(isRevealedTable + k*width+l) = true;
}
}
}
}
bool isWon(){
return numberOfRevealedCells == width * height;
}
private:
void fillTable() {
srand (time(NULL));
int luckFactor;
const int mineRatio = 15;
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
luckFactor = rand() % 100 + 1;
// increment pointer, and dereference unincremented address
*table++ = (luckFactor > mineRatio ? '.' : '*');
*isRevealedTable++ = false;
}
}
// resetting the pointers to the 0th element of tables
table -= width * height * sizeof(*table);
isRevealedTable -= width * height * sizeof(*isRevealedTable);
}
const size_t width, height;
char *table;
bool *isRevealedTable;
int numberOfRevealedCells = 0;
};
}
int main() {
try {
Minesweeper ms(5, 5);
ms.printTable();
ms.countNeighbours();
std::cout << std::endl;
ms.printTable();
std::cout << std::endl;
ms.printGameTable();
while (true) {
std::vector<int> userCoordinates = ms.getUserInput();
ms.reveal(userCoordinates[0], userCoordinates[1]);
std::cout << std::endl;
ms.printGameTable();
if(ms.isWon()){
std::cout << "Congratulations, you won!" << std::endl;
exit(0);
}
}
} catch (const std::bad_alloc &e) {
std::cerr << "Couldn't allocate enough memory for minesweeper table" << std::endl;
return EXIT_FAILURE;
}
return 0;
}