-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.cpp
198 lines (141 loc) · 5.51 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
#include "Minesweeper.h"
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
Minesweeper::Minesweeper(int row, int col){
this->row = row;
this->col = col;
this->opened = 0;
// calculate Total number of Mines.
row * col < 7 ? totalMineCount = 2
: row * col < 10 ? totalMineCount = 3
: totalMineCount = (int)(round( sqrt(row * col)+0.5 ) );
board = new char*[row];
for (int i = 0; i < row; i++)
board[i] = new char[col];
hiddenBoard = new char*[row];
for (int i = 0; i < row; i++)
hiddenBoard[i] = new char[col];
for (int i = 0; i < row ; i++)
for (int j = 0; j < col; j++)
board[i][j] = '-';
equalizeBoards();
}
int Minesweeper::getRow(){return row;}
int Minesweeper::getCol(){return col;}
int Minesweeper::getTotalMineCount(){return totalMineCount;}
void Minesweeper::printBoard(){
cout<<"\033[31m"<<endl;
for(int i = 0; i < row ;i++){
for(int j = 0; j < col ; j++){
cout<<"\33[40m";
if( board[i][j] == '-' )
cout<<"\33[37m"<< ( j && j != col ? " " : "" ) <<"- ";
else if( board[i][j] == '*' )
cout<<"\33[101m\e[01m"<< ( j && j != col ? " " : "" ) <<board[i][j]<<" "<<"\e[0m\33[0m";
else if( board[i][j] == '1' )
cout<<"\33[34m\e[01m"<< ( j && j != col ? " " : "" ) <<board[i][j]<<" "<<"\e[0m\33[0m";
else if( board[i][j] == '2' )
cout<<"\33[32m\e[01m"<< ( j && j != col ? " " : "" ) <<board[i][j]<<" "<<"\e[0m\33[0m";
else if( board[i][j] == '3' )
cout<<"\33[31m\e[01m"<< ( j && j != col ? " " : "" ) <<board[i][j]<<" "<<"\e[0m\33[0m";
else
cout<<"\33[35m\e[01m"<< ( j && j != col ? " " : "" ) <<board[i][j]<<" "<<"\e[0m\33[0m";
}
cout<<"\n";
}
cout<<endl<<"\033[0m";
}
bool Minesweeper::isExist(int r, int c){
return board[r][c] == '*';
}
void Minesweeper::locateMines(){
srand(time(0));
int r,c;
for(int i = 0 ; i < this->totalMineCount ;){
r = rand() % row;
c = rand() % col;
if(!isExist(r,c)){
hiddenBoard[r][c] = '*';
i++;
}
}
}
void Minesweeper::equalizeBoards(){
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
hiddenBoard[i][j] = board[i][j];
}
void Minesweeper::printHiddenBoard(){
cout<<"\033[31m"<<endl;
for(int i = 0; i < row ;i++){
for(int j = 0; j < col ; j++){
cout<<"\33[40m";
if( hiddenBoard[i][j] == '-' )
cout<<"\33[37m"<< ( j && j != col ? " " : "" ) <<"- ";
else if( hiddenBoard[i][j] == '*' )
cout<<"\33[101m\e[01m"<< ( j && j != col ? " " : "" ) <<hiddenBoard[i][j]<<" "<<"\e[0m\33[0m";
else if( hiddenBoard[i][j] == '1' )
cout<<"\33[34m\e[01m"<< ( j && j != col ? " " : "" ) <<hiddenBoard[i][j]<<" "<<"\e[0m\33[0m";
else if( hiddenBoard[i][j] == '2' )
cout<<"\33[32m\e[01m"<< ( j && j != col ? " " : "" ) <<hiddenBoard[i][j]<<" "<<"\e[0m\33[0m";
else if( hiddenBoard[i][j] == '3' )
cout<<"\33[31m\e[01m"<< ( j && j != col ? " " : "" ) <<hiddenBoard[i][j]<<" "<<"\e[0m\33[0m";
else
cout<<"\33[35m\e[01m"<< ( j && j != col ? " " : "" ) <<hiddenBoard[i][j]<<" "<<"\e[0m\33[0m";
}
cout<<"\n";
}
cout<<endl<<"\033[0m";
}
int Minesweeper::calcMinesCountOfSquare(int r, int c){
int count = 0;
//top
count += r-1 >= 0 && hiddenBoard[r-1][c] == '*' ? 1 : 0;
//right
count += c+1 < col && hiddenBoard[r][c+1] == '*' ? 1 : 0;
//bottom
count += r+1 < row && hiddenBoard[r+1][c] == '*' ? 1 : 0;
//left
count += c-1 >= 0 && hiddenBoard[r][c-1] == '*' ? 1 : 0;
//top - left
count += r-1 >= 0 && c-1 >= 0 && hiddenBoard[r-1][c-1] == '*' ? 1 : 0;
//top - right
count += r-1 >= 0 && c+1 < col && hiddenBoard[r-1][c+1] == '*' ? 1 : 0;
//bottom - left
count += r+1 < row && c-1 >= 0 && hiddenBoard[r+1][c-1] == '*' ? 1 : 0;
//bottom - right
count += r+1 < row && c+1 < col && hiddenBoard[r+1][c+1] == '*' ? 1 : 0;
return count;
}
void Minesweeper::calcMinesCountAround(){
for(int i = 0; i < row ;i++)
for(int j = 0; j < col ;j++){
if(hiddenBoard[i][j] != '*')
hiddenBoard[i][j] = '0' + calcMinesCountOfSquare(i,j);
}
}
int Minesweeper::makeAMove(int r, int c){
if ( r > row || r < 0 || c > col || c < 0 )
return 0;
if(hiddenBoard[r][c] == '*'){
cout<<"\t\33[31m------------------------:::\33[33mYou LOSE\033[0m\33[31m:::------------------------\033[0m";
printHiddenBoard();
return -1;
}else{
if(board[r][c] == '-'){
opened++;
}
if(opened + totalMineCount == row * col ){
cout<<"\t\33[31m------------------------:::\33[33mYou Win\033[0m\33[31m:::------------------------\033[0m";
printHiddenBoard();
return -1;
}
cout<<"Opened: "<<opened;
strncpy ( &board[r][c], &hiddenBoard[r][c], sizeof(hiddenBoard[r][c]) );
printBoard();
}
}