-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversi.c
281 lines (237 loc) · 7.43 KB
/
reversi.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//
// Author: Ava Jakob
void printBoard(char board[][26], int n) {
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
printf(" ");
//Print the letters as a header
for(int i = 0; i<n ; i++){
printf("%c", alphabet[i]);
}
printf("\n");
//Print current board configuration
for(int row = 0; row<n ; row++){
printf("%c", alphabet[row]);
printf(" ");
for(int col = 0; col<n; col++){
printf("%c", board[row][col]);
}
printf("\n");
}
}
char oppositecolour(char colour){
if(colour=='W'){
return 'B';
}else if(colour=='B'){
return 'W';
}
}
bool positionInBounds(int n, int row, int col) {
//Check if row and column are valid
if(row >= 0 && row < n && col >= 0 && col < n){
return true;
}
return false;
}
bool checkLegalInDirection(char board[][26], int n, int row,
int col, char colour, int deltaRow, int deltaCol)
{
//Check if row and column are valid
if (!positionInBounds(n, row + deltaRow, col + deltaCol)){
return false;
}
//Check if colour is valid
if (board[row + deltaRow][col + deltaCol] != oppositecolour(colour)){
return false;
}
bool done = false;
bool returnValue = false;
row += deltaRow * 2;
col += deltaCol * 2;
//Check if move is legal in the given direction
while (!done) {
if (!positionInBounds(n, row, col)){
done = true;
} else if (board[row][col] == 'U'){
done = true;
} else if (board[row][col] == colour) {
done = true;
returnValue = true;
} else {
row += deltaRow;
col += deltaCol;
}
}
return returnValue;
}
int flipInDirection(char board[][26], int row, int col, char colour, int deltaRow, int deltaCol, bool flip)
{
int count = 0;
row += deltaRow;
col += deltaCol;
//Flip tiles until a piece of the same initial colour is reached
while (board[row][col] == oppositecolour(colour)) {
if (flip){
board[row][col] = colour;
}
count++;
row += deltaRow;
col += deltaCol;
}
//Return number of pieces flipped
return count;
}
int checkLegalAndFlip(char board[][26], int row, int col, char colour, int size, bool flip){
int score = 0;
//Check if board space is empty
if (board[row][col] != 'U'){
return score;
} else if (!positionInBounds(size, row, col)){
return score;
}
//Only flip associated pieces if requested
if (flip){
board[row][col] = colour;
}
//Flip all pieces that switch colours as a result of this move
if (checkLegalInDirection(board, size, row, col, colour, -1, 0)) //North
score += flipInDirection(board, row, col, colour, -1, 0, flip);
if (checkLegalInDirection(board, size, row, col, colour, -1, 1)) //NorthEast
score += flipInDirection(board, row, col, colour, -1, 1, flip);
if (checkLegalInDirection(board, size, row, col, colour, 0, 1)) //East
score += flipInDirection(board, row, col, colour, 0, 1, flip);
if (checkLegalInDirection(board, size, row, col, colour, 1, 1)) //SouthEast
score += flipInDirection(board, row, col, colour, 1, 1, flip);
if (checkLegalInDirection(board, size, row, col, colour, 1, 0)) //South
score += flipInDirection(board, row, col, colour, 1, 0, flip);
if (checkLegalInDirection(board, size, row, col, colour, 1, -1)) //SouthWest
score += flipInDirection(board, row, col, colour, 1, -1, flip);
if (checkLegalInDirection(board, size, row, col, colour, 0, -1)) //West
score += flipInDirection(board, row, col, colour, 0, -1, flip);
if (checkLegalInDirection(board, size, row, col, colour, -1, -1)) //NorthWest
score += flipInDirection(board, row, col, colour, -1, -1, flip);
//Return number of pieces flipped
return score;
}
bool computerMakesMove(char board[26][26], int size, char colour ){
//Check every possible move
int score = 0;
int highScore = 0;
int position[3]={0,0,0};
//Evaluate all possible moves and associate a score to then choose best move
for(int i = 0 ; i < size; i++){
for(int j = 0; j < size; j++){
score = checkLegalAndFlip(board, i, j, colour, size, false);
if(score > highScore){
highScore = score;
position[0] = i;
position[1] = j;
} else if (score == highScore) {
if(position[0]>i){
highScore = score;
position[0] = i;
position[1] = j;
}
}
score = 0;
}
}
//Ensure move is legal, then announce move and flip associated tiles
if(checkLegalAndFlip(board, position[0], position[1], colour, size, false )){
printf("Computer places %c at %c%c.\n", colour, position[0]+'a', position[1]+'a');
checkLegalAndFlip(board, position[0], position[1], colour, size, true );
printBoard(board, size);
return true;
}
return false;
}
bool playerMakesMove(char board[26][26], int size, char colour){
char config[3];
int row, col;
//Collect move data from the player
printf("Enter move for colour %c (RowCol): ", colour);
scanf("%s", config);
row = config[0];
col = config[1];
//Check if move is legal, then flip pieces accordingly
if(checkLegalAndFlip(board, row-'a', col-'a', colour, size, false)){
checkLegalAndFlip(board, row-'a', col-'a', colour, size, true);
printBoard(board, size);
return true;
}else{
printf("Invalid move.");
return false;
}
}
int countPieces(char board[26][26], int size, char colour){
int count;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(board[i][j]==colour){
count++;
}
}
}
return count;
}
int main(void) {
int size;
char comp;
//Collect board dimensions
printf("Enter the board dimension: ");
scanf("%d", &size);
//Board Setup
char board[size+1][26];
for(int row = 0; row < size; row++){
for(int col = 0; col < size; col++){
board[row][col] = 'U';
}
}
//Change centre pieces for game setup
board[size/2-1][size/2-1]='W';
board[size/2-1][size/2]='B';
board[size/2][size/2-1]='B';
board[size/2][size/2]='W';
//Check which colour Computer is
printf("Computer plays (B/W): ");
scanf(" %c", &comp);
printBoard(board, size);
bool otherMove = false;
int white, black;
char winner;
bool done = false;
//Make sure W player goes first, alternate until board is full
if(comp=='W'){
while(playerMakesMove(board, size, 'B') && !done)
if(computerMakesMove(board, size, 'W')){
continue;
}else if(!otherMove){
printf("%c player has no valid move.\n", comp);
otherMove = true;
computerMakesMove(board, size, 'W');
done = true;
}
}else if(comp=='B'){
while(!done && computerMakesMove(board, size, 'B')){
if(playerMakesMove(board, size, 'W')){
continue;
}else if (!done){
done = true;
printf("\n");
}
}
}
//Evaluate winning conditions
white = countPieces(board, size, 'W');
black = countPieces(board, size, 'B');
if(white>black){
winner = 'W';
}else{
winner = 'B';
}
//Announce Winner
printf("%c player wins.", winner);
return 0;
}