int JulianDir(int s, int PID, int a, int NumOfRows, int NumOfCols, int FishArray[NumOfRows][NumOfCols], int AllPengs, int PengArray[AllPengs][3]) { int idRow = WhichPenguin(PID, AllPengs, PengArray); int PengX = PengArray[idRow][1]; int PengY = PengArray[idRow][2]; int *Move; Move = (int*)malloc(2*sizeof(int)); get_best_move(Move, idRow, s, PengX, PengY, NumOfRows, NumOfCols, FishArray, AllPengs, PengArray); int sp = Move[0]; int dir = Move[1]; // in some cases 'Greedy' algorithm does not work properly if(CheckMove(idRow, dir, sp, NumOfCols, FishArray, AllPengs, PengArray) == 0) { sp = 1; dir = 1; while(CheckMove(idRow, dir, sp, NumOfCols, FishArray, AllPengs, PengArray) == 0) { dir = dir + 1; } } return dir; }
static char *test_get_best_move() { Move m_result; Bitboard *b = create_test_bitboard(); float score = get_best_move(b, &m_result, PIECE_COLOR_WHITE, NULL); mu_assert("got score >= 0", score >= 0.0f); return 0; }
int JulianSpaces(int s, int PID, int a, int NumOfRows, int NumOfCols, int FishArray[NumOfRows][NumOfCols], int AllPengs, int PengArray[AllPengs][3]) { int idRow = WhichPenguin(PID, AllPengs, PengArray); int PengX = PengArray[idRow][1]; int PengY = PengArray[idRow][2]; int *Move; Move = (int*)malloc(2*sizeof(int)); get_best_move(Move, idRow, s, PengX, PengY, NumOfRows, NumOfCols, FishArray, AllPengs, PengArray); int sp = Move[0]; int dir = Move[1]; if(CheckMove(idRow, dir, sp, NumOfCols, FishArray, AllPengs, PengArray) == 0) sp = 1; return sp; }
// -- opponent thinking functions pthread_t opponent_thread; Move best_move_black; void flip_turn(Chessboard *cboard); void *get_opponent_best_move(void *param) { printf(" ** Thinking..."); Chessboard *cboard = (Chessboard *) param; void on_best_move_found (Move *m) { int i; for (i=0; i<64; i++) { cboard->cells_highlighted[i] = CELL_NONE; } cboard->cells_highlighted[CELL(m->to_file, m->to_rank)] = 1; cboard->cell_selected = CELL(m->from_file, m->from_rank); } float score = get_best_move(cboard->bitboard, &best_move_black, 1, on_best_move_found); if (!best_move_black.is_checkmate) { printf("Found best move with score %f\n", score); print_chessboard_move(cboard->bitboard, &best_move_black); bitboard_do_move(cboard->bitboard, &best_move_black); flip_turn(cboard); } else { printf("Checkmate :-(\n"); } } // -- end of opponent thinking functions void flip_turn(Chessboard *cboard) { cboard->player_turn =
// recursive function to try a game int trygame(int depth, int doUpdate) { struct Node *parent = (struct Node*)0; uint64_t goban_hash = goban.hash; int myturn = goban.turn; int result; int pos; int win; int x,y; int haspoints; uint64_t hash; if (depth == MAXDEPTH) return goban_getOwner(target); // see if we can use nt3 hash = hash_get(0, target, goban.hash, NODETYPE_POS |USE_TARGET); parent = hash_get_node(hash); pos = get_best_move( parent, (NODETYPE_POS| USE_TARGET| NODETYPE_MOVE), NULL ); if (parent->visits >= MIN_SPECIAL_VISITS && parent->visits % UPDATE_SPECIAL_RATE == 0) { update_special_node(pos); } if (mode == 1) { int p; for(y = state.boardsize; y >= 1; y--) for(x = 1; x <= state.boardsize; x++) update_special_node(POS(x,y)); p = engine_getbestmove(); if (p) pos = p; } goban_move(pos); #if LOG_MOVES_RATE > 0 if (depth == 0 && tries % LOG_MOVES_RATE == 0) { out(fileGTP, "Target %p ", target); } if (depth < 10 && tries % LOG_MOVES_RATE == 0) out(fileGTP, "%d%p ", 0, pos); #endif result = trygame(depth+1, parent->visits > 0); #if LOG_MOVES_RATE > 0 if (depth == 0 && tries % LOG_MOVES_RATE == 0) out(fileGTP, " res=%d, %r\n", result, goban.score); #endif haspoints = 0; for(y = state.boardsize; y >= 1; y--) for(x = 1; x <= state.boardsize; x++) { int targ = POS(x,y); if (targ != target) continue; result = goban_getOwner(targ); if (!result) continue; haspoints = 1; } if (!haspoints) { //out(fileInfo, "Failure getOwner(target)=%d\n", goban_getOwner(POS(7,5))); //outboard(fileInfo); } if (haspoints && doUpdate) for(y = state.boardsize; y >= 1; y--) for(x=1; x <= state.boardsize; x++) { int targ = POS(x,y); if (targ != target) continue; result = goban_getOwner(targ); if (!result) continue; win = (result == myturn ? 1 : 0); // store nt 3 hash_set_node(hash_get(pos, targ, goban_hash, NODETYPE_POS|NODETYPE_TARGET|NODETYPE_MOVE), win); hash_set_node(hash_get(pos, targ, goban_hash, NODETYPE_POS|NODETYPE_TARGET), win); } // update target-agnostic nodes (generic uct) if (haspoints && doUpdate && goban.score) { win = (myturn * goban.score > 0 ? 1 : 0); hash_set_node(hash_get(pos, 0, goban_hash, NODETYPE_POS|NODETYPE_MOVE), win); hash_set_node(hash_get(pos, 0, goban_hash, NODETYPE_POS), win); } if (haspoints && depth == 0) { for(y = state.boardsize; y >= 1; y--) for(x=1; x <= state.boardsize; x++) { int targ = POS(x,y); result = goban_getOwner(targ); pos_stat[targ].total++; if (result == myturn) pos_stat[targ].won++; else if (result == -myturn) pos_stat[targ].lost++; } //outboard(fileGTP); //update_special_node(pos); return 1; } return 0; return result; }