forked from Pitclair/term-project-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategyCompleted.c
More file actions
205 lines (178 loc) · 6.72 KB
/
strategyCompleted.c
File metadata and controls
205 lines (178 loc) · 6.72 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
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
#include<stdio.h>
#include<stdlib.h>
#define ROW 8
#define COLUMN 8
#define BLACK 1
#define WHITE 2
#define MOVE -1
#define EMPTY 0
//function prototypes below:
//finds and prints the best move according to the implemented strategy
void find_and_print_the_best_move(int const possible_moves_arr[][4], const int possible_moves);
//function to count how many disks a move can flip
int flippedDisk(int const square[ROW][COLUMN], int i, int j, int const player, int const opponent, int x, int y);
//function to set a fixed worth for possible moves
void set_square_worth(const int square[ROW][COLUMN], int const player, int const opponent);
//function to find the possible moves for player
void possible_moves(const int opponent, const int player, int i, int j ,int x_move, int y_move,int square[ROW][COLUMN]);
int main( int argc, char * argv[] )
{
int square[8][8];
int i,j;
int opponent, player=argv[9][0]-'0';
//sets the color of player and opponent
if(player==BLACK)
opponent=WHITE;
else if (player==WHITE)
opponent=BLACK;
//constructs the board
for(i=0; i<ROW; i++)
{
for(j=0; j<COLUMN; j++)
square[i][j]=argv[i+1][j]-'0';
}
//sends the board to be checked for possible moves
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(square[i][j]==player)
{
if (square[i][j+1]==opponent) possible_moves( opponent, player , i, j+1 , 1 , 0 , square );
if (square[i][j-1]==opponent) possible_moves( opponent, player , i, j-1 , -1 , 0 , square );
if (square[i-1][j]==opponent) possible_moves( opponent, player , i-1, j, 0 , -1 , square );
if (square[i+1][j]==opponent) possible_moves( opponent, player , i+1, j, 0 , +1 , square );
if (square[i-1][j+1]==opponent) possible_moves( opponent, player , i-1, j+1, 1 , -1 , square );
if (square[i-1][j-1]==opponent) possible_moves( opponent, player , i-1, j-1, -1 , -1 , square );
if (square[i+1][j+1]==opponent) possible_moves( opponent, player , i+1 , j+1 , 1 , 1 , square );
if (square[i+1][j-1]==opponent) possible_moves( opponent, player , i+1 , j-1 , -1 , 1 , square );
}
}
}
//implementing the strategy
set_square_worth(square, player, opponent);
return 0;
}
void possible_moves(const int opponent, const int player, int i, int j ,int x_move, int y_move,int square[ROW][COLUMN])
{
while (j<ROW && i<COLUMN && i>=0 && j>=0)
{
if (square[i][j]==player)
return;
if (square[i][j]==EMPTY || square[i][j]==MOVE)
{
square[i][j]=MOVE;
return;
}
j=j + x_move;
i=i + y_move;
}
return;
}
void set_square_worth(const int square[ROW][COLUMN], int const player, int const opponent)
{
int i, j;
//fixed worth of each square
int const squareWorth[ROW][COLUMN]=
{{99, -8, 8, 6, 6, 8, -8, 99},
{-8, -24, -4, -3, -3, -4, -24, -8},
{8, -4, 7, 4, 4, 7, -4, 8},
{6, -3, 4, 0, 0, 4, -3, 6},
{6, -3, 4, 0, 0, 4, -3, 6},
{8, -4, 7, 4, 4, 7, -4, 8},
{-8, -24, -4, -3, -3, -4, -24, -8},
{99, -8, 8, 6, 6, 8, -8, 99}};
int possible_moves=0;
for(i=0; i<ROW; i++)
{
for(j=0; j<COLUMN; j++)
{
if(square[i][j]==MOVE)
possible_moves++;
}
}
//array to keep track of the position, worth and the number of disk flipped for each move
int possible_moves_arr[possible_moves][4];
int k=0;
for(i=0; i<ROW; i++)
{
for(j=0; j<COLUMN; j++)
{
if(square[i][j]==MOVE)
{
possible_moves_arr[k][0]=i;
possible_moves_arr[k][1]=j;
possible_moves_arr[k][2]=squareWorth[i][j];
possible_moves_arr[k][3]=0;
k++;
}
}
}
//according to the implemented strategy, the move with the greatest worth is the best move
//if two moves have similar worth, the one that flips less opponent disks is the best one.
int max_worth=possible_moves_arr[0][2];
int x=possible_moves_arr[0][1], y=possible_moves_arr[0][0];
for(i=0; i<possible_moves; i++)
{
if(possible_moves_arr[i][2]>max_worth)
{
max_worth=possible_moves_arr[i][2];
x=possible_moves_arr[i][1];
y=possible_moves_arr[i][0];
}
}
//counting the flippded disks for moves with the greatest worth in 8 directions
for(i=0; i<possible_moves; i++ )
{
if(possible_moves_arr[i][2]==max_worth)
{
possible_moves_arr[i][3]=
flippedDisk(square, 0, 1, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, 0, -1, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, 1, 0, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, -1, 0, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, 1, 1, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, -1, 1, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, 1, -1, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0])+
flippedDisk(square, -1, -1, player, opponent, possible_moves_arr[i][1], possible_moves_arr[i][0]);
}
}
//calling the function to find the move with least flips
find_and_print_the_best_move(possible_moves_arr, possible_moves);
}
int flippedDisk(int const square[ROW][COLUMN], int i, int j, int const player, int const opponent, int x, int y)
{
int i_counter=i, j_counter=j;
int disk_counter=0;
x=x+j;
y=y+i;
while(y<ROW && y>=0 && x<COLUMN && x>=0)
{
if((square[y][x]!=opponent && square[y][x]!=player))
return 0;
if(square[y][x]==opponent)
disk_counter++;
if(square[y][x]==player)
return disk_counter;
x=x+j;
y=y+i;
}
return 0;
}
void find_and_print_the_best_move(int const possible_moves_arr[][4], const int possible_moves)
{
int i,x, y, min_flip=64;
for(i=0; i<possible_moves; i++)
{
if(possible_moves_arr[i][3]!=0)
{
if(possible_moves_arr[i][3]<min_flip)
{
min_flip=possible_moves_arr[i][3];
x=possible_moves_arr[i][1];
y=possible_moves_arr[i][0];
}
}
}
printf("%d %d", x, y);
}