-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
583 lines (477 loc) · 17.2 KB
/
main.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <stdio.h>
// Katherine Zhang 1006837292
// This program sets up the board for Reversi game
/*
* File: project_reversi_skeleton.c
* Author: APS105H1 Teaching Team
* Modified by: * Katherine Zhang *
* Date: April 9 2021
*/
#include "project_reversi_skeleton.h" // DO NOT modify this line
// Note: You may want to add more function declarations here
// ======================== Function declaration ===========================
void initializeBoard(char board[][26], int n);
void printBoard(char board[][26], int n) ;
void boardConfig(char board[][26], char colour, char row, char col, int n);
bool positionInBounds(int n, int row, int col);
bool checkLegalInDirection(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol);
int findAvailableMoves (char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol);
bool availableMoves(char board[][26], int n, char colour);
bool validMove(char board[][26], int n, char row, char col, char colour);
void checkFilpTiles (char board[][26], int n, char row, char col, char colour, int deltaRow, int deltaCol);
void flipTiles(char board[][26], int n, char row, char col, char colour);
// ======================== Function implementation =========================
/* Function: initializeBoard
*
* Print U for unoccupied and place intial pieces on the board
*/
void initializeBoard(char board[][26], int n)
{
int i, j;
// first print U for every position
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
board[i][j] = 'U';
}
// place B and W on middle diagonally
board[(n / 2) - 1][(n / 2) - 1] = 'W';
board[n / 2][n / 2] = 'W';
board[(n / 2) - 1][n / 2] = 'B';
board[n / 2][(n / 2) - 1] = 'B';
}
/*
* Function: printBoard
*
* Print the whole board to the terminal window.
* Takes board dimension (boardDim)
*/
void printBoard(char board[][26], int n)
{
//char occupied;
char colLabel = 'a';
char rowLabel = 'a';
int i, j, k;
// print the column labels according to board size
printf(" ");
for (i = 0; i < n; i++)
{
printf("%c", colLabel);
colLabel++;
}
// print row labels and board according to board size and letter
for (j = 0; j < n; j++)
{
printf("\n%c ", rowLabel);
rowLabel++;
for (k = 0; k < n; k++)
printf("%c", board[j][k]);
}
printf("\n");
}
/*
* Function: positionInBounds
* --------------------
* Checks whether the specified (row, col) lies within the board dimensions.
*/
bool positionInBounds(int n, int row, int col)
{
bool valid;
// if position is out of bounds, return false
if (row < 0 || row > n || col < 0 || col > n)
valid = false;
else
valid = true;
return valid;
}
/*
* Function: checkLegalInDirection
* --------------------
* Checks whether (row, col) is a legal position for a tile of colour by “looking” in the direction
* specified by deltaRow and deltaCol.
*/
bool checkLegalInDirection(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol)
{
char opponent;
int i, j;
// determine which colour is the opponent
if (colour == 'W')
opponent = 'B';
else
opponent = 'W';
// starting at position, go through tile in that direction until over boundaries
for (i = row, j = col; 0 <= i && i < n && 0 <= j && j < n; i += deltaRow, j += deltaCol)
{
// if position adjacent is same opposite, continue checking
if (board[row + deltaRow][col + deltaCol] == opponent)
{
if ((i + deltaRow) < 0 || (j + deltaCol) < 0)
return false;
// if next position is the same colour, position is valid
else if (board[i + deltaRow][j + deltaCol] == colour)
return true;
// if no colour
else if (board[i + deltaRow][j + deltaCol] == 'U')
return false;
// if still opponent colour, go thorugh loop again
}
}
return false;
}
/*
* Function: findAvailableMoves
* --------------------
* find position where a piece can be placed
*/
int findAvailableMoves (char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol)
{
char opponent;
int i, j;
// determine which colour is the opponent
if (colour == 'W')
opponent = 'B';
else
opponent = 'W';
// starting at position, go through tile in that direction until over boundaries
for (i = row, j = col; 0 <= i && i < n && 0 <= j && j < n; i += deltaRow, j += deltaCol)
{
// if position adjacent is oppponent colour, continue checking
if (board[row + deltaRow][col + deltaCol] == opponent)
{
// if next position is the same colour, position is valid
if (board[i + deltaRow][j + deltaCol] == colour && (0 <= (i + deltaRow) && (i + deltaRow) < n && 0 <= (j + deltaCol) && (j + deltaCol) < n))
{
// convert index to row and col
char rowLetter = row + 97;
char colLetter = col + 97;
printf("%c%c\n", rowLetter, colLetter);
return 0;
}
// if no colour
else if (board[i + deltaRow][j + deltaCol] == 'U')
return 0;
// if still opponent colour, go thorugh loop again
}
}
return 0;
}
/*
* Function: availableMoves
* --------------------
* Finds available moves for a colour and prints them
*/
bool availableMoves(char board[][26], int n, char colour)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
//convert i and j index to letter
char iLetter = i + 97;
char jLetter = j + 97;
if (validMove(board, n, iLetter, jLetter, colour))
return true;
}
}
return false;
}
/*
* Function: validMove
* --------------------
* determine if move is valid by calling check funcion
*/
bool validMove(char board[][26], int n, char row, char col, char colour)
{
// convert char row and col to int index
int rowIndex = row - 97;
int colIndex = col - 97;
if (board[rowIndex][colIndex] == 'U')
{
// check if move is valid by calling function for 8 directions
if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, 1, -1))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, 1, 0))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, 1, 1))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, 0, -1))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, 0, 1))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, -1, -1))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, -1, 0))
return true;
else if (checkLegalInDirection(board, n, rowIndex, colIndex, colour, -1, 1))
return true;
else
return false;
}
else
return false;
}
/*
* Function: checkFlipTiles
* --------------------
* function will check if it can flip the tile in direction
*/
void checkFilpTiles (char board[][26], int n, char row, char col, char colour, int deltaRow, int deltaCol)
{
// convert row and col to index
int rowIndex = row - 97;
int colIndex = col - 97;
char opponent;
int i;
// determines opponent colour
if (colour == 'B')
opponent = 'W';
else
opponent = 'B';
// check if piece is in position
if(board[rowIndex][colIndex] == 'U')
{
// check if the move is valid
if(checkLegalInDirection(board, n, rowIndex, colIndex, colour, deltaRow, deltaCol) == true)
{
// flips every opponent colour until piece of the same colour
for(i = 1; ; i++)
{
if(board[rowIndex + deltaRow * i][colIndex + deltaCol * i] == opponent)
board[rowIndex + deltaRow * i][colIndex + deltaCol * i] = colour;
else if (board[rowIndex + deltaRow * i][colIndex + deltaCol * i] == colour)
// cannot flip anymore
return;
}
}
else
return;
}
return;
}
void flipTiles(char board[][26], int n, char row, char col, char colour)
{
// check if it can flip tiles then flips
checkFilpTiles(board, n, row, col, colour, 1, -1);
checkFilpTiles(board, n, row, col, colour, 1, 0);
checkFilpTiles(board, n, row, col, colour, 1, 1);
checkFilpTiles(board, n, row, col, colour, 0, -1);
checkFilpTiles(board, n, row, col, colour, 0, 1);
checkFilpTiles(board, n, row, col, colour, -1, -1);
checkFilpTiles(board, n, row, col, colour, -1, 0);
checkFilpTiles(board, n, row, col, colour, -1, 1);
}
int calculateScore(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol)
{
int score = 0;
char opponent;
int k;
// determine which colour is the opponent
if (colour == 'W')
opponent = 'B';
else
opponent = 'W';
// if there is no tile in place and move is legal
if (board[row][col] == 'U' && checkLegalInDirection(board, n, row, col, colour, deltaRow, deltaCol))
{
for (k = 1; ; k++)
{
if (board[row + deltaRow * k][col + deltaCol * k] == opponent)
score++;
else if (board[row + deltaRow * k][col + deltaCol * k] == colour)
return score;
}
}
return 0;
}
int countScore(char board[][26], int n, int row, int col, char colour)
{
int score = 0;
score = calculateScore(board, n, row, col, colour, -1, -1) +
calculateScore(board, n, row, col, colour, -1, 0) +
calculateScore(board, n, row, col, colour, -1, 1) +
calculateScore(board, n, row, col, colour, 0, -1) +
calculateScore(board, n, row, col, colour, 0, 1) +
calculateScore(board, n, row, col, colour, 1, -1) +
calculateScore(board, n, row, col, colour, 1, 0) +
calculateScore(board, n, row, col, colour, 1, 1) ;
return score;
}
/*
* Function: makeMove ////// FOR LAB 8 ONLY /////
* --------------------
* The player "turn" makes a move at (row, col). and
* Note: This function MUST NOT change board, only row and col can be changed to reflect the move.
* function should calculate a best move for that color
* location of the best move should return using the two pointers (row and col) to two integer values
*/
int makeMove(const char board[26][26], int n, char turn, int *row, int *col)
{
// declaring local variables
int i, j;
int score = 0;
int maxScore = 0;
// calculate score for each tile if it is a valid move
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// convert index to row and col
char iLetter = i + 97;
char jLetter = j + 97;
if (board[i][j] == 'U' && validMove(board, n, iLetter, jLetter, turn))
{
score = countScore(board, n, i, j, turn);
// weighted score based on position the tile can be placed
// 4 corners
if ((i == 0 || i == n) && (j == 0 || j == n))
score = score * 10;
// sidelines
else if(i == 0 || i == 1 || j == 0 || j == 1)
score = score * 5;
// sides
else if(i == 1 || i == n - 1 || j == 1 || j == n - 1)
score = score * 3;
else
score = score * 1;
// set new max score and the row and col where the tile is
if(score > maxScore)
{
maxScore = score;
*row = i;
*col = j;
}
}
}
}
// convert index to row and col
char rowLetterPoint = *row + 97;
char colLetterPoint = *col + 97;
//printf("Computer places %c at %c%c.\n", turn, rowLetterPoint, colLetterPoint);
return 0;
}
char winner(char board[][26], int n, char playerColour, char compColour)
{
int playerTiles = 0;
int compTiles = 0;
char winner;
int i, j;
// counts number of tiles for player and comp
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n ; j++)
{
if (board[i][j] == playerColour)
playerTiles++;
else if (board[i][j] == compColour)
compTiles++;
}
}
// compare number of tiles
if (playerTiles > compTiles)
winner = playerColour;
else if (compTiles > playerTiles)
winner = compColour;
else if (compTiles == playerTiles)
winner = 'U';
return winner;
}
#ifndef COMPETE_MODE // DO NOT DELETE THIS LINE
//////////////////////////// MAIN FUNCTION ///////////////////////////////
int main(void)
{
// Define variables
int boardDim;
char board[26][26];
char compColour;
char playerColour;
int row;
int col;
char userRow;
char userCol;
int rowIndex;
int colIndex;
// Ask for board dimension
printf("Enter the board dimension: ");
scanf("%d", &boardDim);
// Ask user if computer is black or white
printf("Computer plays (B/W) : ");
scanf(" %c", &compColour);
// intialize and print board
initializeBoard(board, boardDim);
printBoard(board, boardDim);
// assigning the turns, if comp = B comp goes first
if (compColour == 'W')
playerColour = 'B';
else
{
playerColour = 'W';
makeMove(board, boardDim, compColour, &row, &col);
// convert index to row and col
char rowLetter = row + 97;
char colLetter = col + 97;
// place tile in position and flip tiles in direction then print
flipTiles(board, boardDim, rowLetter, colLetter, compColour);
board[row][col] = compColour;
printBoard(board, boardDim);
}
// while player or comp has available moves, take turns
while (availableMoves(board, boardDim, playerColour) || availableMoves(board, boardDim, compColour))
{
// if player has available move, player goes
if (availableMoves(board, boardDim, playerColour))
{
printf("Enter move for colour %c (RowCol): ", playerColour);
scanf(" %c%c", &userRow, &userCol);
//printf("\nplayer enters: %c %c\n", userRow, userCol);
// convert char row and col to int index
rowIndex = userRow - 97;
colIndex = userCol - 97;
// check if move is valid
if (validMove(board, boardDim, userRow, userCol, playerColour) && positionInBounds(boardDim, rowIndex, colIndex))
{
// flip the tiles
flipTiles(board, boardDim, userRow, userCol, playerColour);
// position piece in row and col
board[rowIndex][colIndex] = playerColour;
// print board
printBoard(board, boardDim);
}
// if not, comp wins
else
{
printf("Invalid move.\n");
printf("%c player wins.", compColour);
return 0;
}
}
//if comp has available moves but player doesnt, change turn after message
else if (availableMoves(board, boardDim, compColour))
{
printf("%c player has no valid move.\n", playerColour);
}
// if comp has available moves and player finished turn,
if (availableMoves(board, boardDim, compColour))
{
makeMove(board, boardDim, compColour, &row, &col);
// convert index to row and col
char rowLetter = row + 97;
char colLetter = col + 97;
// place tile in position and flip tiles in direction then print
flipTiles(board, boardDim, rowLetter, colLetter, compColour);
board[row][col] = compColour;
printBoard(board, boardDim);
}
// if comp has no available move
else if (availableMoves(board, boardDim, playerColour))
printf("%c player has no valid move.\n", compColour);
}
// decide winner
char winnerColour = winner(board, boardDim, playerColour, compColour);
if (winnerColour == 'B' || winnerColour == 'W')
printf("%c player wins.\n", winnerColour);
else
printf("Draw!\n");
return 0;
}
#endif // DO NOT DELETE THIS LINE