return score;
}
int quiescence(Stats *stats, Board *board, int alpha, int beta) {
MoveList *moves;
Board *copy;
int quiescence(Stats *stats, const Board *board, int alpha, int beta) {
int eval = evaluate(board);
stats->nodes++;
if (eval >= beta) {
return beta;
}
if (eval > alpha) {
alpha = eval;
}
if (eval >= beta) return beta;
if (eval > alpha) alpha = eval;
copy = board_new();
moves = move_list_generate(NULL, board);
move_list_sort(stats, moves);
Board copy;
MoveList moves;
move_list_generate(&moves, board);
move_list_sort(stats, &moves);
for (int i = 0; i < move_list_size(moves); i++) {
board_copy(board, copy);
for (int i = 0; i < move_list_size(&moves); i++) {
board_copy(board, ©);
if (move_make(move_list_move(moves, i), copy, 1) == 0) continue;
if (move_make(move_list_move(&moves, i), ©, 1) == 0) continue;
stats->ply++;
int score = -quiescence(stats, copy, -beta, -alpha);
int score = -quiescence(stats, ©, -beta, -alpha);
stats->ply--;
if (score >= beta) {
move_list_free(&moves);
board_free(©);
return beta;
}
if (score > alpha) {
alpha = score;
}
if (score >= beta) return beta;
if (score > alpha) alpha = score;
}
move_list_free(&moves);
board_free(©);
return alpha;
}
int negamax(Stats *stats, Board *board, int alpha, int beta, int depth) {
MoveList *list;
Board *copy;
int isCheck = 0;
int negamax(Stats *stats, const Board *board, int alpha, int beta, int depth) {
int ply = stats->ply;
stats->pv_length[ply] = ply;
if (depth == 0) return quiescence(stats, board, alpha, beta);
if (ply > MAX_PLY - 1) return evaluate(board);
stats->nodes++;
copy = board_new();
list = move_list_generate(NULL, board);
isCheck = board_isCheck(board);
int isCheck = board_isCheck(board);
if (isCheck) depth++;
Board copy;
MoveList list;
move_list_generate(&list, board);
if (stats->follow_pv) enable_pv_scoring(stats, &list);
move_list_sort(stats, &list);
int legal_moves = 0;
move_list_sort(stats, list);
for (int i = 0; i < move_list_size(list); i++) {
Move move = move_list_move(list, i);
for (int i = 0; i < move_list_size(&list); i++) {
Move move = move_list_move(&list, i);
board_copy(board, copy);
if (move_make(move, copy, 0) == 0) {
board_copy(board, ©);
if (move_make(move, ©, 0) == 0) {
continue;
}
stats->ply++;
int score = -negamax(stats, copy, -beta, -alpha, depth - 1);
int score = -negamax(stats, ©, -beta, -alpha, depth - 1);
stats->ply--;
legal_moves++;