1 |
17 |
sumanta.ch |
|
2 |
|
|
/*
|
3 |
|
|
|
4 |
|
|
connectk -- UMN CSci 5512W project
|
5 |
|
|
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
//#include "config.h"
|
9 |
|
|
//#include <string.h>
|
10 |
|
|
//#include <glib.h>
|
11 |
|
|
//#include <iostream>
|
12 |
|
|
//#include <stdio.h>
|
13 |
|
|
#include "./shared.h"
|
14 |
|
|
#include "pico.h"
|
15 |
|
|
//#include "../connectk.h"
|
16 |
|
|
|
17 |
|
|
/* Variables required to check for cache hits */
|
18 |
|
|
//static int cache_id = -1, cache_depth = -1, cache_branch = -1;
|
19 |
|
|
//static SEARCH cache_search = -1;
|
20 |
|
|
//static AIMoves *cache_moves = NULL;
|
21 |
|
|
//static AIWEIGHT cache_best;
|
22 |
|
|
//static Player *cache_player;
|
23 |
|
|
//static AIFunc cache_func;
|
24 |
|
|
//static BCOORD cache_size;
|
25 |
|
|
|
26 |
|
|
int ai_stop=0;
|
27 |
|
|
int mini(int x,int y){
|
28 |
|
|
return (x<=y)?x:y;
|
29 |
|
|
}
|
30 |
|
|
int maxi(int x,int y){
|
31 |
|
|
return (x>=y)?x:y;
|
32 |
|
|
}
|
33 |
|
|
int mod2(int x){
|
34 |
|
|
#pragma bitsize x 5
|
35 |
|
|
int ans[16]={0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
|
36 |
|
|
return ans[x];
|
37 |
|
|
}
|
38 |
|
|
int mod5(int x){
|
39 |
|
|
#pragma bitsize x 5
|
40 |
|
|
int ans[16]={0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0};
|
41 |
|
|
return ans[x];
|
42 |
|
|
}
|
43 |
|
|
/*static*/ AIWEIGHT df_search(Board *b, AIMoves *moves,/*index_array *index,*/ Player *player,int depth, int cache_index, PIECE searched, AIWEIGHT alpha, AIWEIGHT beta)
|
44 |
|
|
/* Depth is in _moves_ */
|
45 |
|
|
{
|
46 |
|
|
#pragma internal_fast index
|
47 |
|
|
int i, j;
|
48 |
|
|
#pragma bitsize i 16
|
49 |
|
|
#pragma bitsize j 16
|
50 |
|
|
Board b_next[2][16];
|
51 |
|
|
#pragma internal_blockram b_next
|
52 |
|
|
AIMoves moves_next[2][16];
|
53 |
|
|
#pragma internal_fast moves_next
|
54 |
|
|
#pragma unpacked moves_next
|
55 |
|
|
AIWEIGHT utility[5][16];
|
56 |
|
|
#pragma internal_blockram utility
|
57 |
|
|
PIECE turn[5]={b->turn};
|
58 |
|
|
int branch=player->branch;
|
59 |
|
|
|
60 |
|
|
board_copy(b, &b_next[0][0]);
|
61 |
|
|
ai_threats(b_next,0,0,moves_next/*,index*/);
|
62 |
|
|
utility[0][0]=moves_next[0][0].utility;
|
63 |
|
|
turn[0]=b->turn;
|
64 |
|
|
for(i=0;i<branch;i++){
|
65 |
|
|
moves->data[i].x=moves_next[0][0].data[i].x;
|
66 |
|
|
moves->data[i].y=moves_next[0][0].data[i].y;
|
67 |
|
|
//moves->data[i].weight=moves_next[1][i].utility;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
///* Search only the top moves beyond the minimum */
|
71 |
|
|
////aimoves_sort(moves);
|
72 |
|
|
//if (moves->len > player->branch) {
|
73 |
|
|
// //for (i = player->branch; i < moves->len; i++)
|
74 |
|
|
// // if (moves->data[i].weight != moves->data[0].weight)
|
75 |
|
|
// // break;
|
76 |
|
|
// //moves->len = i;
|
77 |
|
|
// moves->len = player->branch;
|
78 |
|
|
//}
|
79 |
|
|
|
80 |
|
|
/* No moves left -- its a draw */
|
81 |
|
|
|
82 |
|
|
if (moves_next[0][0].len < 1) //"(%s)", bcoords_to_string(aim->x, aim->y));
|
83 |
|
|
|
84 |
|
|
return AIW_DRAW;
|
85 |
|
|
//board_copy(b, &b_next[0][0]);
|
86 |
|
|
|
87 |
|
|
/* Search each move available in depth first order */
|
88 |
|
|
for(j=0;j<depth;j++){
|
89 |
|
|
int k,branches=1;
|
90 |
|
|
for(k=0;k<j+1;k++) branches*=branch;
|
91 |
|
|
//int branches=(player->branch)^j;
|
92 |
|
|
//printf("branches %d\n",branches);
|
93 |
|
|
//int current_j=j % 2;
|
94 |
|
|
//int next_j=(j+1) % 2;
|
95 |
|
|
int current_j=mod2(j);
|
96 |
|
|
int next_j=mod2(j+1);
|
97 |
|
|
if (b_next[current_j][0].moves_left <= 0) turn[j]=other_player(b_next[current_j][0].turn);
|
98 |
|
|
else turn[j]=b_next[current_j][0].turn;
|
99 |
|
|
|
100 |
|
|
for (i = 0; i < branches; i++) {
|
101 |
|
|
//if(!(moves_next[j][i>>1].utility==AIW_WIN || moves_next[j][i>>1].utility==-AIW_WIN)){
|
102 |
|
|
if(!(utility[j][i>>1]==AIW_WIN || utility[j][i>>1]==-AIW_WIN)){
|
103 |
|
|
//AIMove aim = *(moves_next[current_j][i>>1].data + (i % branch));
|
104 |
|
|
AIMove aim = *(moves_next[current_j][i>>1].data + mod2(i));
|
105 |
|
|
//printf ("aim->utility %d \n",utility[j][i>>1]);
|
106 |
|
|
|
107 |
|
|
board_copy(&b_next[current_j][i>>1], &b_next[next_j][i]);
|
108 |
|
|
//if(moves_next[j][i/2].len<branch) printf ("caca");
|
109 |
|
|
//printf("%d %d\n",aim.x,aim.y);
|
110 |
|
|
|
111 |
|
|
/* Did we get a bad move? */
|
112 |
|
|
if (!piece_empty(piece_at(&b_next[next_j][i], aim.x, aim.y))) {
|
113 |
|
|
//g_warning("DFS utility function suggested a bad move "
|
114 |
|
|
//"(%s)", bcoords_to_string(aim->x, aim->y));
|
115 |
|
|
//printf("bad move\n");
|
116 |
|
|
continue;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/* Already searched here? */
|
120 |
|
|
///////////////////////////if (piece_at(&b_next[j+1][i], aim->x, aim->y) == searched){
|
121 |
|
|
/////////////////////////// moves_next[j+1][i].utility=moves_next[j+1][i>>1].utility;
|
122 |
|
|
/////////////////////////// continue;
|
123 |
|
|
///////////////////////////}
|
124 |
|
|
///////////////////////////place_piece_type(&b_next[j+1][i], aim->x, aim->y, searched);
|
125 |
|
|
|
126 |
|
|
//b_next = board_new();
|
127 |
|
|
place_piece(&b_next[next_j][i], aim.x, aim.y);
|
128 |
|
|
AIWEIGHT next_alpha = alpha, next_beta = beta;
|
129 |
|
|
//AIFunc func;
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
/* Player has changed */
|
133 |
|
|
//printf("depth %d branches %d turn %d \n\n",j,branches,turn[j]);
|
134 |
|
|
if (b_next[next_j][i].moves_left <= 0) {
|
135 |
|
|
b_next[next_j][i].moves_left = place_p;
|
136 |
|
|
b_next[next_j][i].turn = other_player(b_next[current_j][i].turn);
|
137 |
|
|
searched++;
|
138 |
|
|
next_alpha = -beta;
|
139 |
|
|
next_beta = -alpha;
|
140 |
|
|
}
|
141 |
|
|
b_next[next_j][i].moves_left--;
|
142 |
|
|
|
143 |
|
|
/* Did we win? */
|
144 |
|
|
|
145 |
|
|
if (check_win_full(&b_next[1][i], aim.x, aim.y,0,0,0,0)){
|
146 |
|
|
aim.weight = AIW_WIN;
|
147 |
|
|
moves_next[next_j][i].utility=AIW_WIN;
|
148 |
|
|
utility[j+1][i]=AIW_WIN;
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
//}else if(moves_next[j][i>>1].utility==AIW_WIN || moves_next[j][i>>1].utility==-AIW_WIN ){
|
152 |
|
|
}else if(utility[j][i>>1]==AIW_WIN || utility[j][i>>1]==-AIW_WIN ){
|
153 |
|
|
//moves_next[j+1][i].utility=AIW_WIN;
|
154 |
|
|
utility[j+1][i]=AIW_WIN;
|
155 |
|
|
/* Otherwise, search deeper */
|
156 |
|
|
}else {
|
157 |
|
|
|
158 |
|
|
//func = ai(player->ai)->func;
|
159 |
|
|
//if (!func) {
|
160 |
|
|
// g_warning("DFS player has no AI function");
|
161 |
|
|
// return moves->utility;
|
162 |
|
|
//}
|
163 |
|
|
//moves_next = func(b_next);
|
164 |
|
|
ai_threats(b_next,next_j,i,moves_next/*,index*/);
|
165 |
|
|
utility[j+1][i]=moves_next[next_j][i].utility;
|
166 |
|
|
|
167 |
|
|
//aim->weight = df_search(&b_next, &moves_next, index,player,
|
168 |
|
|
// depth - 1, next_ci, searched,
|
169 |
|
|
// next_alpha, next_beta);
|
170 |
|
|
//aimoves_free(moves_next);
|
171 |
|
|
}
|
172 |
|
|
////////////////////////////////////////////////////////////////////////if (b_next[next_j][i].turn != b->turn)
|
173 |
|
|
//moves_next[j+1][i].utility=-moves_next[j+1][i].utility;
|
174 |
|
|
////////////////////////////////////////////////////////////////utility[j+1][i]=-moves_next[1][i].utility;
|
175 |
|
|
//if (moves_next[j+1][i].utility >= AIW_WIN)
|
176 |
|
|
// moves_next[j+1][i].utility=AIW_WIN;
|
177 |
|
|
|
178 |
|
|
/* Debug search */
|
179 |
|
|
//if (opt_debug_dfsc) {
|
180 |
|
|
// for(j = MAX_DEPTH - depth; j > 0; j--)
|
181 |
|
|
// //g_print("-");
|
182 |
|
|
// //g_print("> d=%d, %s, u=%d, a=%d, b=%d %s\n",
|
183 |
|
|
// // depth, bcoords_to_string(aim->x, aim->y),
|
184 |
|
|
// // aim->weight, alpha, beta,
|
185 |
|
|
// // piece_to_string(b->turn));
|
186 |
|
|
//}
|
187 |
|
|
|
188 |
|
|
//board_free(b_next);
|
189 |
|
|
//if (aim->weight > alpha) {
|
190 |
|
|
// alpha = aim->weight;
|
191 |
|
|
// //cache_set(cache_index, aim);
|
192 |
|
|
|
193 |
|
|
// /* Victory abort */
|
194 |
|
|
// if (alpha >= AIW_WIN)
|
195 |
|
|
// return AIW_WIN;
|
196 |
|
|
|
197 |
|
|
// /* Alpha-beta pruning */
|
198 |
|
|
// if (alpha >= beta)
|
199 |
|
|
// return alpha;
|
200 |
|
|
//}
|
201 |
|
|
//printf("%d %d %d\n",j,i,moves_next[j+1][i].utility);
|
202 |
|
|
}else //moves_next[j+1][i].utility=AIW_WIN;
|
203 |
|
|
utility[j+1][i]=AIW_WIN;
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
for(j=depth-2;j>=0;j--){
|
207 |
|
|
int k,branches=1;
|
208 |
|
|
for(k=0;k<j+1;k++) branches*=branch;
|
209 |
|
|
//int branches=(player->branch)^j;
|
210 |
|
|
//printf("branches %d player %d\n",branches,b_next[j+1][i].turn);
|
211 |
|
|
for (i = 0; i < branches; i=i+2) {
|
212 |
|
|
//if (b_next[next_j][i].turn != b->turn)
|
213 |
|
|
if (turn[j] != b->turn)
|
214 |
|
|
//moves_next[j][i>>1].utility=mini(moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
215 |
|
|
utility[j][i>>1]=mini(utility[j+1][i],utility[j+1][i+1]);
|
216 |
|
|
else
|
217 |
|
|
//moves_next[j][i>>1].utility=maxi(moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
218 |
|
|
utility[j][i>>1]=maxi(utility[j+1][i],utility[j+1][i+1]);
|
219 |
|
|
|
220 |
|
|
//printf("%d %d\n",moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
//for(i=0;i<branch;i++){
|
225 |
|
|
//moves_next[0][0].data[i].x=moves->data[i].x;
|
226 |
|
|
//moves_next[0][0].data[i].y=moves->data[i].y;
|
227 |
|
|
//moves_next[0][0].data[i].weight=moves->data[i].weight;
|
228 |
|
|
//}
|
229 |
|
|
//moves_next[0][0].utility=moves->utility;
|
230 |
|
|
//moves_next[0][0].len=branch;
|
231 |
|
|
for(i=0;i<branch;i++){
|
232 |
|
|
//moves->data[i].x=moves_next[0][0].data[i].x;
|
233 |
|
|
//moves->data[i].y=moves_next[0][0].data[i].y;
|
234 |
|
|
//moves->data[i].weight=moves_next[1][i].utility;
|
235 |
|
|
moves->data[i].weight=utility[1][i];
|
236 |
|
|
}
|
237 |
|
|
moves->len=branch;
|
238 |
|
|
|
239 |
|
|
return alpha;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
int search(Board *b, AIMove *move, Player *player)
|
243 |
|
|
{
|
244 |
|
|
AIMoves moves;
|
245 |
|
|
#pragma internal_blockram moves
|
246 |
|
|
moves.len=0;
|
247 |
|
|
Board copy;
|
248 |
|
|
#pragma internal_blockram copy
|
249 |
|
|
/*index_array index={0};*/
|
250 |
|
|
#pragma internal_fast index
|
251 |
|
|
//AIFunc move_func = ai(player->ai)->func;
|
252 |
|
|
|
253 |
|
|
/* Player is not configured to search */
|
254 |
|
|
//if (player->search == SEARCH_NONE)
|
255 |
|
|
// return;
|
256 |
|
|
|
257 |
|
|
/* Moves list does not need to be searched */
|
258 |
|
|
//if (moves->len <= b->moves_left) {
|
259 |
|
|
//// if (opt_debug_dfsc)
|
260 |
|
|
//// g_debug("DFS no choice abort");
|
261 |
|
|
// return;
|
262 |
|
|
//}
|
263 |
|
|
|
264 |
|
|
///* Board size changed, cache is invalidated */
|
265 |
|
|
//if (board_size != cache_size)
|
266 |
|
|
// cache_moves = NULL;
|
267 |
|
|
//cache_size = board_size;
|
268 |
|
|
|
269 |
|
|
///* Cache hit, last or same board */
|
270 |
|
|
//if (player->cache && cache_moves && cache_moves->len &&
|
271 |
|
|
// cache_search == player->search &&
|
272 |
|
|
// cache_depth == player->depth &&
|
273 |
|
|
// cache_player == player &&
|
274 |
|
|
// cache_func == move_func &&
|
275 |
|
|
// cache_branch == player->branch) {
|
276 |
|
|
// if (b->parent && cache_id == b->parent->ac.id) {
|
277 |
|
|
// aimoves_remove(cache_moves, b->parent->move_x,
|
278 |
|
|
// b->parent->move_y);
|
279 |
|
|
// cache_id = b->ac.id;
|
280 |
|
|
// }
|
281 |
|
|
// if (cache_id == b->ac.id && cache_moves->len) {
|
282 |
|
|
// if (cache_moves->len) {
|
283 |
|
|
// aimoves_copy(cache_moves, moves);
|
284 |
|
|
// if (opt_debug_dfsc)
|
285 |
|
|
// g_debug("DFS cache HIT");
|
286 |
|
|
// return;
|
287 |
|
|
// }
|
288 |
|
|
// aimoves_free(cache_moves);
|
289 |
|
|
// cache_moves = NULL;
|
290 |
|
|
// }
|
291 |
|
|
//}
|
292 |
|
|
|
293 |
|
|
/* Cache miss */
|
294 |
|
|
//if (opt_debug_dfsc)
|
295 |
|
|
// g_debug("DFS cache MISS");
|
296 |
|
|
//cache_id = b->ac.id;
|
297 |
|
|
//if (!cache_moves)
|
298 |
|
|
// cache_moves = aimoves_new();
|
299 |
|
|
//cache_moves->len = 0;
|
300 |
|
|
//cache_best = AIW_MIN;
|
301 |
|
|
//copy = board_new();
|
302 |
|
|
board_copy(b, ©);
|
303 |
|
|
//ai_threats(©,&moves,&index);
|
304 |
|
|
|
305 |
|
|
//if (player->search == SEARCH_DFS) {
|
306 |
|
|
df_search(©, &moves, /*&index,*/player, player->depth, 0,
|
307 |
|
|
PIECE_SEARCHED, AIW_LOSE, AIW_WIN);
|
308 |
|
|
//printf("FINAL WEIGHTS %d %d \n\n",moves.data[0].weight,moves.data[1].weight);
|
309 |
|
|
int ret_val;
|
310 |
|
|
ret_val=aimoves_choose(&moves, move/*,&index*/);
|
311 |
|
|
if (!ret_val)
|
312 |
|
|
return 0;
|
313 |
|
|
else return 1;
|
314 |
|
|
// if (cache_moves->len)
|
315 |
|
|
// aimoves_copy(cache_moves, moves);
|
316 |
|
|
//} else {
|
317 |
|
|
// board_free(copy);
|
318 |
|
|
// g_warning("Unsupported search type %d", player->search);
|
319 |
|
|
// return;
|
320 |
|
|
//}
|
321 |
|
|
//board_free(copy);
|
322 |
|
|
|
323 |
|
|
///* Debug DFS search */
|
324 |
|
|
//if (opt_debug_dfsc)
|
325 |
|
|
// dfs_cache_dump();
|
326 |
|
|
|
327 |
|
|
///* Save params so we can check if we have a hit later */
|
328 |
|
|
//cache_player = player;
|
329 |
|
|
//cache_search = player->search;
|
330 |
|
|
//cache_depth = player->depth;
|
331 |
|
|
//cache_branch = player->branch;
|
332 |
|
|
//cache_func = move_func;
|
333 |
|
|
}
|