-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.hpp
340 lines (302 loc) · 7.36 KB
/
board.hpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#pragma once
#include <vector>
#include <iostream>
#include <utility>
#include "init.hpp"
#include "cell.hpp"
#include "colour.hpp"
#include "display.hpp"
const int LENGTH = 8;
const int BREADTH = 8;
const int PADDING = 4;
enum TURN{
PLAYER_ONE,
PLAYER_TWO
};
class Board
{
public:
Board();
void draw();
void updateBoardOntoBuffer();
void getMove();
void placeDisk();
bool isValidMove(int, int);
int noOfFlippedDiscs(Cell_States, int, int, int, int, int);
void flipDiscs(Cell_States, int, int, int, int);
bool isGameOver();
void declareWinner();
bool isNeighbouringDisk(int, int);
bool isAtCurrPos(int, int);
bool isValidIndex(int , int);
int setDirX(int);
int setDirY(int);
friend class Computer;
TURN turn;
private:
int l;
int b;
int x;
int y;
int noOfBlackDisks;
int noOfWhiteDisks;
COLOUR cursorCol;
std::vector<std::string> buf;
std::vector<std::vector<Cell>> cells;
bool dirs[9];
};
Board::Board(){
l = LENGTH;
b = BREADTH;
turn = PLAYER_ONE;
cells = std::vector<std::vector<Cell>>(b, std::vector<Cell>(l, Cell()));
x = 1;
y = 1;
cursorCol = RED;
noOfBlackDisks = noOfWhiteDisks = 2;
cells[(l-1)/2][(b-1)/2].state = WHITE;
cells[l/2][(b-1)/2].state = BLACK;
cells[(l-1)/2][b/2].state = BLACK;
cells[l/2][b/2].state = WHITE;
}
void Board::updateBoardOntoBuffer(){
std::string s = std::string(PADDING, ' ');
for(int j = 0; j < l; ++j){
if(j == 0){
s += "┏";
for(int i = 0; i < b-1; ++i){
if(isAtCurrPos(j, i)) s += cursorCol + "━━━" + RESET +"┳";
else s += "━━━┳";
}
if(isAtCurrPos(j, b-1)) s += cursorCol + "━━━" + RESET +"┓";
else s += "━━━┓";
buf.push_back(s);
s = std::string(PADDING, ' ');
}
if(j > 0 && j < l){
s += "┣";
for(int i = 0; i < b-1; ++i){
if(isAtCurrPos(j-1, i) || isAtCurrPos(j, i)) s += cursorCol + "━━━" + RESET + "╋";
else s += "━━━╋";
}
if(isAtCurrPos(j-1, b-1)|| isAtCurrPos(j, b-1)) s += cursorCol + "━━━" + RESET +"┫";
else s += "━━━┫";
buf.push_back(s);
s = std::string(PADDING, ' ');
}
if(isAtCurrPos(j, 0)) s += cursorCol + "┃" + RESET;
else s += "┃";
for(int i = 0; i < b; ++i){
switch(cells[j][i].state){
case EMPTY:
s += " ";
break;
case BLACK:
s += "⚫️";
break;
case WHITE:
s += "⚪";
break;
}
if(isAtCurrPos(j, i) || isAtCurrPos(j, i+1)) s += cursorCol + " ┃" + RESET;
else s += " ┃";
}
buf.push_back(s);
s = std::string(PADDING, ' ');
if(j == l-1){
s += "┗";
for(int i = 0; i < b-1; ++i){
if(isAtCurrPos(j, i)) s += cursorCol + "━━━" + RESET + "┻";
else s += "━━━┻";
}
if(isAtCurrPos(j, b-1)) s += cursorCol + "━━━" + RESET + "┛";
else s += "━━━┛";
buf.push_back(s);
s = std::string(PADDING, ' ');
}
}
//for(auto str: buf) std::cout << str;
//std::cout << noOfWhiteDisks << " " << noOfBlackDisks << "\n"; //debugging code
}
void Board::draw(){
buf.clear();
updateBoardOntoBuffer();
displayControls(buf);
displayScore(noOfBlackDisks, noOfWhiteDisks, buf);
for(auto str: buf) std::cout << str << "\n";
}
void Board::getMove(){
KEY k = getKey();
switch (k){
case K_UP:
if(isValidIndex(y-1, x)) --y;
if(isValidMove(y, x)) cursorCol = GREEN;
else cursorCol = RED;
return;
case K_DOWN:
if(isValidIndex(y+1, x)) ++y;
if(isValidMove(y, x)) cursorCol = GREEN;
else cursorCol = RED;
return;
case K_LEFT:
if(isValidIndex(y, x-1)) --x;
if(isValidMove(y, x)) cursorCol = GREEN;
else cursorCol = RED;
return;
case K_RIGHT:
if (isValidIndex(y, x+1)) ++x;
if(isValidMove(y, x)) cursorCol = GREEN;
else cursorCol = RED;
return;
case K_SPACE:
if(cursorCol == GREEN){
placeDisk();
for(int k = 0; k < 9; ++k){
if(dirs[k]){
int dir_y = setDirY(k);
int dir_x = setDirX(k);
flipDiscs(cells[y][x].state, y+dir_y, x+dir_x, dir_y, dir_x);
}
}
cursorCol = RED;
}
return;
}
}
void Board::placeDisk(){
if(turn == PLAYER_ONE){
cells[y][x].state = BLACK;
++noOfBlackDisks;
turn = PLAYER_TWO;
}
else{
cells[y][x].state = WHITE;
++noOfWhiteDisks;
turn = PLAYER_ONE;
}
}
bool Board::isValidMove(int y_pos, int x_pos){
if(cells[y_pos][x_pos].state != EMPTY) return false;
if(! isNeighbouringDisk(y_pos, x_pos)) return false;
Cell_States s = turn == PLAYER_ONE ? BLACK : WHITE;
int count = 0;
bool dirs_copy[9];
for(int j = -1; j <= 1; ++j){
for(int i = -1; i <= 1; ++i){
if(j == 0 && i == 0){
dirs_copy[count++] = false;
continue;
}
if(isValidIndex(y_pos+j, x_pos+i)){
if(cells[y_pos+j][x_pos+i].state == EMPTY) dirs_copy[count] = false;
else if(cells[y_pos+j][x_pos+i].state == s) dirs_copy[count] = false;
else dirs_copy[count] = true;
}
else dirs_copy[count] = false;
++count;
}
}
int search_dir_x, search_dir_y;
bool flag = false;
for(int k = 0; k < 9; ++k){
if(dirs_copy[k]){
search_dir_y = setDirY(k);
search_dir_x = setDirX(k);
if(noOfFlippedDiscs(s, y_pos+search_dir_y, x_pos+search_dir_x, search_dir_y, search_dir_x, 0) > 0){
flag = true;
}
else dirs_copy[k] = false;
}
}
if(isAtCurrPos(y_pos, x_pos)){
for(int i = 0; i < 9; ++i){
dirs[i] = dirs_copy[i];
}
}
return flag;
}
int Board::noOfFlippedDiscs(Cell_States state, int curr_y, int curr_x, int dir_y, int dir_x, int flippedDiscs = 0){
if(!isValidIndex(curr_y, curr_x)) return 0;
if(cells[curr_y][curr_x].state == EMPTY) return 0;
if(cells[curr_y][curr_x].state == state) return flippedDiscs;
else return noOfFlippedDiscs(state, curr_y + dir_y, curr_x + dir_x, dir_y, dir_x, flippedDiscs + 1);
}
void Board::flipDiscs(Cell_States state, int curr_y, int curr_x, int dir_y, int dir_x){
if(cells[curr_y][curr_x].state == state) return;
cells[curr_y][curr_x].flip();
if(state == WHITE){
++noOfWhiteDisks;
--noOfBlackDisks;
}
else if(state == BLACK){
++noOfBlackDisks;
--noOfWhiteDisks;
}
flipDiscs(state, curr_y + dir_y, curr_x + dir_x, dir_y, dir_x);
}
bool Board::isGameOver(){
for(int j = 0; j < l; ++j){
for(int i = 0; i < b; ++i){
if(isValidMove(j, i)) return false;
}
}
return true;
}
void Board::declareWinner(){
std::cout << "\n";
std::string winner_text;
if(noOfBlackDisks > noOfWhiteDisks) winner_text = " Player One is the Winner!";
else if(noOfWhiteDisks > noOfBlackDisks) winner_text = " Player Two is the Winner!";
else winner_text = " It is a Tie!";
std::cout << GREEN << winner_text << "\n" << RESET;
}
bool Board::isNeighbouringDisk(int y_pos, int x_pos){
for(int j = -1; j <= 1; ++j){
for(int i = -1; i <= 1; ++i){
if (j == 0 && i == 0) continue;
if (!isValidIndex(y_pos+j, x_pos+i)) continue;
if (cells[y_pos+j][x_pos+i].state != EMPTY) return true;
}
}
return false;
}
inline int Board::setDirY(int k){
switch(k){
case 0:
case 1:
case 2:
return -1;
case 3:
case 5:
return 0;
case 6:
case 7:
case 8:
return +1;
}
return 0;
}
inline int Board::setDirX(int k){
switch(k){
case 0:
case 3:
case 6:
return -1;
case 1:
case 7:
return 0;
case 2:
case 5:
case 8:
return +1;
}
return 0;
}
inline bool Board::isValidIndex(int j, int i){
if(j < 0 || i < 0) return false;
if(j >= l || i >= b) return false;
return true;
}
inline bool Board::isAtCurrPos(int j, int i){
return i == x && j == y;
}