-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardState.cpp
247 lines (200 loc) · 7.19 KB
/
BoardState.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
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
#include "BoardState.h"
#include <iostream>
BoardState::BoardState() {
};
// Get the currentColour of the cell at (x,y)
int BoardState::getCell(int x, int y) {
return _board[x][y];
}
// Set the cell at (x,y) of the specified currentColour
void BoardState::setCell(int x, int y, int currentColour) {
_board[x][y] = currentColour;
}
// Check if the game is over
bool BoardState::isGameOver() {
bool gameOver = false;
if (getLegalMoves().empty()) {
currentColour = -currentColour; // change currentColour temporarily to check the legal moves
if (getLegalMoves().empty())
gameOver = true;
currentColour = -currentColour; // change currentColour back
}
return gameOver;
}
// Check if the move made is legal
bool BoardState::checkLegalMove(int x, int y) {
return
checkOnBoard(x, y) && (getCell(x, y) == 0) &&
((checkLeft(x, y) != -1) || (checkRight(x, y) != -1) ||
(checkUp(x, y) != -1) || (checkDown(x, y) != -1) ||
(checkUpLeft(x, y) != -1) || (checkUpRight(x, y) != -1) ||
(checkDownLeft(x, y) != -1) || (checkDownRight(x, y) != -1));
}
// Make a legal move and then switch the turn over to the other player
void BoardState::makeLegalMove(int x, int y) {
int a, b = 0;
// Squares to left
a = checkLeft(x, y);
if (a != -1)
for (int i = x; i > a; i--)
setCell(i, y, currentColour);
// Squares to right
a = checkRight(x, y);
if (a != -1)
for (int i = x; i < a; i++)
setCell(i, y, currentColour);
// Squares above
a = checkUp(x, y);
if (a != -1)
for (int j = y; j > a; j--)
setCell(x, j, currentColour);
// Squares below
a = checkDown(x, y);
if (a != -1)
for (int j = y; j < a; j++)
setCell(x, j, currentColour);
// Squares to above left
a = checkUpLeft(x, y);
if (a != -1)
for (int i = x, j = y; i > a; i--, j--)
setCell(i, j, currentColour);
// Squares above right
a = checkUpRight(x, y);
if (a != -1)
for (int i = x, j = y; i < a; i++, j--)
setCell(i, j, currentColour);
// Squares to below left
a = checkDownLeft(x, y);
if (a != -1)
for (int i = x, j = y; i > a; i--, j++)
setCell(i, j, currentColour);
// Squares below right
a = checkDownRight(x, y);
if (a != -1)
for (int i = x, j = y; i < a; i++, j++)
setCell(i, j, currentColour);
// Change player
currentColour = -currentColour;
}
// Check if the move is on the board
bool BoardState::checkOnBoard(int x, int y) {
return (x >= 0) && (x < 8) && (y >= 0) && (y < 8);
}
// Return the legal moves of the current player
std::vector<std::vector<int>> BoardState::getLegalMoves() {
std::vector<std::vector<int>> legalMoves;
// Iterate through every cell and check if the cell is legal
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (checkLegalMove(i, j)) {
// Create a vector representation of the move and push it to the move list
std::vector<int> move = {i, j};
legalMoves.push_back(move);
}
return legalMoves;
}
// Copy constructor
BoardState::BoardState(BoardState& boardState) {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
_board[i][j] = boardState._board[i][j];
currentColour = boardState.currentColour;
}
// Return largest value of i < x such that board[j][i] is opposite to currentColour for all
// j where (i < j < x) and board[i][y] is currentColour. Return -1 if none in range
int BoardState::checkLeft(int x, int y) {
int i = x - 1;
while ((i > 0) && (getCell(i, y) == -currentColour)) i--;
if ((i < x - 1) && getCell(i, y) == currentColour) return i;
else return -1;
}
// Return largest value of i > x such that board[j][i] is opposite to currentColour for all
// j where (i > j > x) and board[i][y] is currentColour. Return -1 if none in range
int BoardState::checkRight(int x, int y) {
int i = x + 1;
while ((i < 7) && (getCell(i, y) == -currentColour)) i++;
if ((i > x + 1) && getCell(i, y) == currentColour) return i;
else return -1;
}
// Return largest value of i < y such that board[x][j] is opposite to currentColour for all
// j where (i < j < y) and board[x][i] is currentColour. Return -1 if none in range
int BoardState::checkUp(int x, int y) {
int i = y - 1;
while ((i > 0) && (getCell(x, i) == -currentColour)) i--;
if ((i < y - 1) && getCell(x, i) == currentColour) return i;
else return -1;
}
// Return largest value of i > y such that board[x][j] is opposite to currentColour for all
// j where (i > j > y) and board[x][i] is currentColour. Return -1 if none in range
int BoardState::checkDown(int x, int y) {
int i = y + 1;
while ((i < 7) && (getCell(x, i) == -currentColour)) i++;
if ((i > y + 1) && getCell(x, i) == currentColour) return i;
else return -1;
}
// Return largest value of i < x such that board[x-j][y-j] is opposite to currentColour for all
// j where (i < j < endOfBoard) and board[i][y] is currentColour. Return -1 if none in range
int BoardState::checkUpLeft(int x, int y) {
int i = x - 1;
int j = y - 1;
while ((i > 0) && (j > 0) && (getCell(i, j) == -currentColour)) {
i--;
j--;
}
if ((i < x - 1) && getCell(i, j) == currentColour) return i;
else return -1;
}
// Return largest value of i > x such that board[j][i] is opposite to currentColour for all
// where j (i > j > x) and board[i][y] is currentColour. Return -1 if none in range
int BoardState::checkUpRight(int x, int y) {
int i = x + 1;
int j = y - 1;
while ((i < 7) && (j > 0) && (getCell(i, j) == -currentColour)) {
i++;
j--;
}
if ((i > x + 1) && getCell(i, j) == currentColour) return i;
else return -1;
}
// Return largest value of i < x such that board[x-j][y-j] is opposite to currentColour for all
// where j (i < j < endOfBoard) and board[i][y] is currentColour. Return -1 if none in range
int BoardState::checkDownLeft(int x, int y) {
int i = x - 1;
int j = y + 1;
while ((i > 0) && (j < 7) && (getCell(i, j) == -currentColour)) {
i--;
j++;
}
if ((i < x - 1) && getCell(i, j) == currentColour) return i;
else return -1;
}
// Return largest value of i > x such that board[j][i] is opposite to currentColour for all
// where j (i > j > x) and board[i][y] is currentColour. Return -1 if none in range
int BoardState::checkDownRight(int x, int y) {
int i = x + 1;
int j = y + 1;
while ((i < 7) && (j < 7) && (getCell(i, j) == -currentColour)) {
i++;
j++;
}
if ((i > x + 1) && getCell(i, j) == currentColour) return i;
else return -1;
}
int* BoardState::getScores() {
int scores[2]{}; // Black Count, White Count
int white = 0;
int black = 0;
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (getCell(i, j) == 1)
white++;
else if (getCell(i, j) == -1)
black++;
std::cout << black << std::endl;
std::cout << white << std::endl;
scores[0] = black;
scores[1] = white;
std::cout << scores[0] << std::endl;
std::cout << scores[1] << std::endl;
return scores;
}