Line 28... |
Line 28... |
return (x<=y)?x:y;
|
return (x<=y)?x:y;
|
}
|
}
|
int maxi(int x,int y){
|
int maxi(int x,int y){
|
return (x>=y)?x:y;
|
return (x>=y)?x:y;
|
}
|
}
|
static AIWEIGHT df_search(Board *b, AIMoves *moves,unsigned int *index, Player *player,
|
static AIWEIGHT df_search(Board *b, AIMoves *moves,index_array *index, Player *player,
|
int depth, int cache_index,
|
int depth, int cache_index,
|
PIECE searched, AIWEIGHT alpha, AIWEIGHT beta)
|
PIECE searched, AIWEIGHT alpha, AIWEIGHT beta)
|
/* Depth is in _moves_ */
|
/* Depth is in _moves_ */
|
{
|
{
|
#pragma bitsize index 9
|
|
#pragma internal_fast index
|
|
unsigned int index1[361]={0};
|
|
#pragma bitsize index 9
|
|
#pragma internal_fast index
|
#pragma internal_fast index
|
int i, j;
|
int i, j;
|
|
#pragma bitsize i 16
|
|
#pragma bitsize j 16
|
Board b_next[5][16];
|
Board b_next[5][16];
|
#pragma internal_blockram b_next
|
#pragma internal_blockram b_next
|
AIMoves moves_next[5][16];
|
AIMoves moves_next[5][16];
|
#pragma internal_fast moves_next
|
#pragma internal_fast moves_next
|
|
AIWEIGHT utility[5][16];
|
int branch=player->branch;
|
int branch=player->branch;
|
|
|
|
board_copy(b, &b_next[0][0]);
|
|
ai_threats(b_next,0,0,moves_next,index);
|
|
|
|
|
///* Search only the top moves beyond the minimum */
|
///* Search only the top moves beyond the minimum */
|
////aimoves_sort(moves);
|
////aimoves_sort(moves);
|
//if (moves->len > player->branch) {
|
//if (moves->len > player->branch) {
|
Line 58... |
Line 59... |
// //moves->len = i;
|
// //moves->len = i;
|
// moves->len = player->branch;
|
// moves->len = player->branch;
|
//}
|
//}
|
|
|
/* No moves left -- its a draw */
|
/* No moves left -- its a draw */
|
for(i=0;i<branch;i++){
|
|
moves_next[0][0].data[i].x=moves->data[i].x;
|
|
moves_next[0][0].data[i].y=moves->data[i].y;
|
|
moves_next[0][0].data[i].weight=moves->data[i].weight;
|
|
}
|
|
moves_next[0][0].utility=moves->utility;
|
|
moves_next[0][0].len=branch;
|
|
|
|
if (moves->len < 1) //"(%s)", bcoords_to_string(aim->x, aim->y));
|
if (moves_next[0][0].len < 1) //"(%s)", bcoords_to_string(aim->x, aim->y));
|
|
|
return AIW_DRAW;
|
return AIW_DRAW;
|
board_copy(b, &b_next[0][0]);
|
//board_copy(b, &b_next[0][0]);
|
|
|
/* Search each move available in depth first order */
|
/* Search each move available in depth first order */
|
for(j=0;j<depth;j++){
|
for(j=0;j<depth;j++){
|
int k,branches=1;
|
int k,branches=1;
|
for(k=0;k<j+1;k++) branches*=branch;
|
for(k=0;k<j+1;k++) branches*=branch;
|
//int branches=(player->branch)^j;
|
//int branches=(player->branch)^j;
|
//printf("branches %d\n",branches);
|
//printf("branches %d\n",branches);
|
for (i = 0; i < branches; i++) {
|
for (i = 0; i < branches; i++) {
|
AIMove *aim = moves_next[j][i>>1].data + (i % branch);
|
//if(!(moves_next[j][i>>1].utility==AIW_WIN || moves_next[j][i>>1].utility==-AIW_WIN)){
|
|
if(!(utility[j][i>>1]==AIW_WIN || utility[j][i>>1]==-AIW_WIN)){
|
|
AIMove aim = *(moves_next[j][i>>1].data + (i % branch));
|
|
//printf ("aim->utility %d \n",moves_next[j][i>>1].utility);
|
|
|
board_copy(&b_next[j][i>>1], &b_next[j+1][i]);
|
board_copy(&b_next[j][i>>1], &b_next[j+1][i]);
|
|
//if(moves_next[j][i/2].len<branch) printf ("caca");
|
|
//printf("%d %d\n",aim.x,aim.y);
|
|
|
/* Did we get a bad move? */
|
/* Did we get a bad move? */
|
//if (!piece_empty(piece_at(&b_next[j+1][i], aim->x, aim->y))) {
|
if (!piece_empty(piece_at(&b_next[j+1][i], aim.x, aim.y))) {
|
// //g_warning("DFS utility function suggested a bad move "
|
//g_warning("DFS utility function suggested a bad move "
|
// //"(%s)", bcoords_to_string(aim->x, aim->y));
|
//"(%s)", bcoords_to_string(aim->x, aim->y));
|
// //printf("bad move\n");
|
//printf("bad move\n");
|
// continue;
|
continue;
|
//}
|
}
|
|
|
/* Already searched here? */
|
/* Already searched here? */
|
///////////////////////////if (piece_at(&b_next[j+1][i], aim->x, aim->y) == searched){
|
///////////////////////////if (piece_at(&b_next[j+1][i], aim->x, aim->y) == searched){
|
/////////////////////////// moves_next[j+1][i].utility=moves_next[j+1][i>>1].utility;
|
/////////////////////////// moves_next[j+1][i].utility=moves_next[j+1][i>>1].utility;
|
/////////////////////////// continue;
|
/////////////////////////// continue;
|
///////////////////////////}
|
///////////////////////////}
|
///////////////////////////place_piece_type(&b_next[j+1][i], aim->x, aim->y, searched);
|
///////////////////////////place_piece_type(&b_next[j+1][i], aim->x, aim->y, searched);
|
|
|
//b_next = board_new();
|
//b_next = board_new();
|
place_piece(&b_next[j+1][i], aim->x, aim->y);
|
place_piece(&b_next[j+1][i], aim.x, aim.y);
|
AIWEIGHT next_alpha = alpha, next_beta = beta;
|
AIWEIGHT next_alpha = alpha, next_beta = beta;
|
//AIFunc func;
|
//AIFunc func;
|
|
|
|
|
/* Player has changed */
|
/* Player has changed */
|
Line 115... |
Line 114... |
}
|
}
|
b_next[j+1][i].moves_left--;
|
b_next[j+1][i].moves_left--;
|
|
|
/* Did we win? */
|
/* Did we win? */
|
|
|
if (check_win_full(&b_next[j+1][i], aim->x, aim->y,0,0,0,0)){
|
if (check_win_full(&b_next[j+1][i], aim.x, aim.y,0,0,0,0)){
|
aim->weight = AIW_WIN;
|
aim.weight = AIW_WIN;
|
moves_next[j+1][i].utility=AIW_WIN;
|
moves_next[j+1][i].utility=AIW_WIN;
|
|
utility[j+1][i]=AIW_WIN;
|
|
|
|
|
}else if(moves_next[j][i>>1].utility==AIW_WIN || moves_next[j][i>>1].utility==-AIW_WIN ){
|
}else if(moves_next[j][i>>1].utility==AIW_WIN || moves_next[j][i>>1].utility==-AIW_WIN ){
|
moves_next[j+1][i].utility=AIW_WIN;
|
//moves_next[j+1][i].utility=AIW_WIN;
|
|
utility[j+1][i]=AIW_WIN;
|
/* Otherwise, search deeper */
|
/* Otherwise, search deeper */
|
}else {
|
}else {
|
|
|
//func = ai(player->ai)->func;
|
//func = ai(player->ai)->func;
|
//if (!func) {
|
//if (!func) {
|
// g_warning("DFS player has no AI function");
|
// g_warning("DFS player has no AI function");
|
// return moves->utility;
|
// return moves->utility;
|
//}
|
//}
|
//moves_next = func(b_next);
|
//moves_next = func(b_next);
|
ai_threats(&b_next[j+1][i],&moves_next[j+1][i],&index1[0]);
|
ai_threats(b_next,j+1,i,moves_next,index);
|
|
utility[j+1][i]=moves_next[j+1][i].utility;
|
|
|
//aim->weight = df_search(&b_next, &moves_next, index,player,
|
//aim->weight = df_search(&b_next, &moves_next, index,player,
|
// depth - 1, next_ci, searched,
|
// depth - 1, next_ci, searched,
|
// next_alpha, next_beta);
|
// next_alpha, next_beta);
|
//aimoves_free(moves_next);
|
//aimoves_free(moves_next);
|
}
|
}
|
if (b_next[j+1][i].turn != b->turn)
|
if (b_next[j+1][i].turn != b->turn)
|
moves_next[j+1][i].utility=-moves_next[j+1][i].utility;
|
//moves_next[j+1][i].utility=-moves_next[j+1][i].utility;
|
|
utility[j+1][i]=-moves_next[j+1][i].utility;
|
//if (moves_next[j+1][i].utility >= AIW_WIN)
|
//if (moves_next[j+1][i].utility >= AIW_WIN)
|
// moves_next[j+1][i].utility=AIW_WIN;
|
// moves_next[j+1][i].utility=AIW_WIN;
|
|
|
/* Debug search */
|
/* Debug search */
|
//if (opt_debug_dfsc) {
|
//if (opt_debug_dfsc) {
|
Line 153... |
Line 157... |
// // aim->weight, alpha, beta,
|
// // aim->weight, alpha, beta,
|
// // piece_to_string(b->turn));
|
// // piece_to_string(b->turn));
|
//}
|
//}
|
|
|
//board_free(b_next);
|
//board_free(b_next);
|
if (aim->weight > alpha) {
|
//if (aim->weight > alpha) {
|
alpha = aim->weight;
|
// alpha = aim->weight;
|
//cache_set(cache_index, aim);
|
// //cache_set(cache_index, aim);
|
|
|
/* Victory abort */
|
// /* Victory abort */
|
if (alpha >= AIW_WIN)
|
// if (alpha >= AIW_WIN)
|
return AIW_WIN;
|
// return AIW_WIN;
|
|
|
/* Alpha-beta pruning */
|
// /* Alpha-beta pruning */
|
if (alpha >= beta)
|
// if (alpha >= beta)
|
return alpha;
|
// return alpha;
|
}
|
//}
|
//printf("%d %d %d\n",j,i,moves_next[j+1][i].utility);
|
//printf("%d %d %d\n",j,i,moves_next[j+1][i].utility);
|
|
}else //moves_next[j+1][i].utility=AIW_WIN;
|
|
utility[j+1][i]=AIW_WIN;
|
}
|
}
|
}
|
}
|
for(j=depth-1;j>0;j--){
|
for(j=depth-1;j>0;j--){
|
int k,branches=1;
|
int k,branches=1;
|
for(k=0;k<j+1;k++) branches*=branch;
|
for(k=0;k<j+1;k++) branches*=branch;
|
//int branches=(player->branch)^j;
|
//int branches=(player->branch)^j;
|
//printf("branches %d player %d\n",branches,b_next[j+1][i].turn);
|
//printf("branches %d player %d\n",branches,b_next[j+1][i].turn);
|
for (i = 0; i < branches; i=i+2) {
|
for (i = 0; i < branches; i=i+2) {
|
if (b_next[j+1][i].turn != b->turn)
|
if (b_next[j+1][i].turn != b->turn)
|
moves_next[j][i>>1].utility=maxi(moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
//moves_next[j][i>>1].utility=mini(moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
|
utility[j][i>>1]=mini(utility[j+1][i],utility[j+1][i+1]);
|
else
|
else
|
moves_next[j][i>>1].utility=mini(moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
//moves_next[j][i>>1].utility=maxi(moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
|
utility[j][i>>1]=maxi(utility[j+1][i],utility[j+1][i+1]);
|
|
|
//printf("%d %d\n",moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
//printf("%d %d\n",moves_next[j+1][i].utility,moves_next[j+1][i+1].utility);
|
}
|
}
|
}
|
}
|
|
|
|
//for(i=0;i<branch;i++){
|
|
//moves_next[0][0].data[i].x=moves->data[i].x;
|
|
//moves_next[0][0].data[i].y=moves->data[i].y;
|
|
//moves_next[0][0].data[i].weight=moves->data[i].weight;
|
|
//}
|
|
//moves_next[0][0].utility=moves->utility;
|
|
//moves_next[0][0].len=branch;
|
for(i=0;i<branch;i++){
|
for(i=0;i<branch;i++){
|
moves->data[i].weight=moves_next[1][i].utility;
|
moves->data[i].x=moves_next[0][0].data[i].x;
|
|
moves->data[i].y=moves_next[0][0].data[i].y;
|
|
//moves->data[i].weight=moves_next[1][i].utility;
|
|
moves->data[i].weight=utility[1][i];
|
}
|
}
|
|
moves->len=branch;
|
|
|
return alpha;
|
return alpha;
|
}
|
}
|
|
|
int search(const Board *b, AIMove *move, Player *player)
|
int search(Board *b, AIMove *move, Player *player)
|
{
|
{
|
AIMoves moves;
|
AIMoves moves;
|
#pragma internal_blockram moves
|
#pragma internal_blockram moves
|
|
moves.len=0;
|
Board copy;
|
Board copy;
|
#pragma internal_blockram copy
|
#pragma internal_blockram copy
|
unsigned int index[361]={0};
|
index_array index={0};
|
#pragma bitsize index 9
|
|
#pragma internal_fast index
|
#pragma internal_fast index
|
//AIFunc move_func = ai(player->ai)->func;
|
//AIFunc move_func = ai(player->ai)->func;
|
|
|
/* Player is not configured to search */
|
/* Player is not configured to search */
|
//if (player->search == SEARCH_NONE)
|
//if (player->search == SEARCH_NONE)
|
Line 249... |
Line 270... |
// cache_moves = aimoves_new();
|
// cache_moves = aimoves_new();
|
//cache_moves->len = 0;
|
//cache_moves->len = 0;
|
//cache_best = AIW_MIN;
|
//cache_best = AIW_MIN;
|
//copy = board_new();
|
//copy = board_new();
|
board_copy(b, ©);
|
board_copy(b, ©);
|
ai_threats(©,&moves,&index[0]);
|
//ai_threats(©,&moves,&index);
|
|
|
//if (player->search == SEARCH_DFS) {
|
//if (player->search == SEARCH_DFS) {
|
df_search(©, &moves, &index[0],player, player->depth, 0,
|
df_search(©, &moves, &index,player, player->depth, 0,
|
PIECE_SEARCHED, AIW_LOSE, AIW_WIN);
|
PIECE_SEARCHED, AIW_LOSE, AIW_WIN);
|
//printf("%d %d \n",moves.data[0].weight,moves.data[1].weight);
|
//printf("%d %d \n",moves.data[0].weight,moves.data[1].weight);
|
int ret_val;
|
int ret_val;
|
ret_val=aimoves_choose(&moves, move,&index[0]);
|
ret_val=aimoves_choose(&moves, move,&index);
|
if (!ret_val)
|
if (!ret_val)
|
return 0;
|
return 0;
|
else return 1;
|
else return 1;
|
// if (cache_moves->len)
|
// if (cache_moves->len)
|
// aimoves_copy(cache_moves, moves);
|
// aimoves_copy(cache_moves, moves);
|