int ply;
int best_move;
int move_score(CBoard_T board, Move move) {
if (Move_capture(move)) {
return Scores[CBoard_piece_get(board, Move_source(move))]
.capture[CBoard_piece_get(board, Move_target(move))];
} else {
}
return 0;
}
void MoveList_sort(CBoard_T board, MoveList_T list) {
int score[list->count];
for (int i = 0; i < list->count; i++)
score[i] = move_score(board, list->moves[i]);
for (int i = 0; i < list->count; i++)
for (int j = i + 1; j < list->count; j++)
if (score[i] < score[j]) {
Move t = list->moves[i];
list->moves[i] = list->moves[j];
list->moves[j] = t;
int s = score[i];
score[i] = score[j];
score[j] = s;
}
}
int quiescence(CBoard_T board, int alpha, int beta) {
MoveList_T moves;
CBoard_T backup;
int eval = evaluate(board);
nodes++;
if (eval >= beta) {
MoveList_free(&moves);
CBoard_free(&backup);
return beta;
}
if (eval > alpha) {
alpha = eval;
}
backup = CBoard_new();
moves = generate_moves(board, NULL);
MoveList_sort(board, moves);
int legal_moves = 0;
for (int i = 0; i < moves->count; i++) {
CBoard_copy(board, backup);
ply++;
if (make_move(board, moves->moves[i], 1) == 0) {
CBoard_copy(backup, board);
ply--;
continue;
}
int score = -quiescence(board, -beta, -alpha);
CBoard_copy(backup, board);
ply--;
if (score >= beta) {
MoveList_free(&moves);
CBoard_free(&backup);
return beta;
}
if (score > alpha) {
alpha = score;
}
}
MoveList_free(&moves);
CBoard_free(&backup);
return alpha;
}
int negamax(CBoard_T board, int alpha, int beta, int depth) {
MoveList_T moves;
CBoard_T backup;
int isCheck = 0;
// tmp
Move best;
int old_alpha = alpha;
if (depth == 0) {
return evaluate(board);
return quiescence(board, alpha, beta);
}
nodes++;
backup = CBoard_new();
moves = generate_moves(board, NULL);
isCheck = CBoard_isCheck(board);
if (isCheck)
depth++;
MoveList_sort(board, moves);
int legal_moves = 0;
for (int i = 0; i < moves->count; i++) {