-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.h
292 lines (257 loc) · 8.35 KB
/
Board.h
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
using namespace sf;
using namespace std;
#include <vector>
class Board
{
struct Chip
{
enum State : int
{
BOARD = 0,
RED = 1,
YELLOW = -1,
BACKGROUND = 2
};
Vector2f pos;
State state;
Chip()
{
}
Chip(Vector2f p)
{
pos = p;
}
};
int startCol = 4;
int endCol = 12;
int startRow = 3;
int endRow = 9;
vector<vector<Chip>> board;
public:
Board()
{
}
void copy(Board rhs)
{
this->board = rhs.board;
}
void setBoard()
{
// set up background
for (int i = 0 ; i < 13 ; i++)
{
vector<Chip> tempVec;
for (int j = 0; j < 16; j++)
{
float x = (float) (j * 64);
float y = (float) (i * 64);
Chip temp = Chip(Vector2f(x, y));
temp.state = Chip::BACKGROUND;
tempVec.push_back(temp);
}
board.push_back(tempVec);
}
// set up game board
// Board is 6x7 but there are 2 extra cols and 1 extra row for border
for (int i = startRow ; i <= endRow ; i++)
for (int j = endCol ; j >= startCol ; j--)
{
if (i == 9 || j == 4 || j == 12) board[i][j].state = Chip::BOARD;
}
}
void resetBoard()
{
for (int i = startRow ; i <= endRow ; i++)
for (int j = startCol + 1; j <= endCol - 1; j++)
board[i][j].state = Chip::BACKGROUND;
}
void drawBoard(RenderWindow &window)
{
RectangleShape tile(Vector2f(64,64));
tile.setOutlineColor(Color::Black);
tile.setOutlineThickness(4);
for (int i = 0 ; i < 12 ; i++)
for (int j = 0 ; j < 16 ; j++)
{
tile.setPosition(board[i][j].pos);
if (board[i][j].state == Chip::BOARD)
tile.setFillColor(Color::Blue);
else if (board[i][j].state == Chip::RED)
tile.setFillColor(Color::Red);
else if (board[i][j].state == Chip::YELLOW)
tile.setFillColor(Color::Yellow);
else if (board[i][j].state == Chip::BACKGROUND)
tile.setFillColor(Color::White);
window.draw(tile);
}
}
int selectCol(int mouseX)
{
// we have to check between cols 5 and 11 inclusive
for (int i = startCol ; i <= endCol-1 ; i++)
if (mouseX <= (i+1) * 64 && mouseX >= i * 64)
return i;
}
bool colFull(int col)
{
return (board[startRow][col].state != Chip::BACKGROUND);
}
vector<pair<int,int>> validMoves()
{
vector<pair<int,int>> moves;
for (int i = startCol+1 ; i <= endCol-1 ; i++)
if (!colFull(i))
moves.emplace_back(i, 0);
return moves;
}
void makeMove(int player, int col)
{
if (this->isTerminalState())
return;
if (col < startCol + 1 || col > endCol + 1)
return;
for (int i = endRow - 1; i >= startRow ; i--)
if (board[i][col].state == Chip::BACKGROUND)
{
board[i][col].state = (Chip::State) player;
break;
}
}
bool checkWin(int player)
{
for (int i = endRow - 1 ; i >= startRow ; i--)
for (int j = startCol + 1 ; j <= endCol + 1 ; j++)
{
// check row
if (board[i][j].state == player)
if (board[i-1][j].state == player)
if (board[i-2][j].state == player)
if (board[i-3][j].state == player)
return true;
// check column
if (board[i][j].state == player)
if (board[i][j+1].state == player)
if (board[i][j+2].state == player)
if (board[i][j+3].state == player)
return true;
// check right diagonal
if (board[i][j].state == player)
if (board[i-1][j+1].state == player)
if (board[i-2][j+2].state == player)
if (board[i-3][j+3].state == player)
return true;
// check left diagonal
if (board[i][j].state == player)
if (board[i-1][j-1].state == player)
if (board[i-2][j-2].state == player)
if (board[i-3][j-3].state == player)
return true;
}
return false;
}
bool isTerminalState()
{
return (checkWin(-1) || checkWin(1) || validMoves().empty());
}
int score(vector<Chip> chips, int player)
{
int score = 0;
int pChips = 0;
int oChips = 0;
int empty = 0;
for (auto chip : chips)
{
if (chip.state == player)
pChips++;
else if (chip.state == -player)
oChips++;
else
empty++;
}
if (pChips == 4)
score += 1001;
else if (pChips == 3 && empty == 1)
score += 100;
else if (pChips == 2 && empty == 2)
score += 10;
else if (oChips == 2 && empty == 2)
score -= 20;
else if (oChips == 3 && empty == 1)
score -= 1000;
return score;
}
int scoreBoard(Board b, int player)
{
// score center column
int sum = 0;
for (int i = endRow - 1 ; i >= startRow ; i--)
if (b.board[i][8].state == player)
sum += 3;
// score horizontal
for (int i = endRow - 1 ; i >= startRow ; i--)
for (int j = startCol + 1 ; j <= endCol - 1 ; j++)
{
vector<Chip> chips;
for (int k = 0 ; k < 4 ; k++)
chips.push_back(b.board[i][j+k]);
sum += score(chips, player);
}
// score vertical
for (int i = endRow - 1 ; i >= startRow ; i--)
for (int j = startCol + 1 ; j <= endCol - 1 ; j++)
{
vector<Chip> chips;
for (int k = 0 ; k < 4 ; k++)
chips.push_back(b.board[i-k][j]);
sum += score(chips, player);
}
// score diagonal
for (int i = endRow - 1 ; i >= startRow ; i--)
for (int j = startCol + 1 ; j <= endCol - 1 ; j++)
{
vector<Chip> chips;
for (int k = 0 ; k < 4 ; k++)
chips.push_back(b.board[i-k][j+k]);
sum += score(chips, player);
}
// score diagonal
for (int i = endRow - 1 ; i >= startRow ; i--)
for (int j = startCol + 1 ; j <= endCol - 1 ; j++)
{
vector<Chip> chips;
for (int k = 0 ; k < 4 ; k++)
chips.push_back(b.board[i-k][j-k]);
sum += score(chips, player);
}
return sum;
}
pair<int,int> miniMax(int player, Board _board, int depth, int alpha, int beta)
{
Board temp = Board();
pair<int,int> bestMove = make_pair(-1,NULL);
for (auto move : validMoves())
{
temp.copy(_board);
temp.makeMove(player, move.first);
if (temp.isTerminalState() || depth == 0)
move.second = scoreBoard(temp, -1);
else
move.second = miniMax(-player, temp, depth - 1, alpha, beta).second;
if ((bestMove.first == -1 || bestMove.second < move.second) && player == -1)
{
bestMove = make_pair(move.first, move.second);
alpha = max(alpha, bestMove.second);
if (alpha >= beta)
break;
}
else if ((bestMove.first == -1 || bestMove.second > move.second) && player == 1)
{
bestMove = make_pair(move.first, move.second);
beta = min(beta, bestMove.second);
if (alpha >= beta)
break;
}
}
return bestMove;
}
};