}
}
return side == WHITE ? score : -score;
return score;
}
int ply;
int best_move;
int negamax(CBoard_T board, int alpha, int beta, int depth) {
MoveList_T moves;
CBoard_T backup;
// tmp
Move best;
int old_alpha = alpha;
if (depth == 0) {
return evaluate(board);
}
nodes++;
backup = CBoard_new();
moves = generate_moves(board, NULL);
int legal_moves = 0;
for (int i = 0; i < moves->count; i++) {
CBoard_copy(board, backup);
ply++;
if (make_move(board, moves->moves[i], 0) == 0) {
CBoard_copy(backup, board);
ply--;
continue;
}
legal_moves++;
int score = -negamax(board, -beta, -alpha, depth - 1);
CBoard_copy(backup, board);
ply--;
if (score >= beta) {
MoveList_free(&moves);
CBoard_free(&backup);
return beta;
}
if (score > alpha) {
alpha = score;
if (ply == 0)
best = moves->moves[i];
}
}
if (legal_moves == 0) {
if (CBoard_isCheck(board))
return -49000 + ply;
else
return 0;
}
if (old_alpha != alpha)
best_move = best;
MoveList_free(&moves);
CBoard_free(&backup);
return alpha;
}
void search_position(CBoard_T cboard, int depth) {
(void)cboard;
(void)depth;
printf("bestmove d7d5\n");
void search_position(CBoard_T board, int depth) {
best_move = 0;
int score = negamax(board, -5000000, 5000000, depth);
if (best_move) {
printf("info score cp %d depth %d nodes %ld\n", score, depth, nodes);
printf("bestmove ");
Move_print_UCI(best_move);
printf("\n");
}
}
void print_info(void) {