Skip to content

Commit

Permalink
rosaline+search: return SearchResults instead of a ScoredMove from se…
Browse files Browse the repository at this point in the history
…arch
  • Loading branch information
e0ff committed Nov 25, 2023
1 parent 36cc95d commit 657a0de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
12 changes: 6 additions & 6 deletions cmd/rosaline/interfaces/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ func (i cliInterface) Loop() {
}
}

move := i.searcher.Search(position, depth)
fmt.Println("best move:", move)
fmt.Println("score:", move.Score)
results := i.searcher.Search(position, depth)
fmt.Println("best move:", results.BestMove)
fmt.Println("score:", results.Score)
} else if cmd == "evaluate" {
score := i.evaluator.Evaluate(position)
fmt.Println("score:", score)
} else if cmd == "play" {
move := i.searcher.Search(position, DefaultDepth)
position.MakeMove(move.Move)
fmt.Println("played:", move.Move)
results := i.searcher.Search(position, DefaultDepth)
position.MakeMove(results.BestMove)
fmt.Println("played:", results.BestMove)
} else if cmd == "fen" {
fmt.Println(position.Fen())
} else if cmd == "setfen" {
Expand Down
6 changes: 3 additions & 3 deletions cmd/rosaline/interfaces/uci.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ loop:
position.MakeUciMove(lastMove)
break
case "go":
move := i.searcher.Search(position, DefaultDepth)
position.MakeMove(move.Move)
fmt.Println("bestmove", move)
results := i.searcher.Search(position, DefaultDepth)
position.MakeMove(results.BestMove)
fmt.Println("bestmove", results.BestMove)
break
case "quit":
break loop
Expand Down
32 changes: 24 additions & 8 deletions internal/search/negamax.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ const (
maxNumberKillerMoves = 128
)

type SearchResults struct {
BestMove chess.Move
Score int
Depth int
Nodes int
}

type NegamaxSearcher struct {
evaluator evaluation.Evaluator
drawTable drawTable

killerMoves map[chess.Color][]chess.Move
killerMoveIndex int

nodes int
}

func NewNegamaxSearcher(evaluator evaluation.Evaluator) NegamaxSearcher {
Expand All @@ -29,29 +38,34 @@ func NewNegamaxSearcher(evaluator evaluation.Evaluator) NegamaxSearcher {
drawTable: newDrawTable(),
killerMoves: make(map[chess.Color][]chess.Move),
killerMoveIndex: 0,
nodes: 0,
}
}

func (s NegamaxSearcher) Search(position chess.Position, depth int) ScoredMove {
func (s NegamaxSearcher) Search(position chess.Position, depth int) SearchResults {
s.nodes = 0

moves := position.GenerateMoves(chess.LegalMoveGeneration)
bestMove := ScoredMove{}
bestMove := chess.Move{}
bestScore := math.MinInt + 1

for _, move := range moves {
position.MakeMove(move)

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

if score > bestScore {
bestScore = score
bestMove = scoredMove
bestMove = move
}

position.Undo()
}

return bestMove
return SearchResults{
BestMove: bestMove,
Score: bestScore,
Depth: depth,
Nodes: s.nodes,
}
}

func (s NegamaxSearcher) scoreMove(position chess.Position, move chess.Move) int {
Expand Down Expand Up @@ -91,6 +105,8 @@ func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int,
return s.evaluator.AbsoluteEvaluation(position)
}

s.nodes++

slices.SortFunc(moves, func(m1, m2 chess.Move) int {
return cmp.Compare(s.scoreMove(position, m1), s.scoreMove(position, m2))
})
Expand Down
20 changes: 0 additions & 20 deletions internal/search/scoredmove.go

This file was deleted.

0 comments on commit 657a0de

Please sign in to comment.