I was following a tutorial for making a chess engine and in that, while writing the alpha beta pruning function the tutorial before checking for all possible moves generated , checks weather the current side is in check or not. If in check the depth was increased and i couldn't understand the reason. I am fairly new to chess engine programming and have searched about this but couldn't find anything relevant. Any help will be much appreciated.Thanks.
Here is the code for alpha beta pruning.
function AlphaBeta(depth, alpha, beta)
{
SearchController.nodes++;
if (depth <= 0)
{
return Evaluate();
}
//fiftyMove rule or if a position is Repeated
if ((IsRepeated() || GameBoard.fiftyMove >= 100) && GameBoard.hisPly != 0)
return 0;
if (GameBoard.ply > MAXDEPTH - 1 )
return Evaluate();
//this is the line i am talking about
var InCheck = SqAttacked(GameBoard.pieceList[Kings[GameBoard.side] * 10] , GameBoard.side ^ 1 );
if (InCheck) {
depth++; //<---------HERE
}
var score = -INFINITE;
GenerateMoves();
var Move = NOMOVE, MoveNum = 0, BestMove = NOMOVE;
var legal = 0; // To see weather any legal moves are possible
var start = GameBoard.moveListStart[GameBoard.ply], end = GameBoard.moveListStart[GameBoard.ply + 1];
for(MoveNum = start; MoveNum < end; MoveNum++)
{
Move = GameBoard.moveList[MoveNum];
if (!MakeMove(Move))
continue;
legal++;
score = -AlphaBeta(depth - 1, -beta, -alpha);
TakeMoveBack();
if (score > alpha)
{
if(score >= beta)
return beta;
alpha = score;
BestMove = Move;
}
console.log('For move : ' + PrMove(Move) + ' Score : ' + score );
}
if (BestMove != NOMOVE)
{
StorePvMove(BestMove);
}
if (legal == 0)
{
if (SqAttacked(GameBoard.pieceList[Kings[GameBoard.side] * 10] , GameBoard.side ^ 1))
{
return MATE - GameBoard.ply;
}
else return 0;
}
console.log("");
console.log("Best Move at Height : " + depth + ' Move : ' + PrMove(BestMove) + ' Score : ' + alpha);
return alpha;
}