Skip to content

Commit

Permalink
search: extend search if we are in check
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 23, 2023
1 parent 3b3781c commit 31dac76
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions internal/search/negamax.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s NegamaxSearcher) Search(position chess.Position, depth int) ScoredMove {
for _, move := range moves {
position.MakeMove(move)

score := -s.doSearch(position, initialAlpha, initialBeta, depth-1, 0)
score := -s.doSearch(position, initialAlpha, initialBeta, depth-1, 0, 0)
scoredMove := NewScoredMove(move, score)

if score > bestScore {
Expand All @@ -45,7 +45,7 @@ func (s NegamaxSearcher) Search(position chess.Position, depth int) ScoredMove {
return bestMove
}

func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int, depth int, ply int) int {
func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int, depth int, ply int, extensions int) int {
if s.drawTable.IsRepeat(position.Hash()) {
return evaluation.DrawScore
}
Expand All @@ -54,9 +54,15 @@ func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int,
return evaluation.DrawScore
}

inCheck := position.IsKingInCheck(position.Turn())
if inCheck && extensions < 2 { // limit the number of depth increases we will do to 2
depth++
extensions++
}

moves := position.GenerateMoves(chess.LegalMoveGeneration)
if len(moves) == 0 {
if position.IsKingInCheck(position.Turn()) {
if inCheck {
return -evaluation.MateScore + ply
} else {
return evaluation.DrawScore
Expand All @@ -71,7 +77,7 @@ func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int,
s.drawTable.Push(position.Hash())

position.MakeMove(move)
score := -s.doSearch(position, -beta, -alpha, depth-1, ply+1)
score := -s.doSearch(position, -beta, -alpha, depth-1, ply+1, extensions)
position.Undo()

s.drawTable.Pop()
Expand Down

0 comments on commit 31dac76

Please sign in to comment.