-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_ai_mode_hard.h
288 lines (245 loc) · 6.52 KB
/
final_ai_mode_hard.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<stdbool.h>
#include "help_functions.h"
#define infini 1000000000
#define WIN 2000
#define LOSS -2000
int depth_hard=5;
long int g_l_BOARDS=0; //le nombre de board examiner durant la recherche minmax
int nexttoplay (int board[9][9], int i_player)
{
int p,p_moves=0,player_moves=0;
p = ennemi(i_player);
p_moves=can_move(p,board);
re_init(board);
if (p_moves)
{
return p;
}
//si l'adv has to skip his turn
player_moves=can_move(i_player,board);
re_init(board);
if (player_moves)
{
return i_player;
}
return 0; // if no one can move
}
void copyboard (int i_dest[9][9],int i_source[9][9]) {
int i,j;
for (i=0; i<9; i++)
{
for (j=0; j<9; j++)
{
i_dest[i][j] = i_source[i][j];
}
}
}
//we save all the possible moves in a pointer in case the ai is playing
int * legalmoves ( int board[9][9])
{
int j, i,k, * moves;
moves = (int *)malloc(65 * sizeof(int));
moves[0] = 0;
k = 0;
for (i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(board[i][j]==vide)
{
k++;
moves[k]=i*10+j;
}
}
}
moves[0]=k;
return moves;
}
int evaluation (int i_player, int board[9][9])
{ /* utility is measured */
int i,j, p, i_nbl=0, i_nb2 = 0; /* number of pieces */
p = ennemi(i_player);
for (i=1; i<9; i++)
{
for (j=1; j<9; j++)
{
if (board[i][j]==i_player)
{
i_nbl++;
}
if (board[i][j]==p)
{
i_nb2++;
}
}
}
return i_nbl-i_nb2;
}
int weightedevaluation (int player, int board[9][9])
{
int i,j, i_nb1=0, i_nb2=0, p;
const int weights[9][9]={{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0,120,-20, 20, 5, 5, 20,-20,120},
{0,-20,-40, -5, -5, -5, -5,-40,-20},
{0, 20, -5, 15, 3, 3, 15, -5, 20},
{0, 5, -5, 3, 3, 3, 3, -5, 5},
{0, 5, -5, 3, 3, 3, 3, -5, 5},
{0, 20, -5, 15, 3, 3, 15, -5, 20},
{0,-20,-40, -5, -5, -5, -5,-40,-20},
{0,120,-20, 20, 5, 5, 20,-20,120}};
p = ennemi(player);
for (i=1; i<9; i++)
{
for (j=1; j<9; j++)
{
if (board[i][j]==player)
{
i_nb1=i_nb1+weights[i][j];
}
if (board[i][j]==p)
{
i_nb2=i_nb2+weights[i][j];
}
}
}
return (i_nb1-i_nb2);
}
//board deja marquée avec les vides
int newboardmax[9][9];
int maxchoice (int player, int board[9][9], int ply,int weight)
{
int i, i_max, next_player, newscore, * moves;
int x=0,y=0;
//int minchoice (int pl, int table[9)[9], int depth, int _weight);
//elle sera défini après
if (ply == 0)
{
return weight;
}
can_move(player,board);
moves = legalmoves( board);
i_max = LOSS - 1;
for (i=1; i <= moves[0]; i++)
{
copyboard(newboardmax,board);
can_move(player, newboardmax);
g_l_BOARDS = g_l_BOARDS + 1;
x=moves[i]/10;
y=moves[i]-10*x;
flip(player,x,y, newboardmax);
re_init(newboardmax);
next_player = nexttoplay(newboardmax, player);
if (next_player == 0)
{
newscore = evaluation(player, newboardmax);
if (newscore > 0) newscore = WIN;
if (newscore < 0) newscore = LOSS;
}
else if (next_player == player)
{
newscore = maxchoice(player, newboardmax, ply-1, weight);
}
else if (next_player == ennemi(player))
{
newscore = minchoice(player, newboardmax, ply-1, weight);
}
if (newscore > i_max)
{
i_max = newscore;
}
}
free(moves);
return i_max;
}
int newboardmin[9][9];
int minchoice (int player, int board[9][9], int ply,int weight)
{
int i, i_min, next_player, newscore, * moves;
int x=0,y=0;
if (ply == 0)
{
return weight;
}
can_move(player,board);
moves = legalmoves( board);
i_min = WIN+1;
for (i=1; i <= moves[0]; i++)
{
copyboard(newboardmin,board);
can_move(player,newboardmin);
g_l_BOARDS = g_l_BOARDS + 1;
x=moves[i]/10;
y=moves[i]-x*10;
flip(ennemi(player),x,y, newboardmin);
re_init(newboardmin);
next_player = nexttoplay(newboardmin, ennemi(player));
if (next_player == 0) {
newscore = evaluation(player, newboardmin);
if (newscore > 0) newscore = WIN;
if (newscore < 0) newscore = LOSS;
}
else if (next_player == player)
{
newscore = maxchoice(player, newboardmin, ply-1, weight);
}
else if (next_player == ennemi(player))
{
newscore = minchoice(player, newboardmin, ply-1, weight);
}
if (newscore < i_min)
{
i_min = newscore;
}
}
free(moves);
return i_min;
}
// board avec les cases vides marquées
int newboard[9][9];
int minmax1 (int player, int board[9][9], int ply, int weight)
{
int i, i_max, next_player, newscore, bestmove, * moves;
int x,y;
can_move(player,board);
moves = legalmoves( board); /* get all legal moves for player */
i_max = LOSS - 1; /* any legal move will exceed this score */
for (i=1; i <= moves[0]; i++)
{
copyboard(newboard,board);
g_l_BOARDS = g_l_BOARDS + 1;
x= (int) (moves[i]/10);
y= moves[i]%10;
flip(player,x,y, newboard);
re_init(newboard);
next_player = nexttoplay(newboard, player);
if (next_player == 0) { /* game over, so determine winner */
newscore = evaluation(player, newboard);
if (newscore > 0) newscore = WIN; /* a win for player */
if (newscore < 0) newscore = LOSS; /* a win for opp */
}
else if (next_player == player) /* opponent cannot move */
{
newscore = maxchoice(player, newboard, ply-1, weight);
}
else if (next_player == ennemi(player))
{
newscore = minchoice(player, newboard, ply-1, weight);
}
if (newscore > i_max)
{
i_max = newscore;
bestmove = moves[i]; /* a better move found */
}
}
free(moves);
return bestmove;
}
int maxweighted(int player, int board[9][9])
{
int weight = weightedevaluation(player,board);
int best_move=minmax1(player, board, 6, weight);
return best_move;
}