-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_four_board.cpp
294 lines (210 loc) · 6.34 KB
/
connect_four_board.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
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
#include <iostream>
#include <cctype>
#include "general_board.h"
#include "connect_four_board.h"
using namespace std;
/* Markers to fill the board */
const char ConnectFourBoard :: player_markers[] = {'.', 'X', 'O'};
/* Constructor */
ConnectFourBoard :: ConnectFourBoard() {
no_players = 2;
player_turn = 1; //Player 1 starts
num_rows = 6;
num_cols = 7; //Standard 6x7 board
game_over = false;
winner = 0; //0 to indicate nobody has won yet
last_played_row = -1;
last_played_col = -1;
// Initialize the board to be empty
for (int row = 0; row < num_rows; row++){
for (int col = 0; col < num_cols; col++){
board[row][col] = '.';
}
}
}
/* END OF FUNCTION DEFINITION */
/* Function to create a copy of the current board */
ConnectFourBoard ConnectFourBoard :: cloneBoard(ConnectFourBoard* a_board) {
for (int row = 0; row < num_rows; row++){
for (int col = 0; col < num_cols; col++){
board[row][col] = a_board->board[row][col];
}
}
player_turn = a_board->player_turn;
game_over = a_board->game_over;
winner = a_board->winner;
last_played_row = a_board->last_played_row;
last_played_col = a_board->last_played_col;
}
/* Function to get the legal avaliable moves for this state of the board */
vector<Move> ConnectFourBoard :: getAvailableMoves(player a_player){
checkPlayer();
vector<Move> available_moves;
if (game_over) {
return available_moves;
}
available_moves.reserve(num_cols);
for (int col = 0; col < num_cols; ++col) {
if (board[0][col] == player_markers[0]) {
available_moves.push_back(col);
}
}
return available_moves;
}
/* END OF FUNCTION DEFINITION */
player ConnectFourBoard :: getWinner(){
return winner;
}
/* END OF FUNCTION DEFINITION */
bool ConnectFourBoard :: isGameOver(){
return game_over;
}
/* END OF FUNCTION DEFINITION */
bool ConnectFourBoard :: checkEndgame(){
// Check - was there but possibly remove.
// if (last_col < 0) {
//return player_markers[0];
//}
// We only need to check around the last piece played.
char piece = board[last_played_row][last_played_col];
// X X X X
int left = 0, right = 0;
for (int col = last_played_col - 1; col >= 0 && board[last_played_row][col] == piece; --col) left++;
for (int col = last_played_col + 1; col < num_cols && board[last_played_row][col] == piece; ++col) right++;
if (left + 1 + right >= 4) {
winner = player_turn;
game_over = true;
return true;
}
// X
// X
// X
// X
int up = 0, down = 0;
for (int row = last_played_row - 1; row >= 0 && board[row][last_played_col] == piece; --row) up++;
for (int row = last_played_row + 1; row < num_rows && board[row][last_played_col] == piece; ++row) down++;
if (up + 1 + down >= 4) {
winner = player_turn;
game_over = true;
return true;
}
// X
// X
// X
// X
up = 0;
down = 0;
for (int row = last_played_row - 1, col = last_played_col - 1; row >= 0 && col >= 0 && board[row][col] == piece; --row, --col) up++;
for (int row = last_played_row + 1, col = last_played_col + 1; row < num_rows && col < num_cols && board[row][col] == piece; ++row, ++col) down++;
if (up + 1 + down >= 4) {
winner = player_turn;
game_over = true;
return true;
}
// X
// X
// X
// X
up = 0;
down = 0;
for (int row = last_played_row + 1, col = last_played_col - 1; row < num_rows && col >= 0 && board[row][col] == piece; ++row, --col) up++;
for (int row = last_played_row - 1, col = last_played_col + 1; row >= 0 && col < num_cols && board[row][col] == piece; --row, ++col) down++;
if (up + 1 + down >= 4) {
winner = player_turn;
game_over = true;
return true;
}
/* Check for draws */
vector<Move> remaining_moves = getAvailableMoves(0);
if (remaining_moves.empty()){
winner = 9; // Use 9 to indicate draw
game_over = true;
return true;
}
return false;
}
/* END OF FUNCTION DEFINITION */
/* Function to submit a move to the board. Also checks the move is valid.
Output: returns true if move successfully made. */
bool ConnectFourBoard :: doMove(Move a_move) {
if (!checkInput(a_move)) {
return false;
}
if (board[0][a_move] != player_markers[0]) {
//cerr << "column already full" << endl;
return false;
}
if (!checkPlayer()){
cerr << "something went wrong with player numbers"<<endl;
return false;
}
if (isGameOver()){
cerr << "Game has already ended";
return false;
}
//find available spot and set it
int row = num_rows - 1;
while (board[row][a_move] != player_markers[0]) row--;
board[row][a_move] = player_markers[player_turn];
last_played_col = a_move;
last_played_row = row;
/* Check state of game after the move (i.e. if it ended) */
checkEndgame();
player_turn = 3 - player_turn;
return true;
}
/* END OF FUNCTION DEFINITION */
/* Helper function to print te board to screen */
void ConnectFourBoard :: printBoard(){
cout << endl;
cout << " ";
//print header
for (int col = 0; col < num_cols; ++col) {
cout << col << ' ';
}
cout << endl;
//Print inner board
for (int row = 0; row < num_rows; ++row) {
cout << "|";
for (int col = 0; col < num_cols - 1; ++col) {
cout << board[row][col] << ' ';
}
cout << board[row][num_cols - 1] << "|" << endl;
}
//Print bottom part of frame
cout << "+";
for (int col = 0; col < num_cols - 1; ++col) {
cout << "--";
}
cout << "-+" << endl;
if (!isGameOver())
cout << player_markers[player_turn] << " (Player " << player_turn << ")" << " to move " << endl << endl;
}
/* END OF FUNCTION DEFINITION */
/* maybe unused?? */
player ConnectFourBoard :: getPlayerTurn(){
return player_turn;
}
/* END OF FUNCTION DEFINITION */
/* Helper function to check input is clean. To be improved for letters. modifying cin possibly */
bool ConnectFourBoard :: checkInput(const Move& a_move) {
// Check move out of bounds
if (a_move < 0 || a_move > 6) {
cerr << "Move out of bounds." << endl;
return false;
}
// Check letter
if (isalpha(a_move)) {
cerr << "need numeric input" << endl;
return false;
}
return true;
}
/* END OF FUNCTION DEFINITION */
/* Quick check to see if it's still player 1 or 2 to move */
bool ConnectFourBoard :: checkPlayer() {
if (player_turn == 1 || player_turn == 2)
return true;
return false;
}
/* END OF FUNCTION DEFINITION */