Code: Select all
static inline int NegaMaxSearch(int alpha, int beta, CHESSBOARD *board, SEARCH *info, int depth)
{
int legalMoves = 0;
int bestScore = -50000;
int bestMove = 0;
int bestLast = 0;
if(info->bestMove)
bestLast = info->bestMove;
if(InCheck(board, side))
depth++;
if(!depth)
{
info->nodes++;
return QuiescenceSearch(alpha, beta, board, info, 4);
}
MOVELIST list[1];
GenerateMoves(board, list);
for(int moveNum = 0; moveNum < list->moveCount; ++moveNum)
{
CHESSBOARD boardStored[1];
boardStored[0] = board[0];
if(!MakeMove(board, list->moves[moveNum].move, all))
continue;
legalMoves++;
int score = -NegaMaxSearch(-beta, -alpha, board, info, depth - 1);
if(score > bestScore)
{
bestScore = score;
bestMove = list->moves[moveNum].move;
info->bestScore = bestScore;
info->bestMove = bestMove;
if(score >= beta)
return beta;
if(score > alpha)
alpha = score;
}
TakeBack(board, boardStored);
}
// checkmate/stalemate detection
if(!legalMoves)
{
if(InCheck(board, side))
return -100000; // on checkmate
else
return 0; // on stalemate
}
if(bestMove)
{
info->bestScore = bestScore;
info->bestMove = bestMove;
}
return alpha;
}
static inline void SearchPosition(CHESSBOARD *board, SEARCH *info, int depth)
{
for(int currentDepth = 1; currentDepth <= depth; currentDepth++)
{
NegaMaxSearch(-50000, 50000, board, info, currentDepth);
printf("info score cp %d depth %d nodes %ld\n", info->bestScore, currentDepth, info->nodes);
}
if(info->bestMove)
{
printf("bestmove ");
PrintMove(info->bestMove);
printf("\n");
}
}
white: bestmove e2e4
black: bestmove e7e5
and so on until reach the given depth?
I've spent more then 12 hours researching this subject but feel completely lost due to the lack of basis. Thanks in advance!