-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOLLINSS-reversi3.c
More file actions
56 lines (46 loc) · 2.07 KB
/
COLLINSS-reversi3.c
File metadata and controls
56 lines (46 loc) · 2.07 KB
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
#include <stdio.h>
#include "reversi.h"
/*
* Best_Move()
*
* This function finds the best move that given <player> can make
*
* Returns the score of the best move that given player can make, i.e. the
* number of squares converted from the opponent's colour to the player's
* colour by the best move, plus one for the move itself.
*
* Returns 0 if no move can be made
*
* Fills <xposition> and <yposition> pointers to the best move in the grid
* If several moves give the same best score, uses the first move when
* scanning the board in "raster scan" order (see image processing assignment)
*/
int Best_Move(int player, int *xposition, int *yposition){
/* the function returns -1 because that indicates to the calling function
* that the given player can't move, which is the behaviour we want when
* the function isn't implemented. When you write the function, replace
* the return value with the score for the best move.
*/
int col, row, score, FinalScore=0, enemy;
/* "enemy" is set based on which player's turn it is */
if (player==PLAYER1)
enemy=PLAYER2;
if (player==PLAYER2)
enemy=PLAYER1;
/* each location on the board is looped through */
for (row=0; row<size; row++){
for (col=1; col<=size; col++){
/* location is checked if it's an empty space and if it's surrounded by "enemy" tiles */
if (board[col][row]==EMPTY_SPACE && (board[col-1][row-1]==enemy||(board[(col)-1][row]==enemy)||(board[(col)-1][(row)+1]==enemy)||board[col][(row)+1]==enemy||board[col][(row)-1]==enemy||board[(col)+1][(row)+1]==enemy||board[(col)+1][row]==enemy||board[(col)+1][(row)-1]==enemy)){
score=Get_Score(player, col, row);
/* always the move that will yield the highest score is updated to *xposition and *yposition */
if (score>FinalScore){
FinalScore=score;
*xposition= col;
*yposition= row;
}
}
}
}
return FinalScore; /* 0 is returned if there are no more valid moves on the board */
}