From 92fec32765839e83ce98f1491b81a386b9915229 Mon Sep 17 00:00:00 2001 From: Jeremy Hertel Date: Sun, 26 Nov 2023 15:34:59 -0600 Subject: [PATCH] everywhere: pass Position by reference to search and evaluation --- cmd/rosaline/interfaces/cli.go | 6 +++--- cmd/rosaline/interfaces/uci.go | 2 +- internal/evaluation/evaluator.go | 8 ++++---- internal/evaluation/evaluator_test.go | 2 +- internal/search/negamax.go | 8 ++++---- internal/search/negamax_test.go | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cmd/rosaline/interfaces/cli.go b/cmd/rosaline/interfaces/cli.go index e0856a5..ed0588a 100644 --- a/cmd/rosaline/interfaces/cli.go +++ b/cmd/rosaline/interfaces/cli.go @@ -83,15 +83,15 @@ func (i cliInterface) Loop() { } } - results := i.searcher.Search(position, depth) + results := i.searcher.Search(&position, depth) fmt.Println("best move:", results.BestMove) fmt.Println("score:", results.Score) fmt.Println("elapsed:", results.Time.Seconds()) } else if cmd == "evaluate" { - score := i.evaluator.Evaluate(position) + score := i.evaluator.Evaluate(&position) fmt.Println("score:", score) } else if cmd == "play" { - results := i.searcher.Search(position, DefaultDepth) + results := i.searcher.Search(&position, DefaultDepth) position.MakeMove(results.BestMove) fmt.Println("played:", results.BestMove) } else if cmd == "fen" { diff --git a/cmd/rosaline/interfaces/uci.go b/cmd/rosaline/interfaces/uci.go index ae3eeaa..d6baebf 100644 --- a/cmd/rosaline/interfaces/uci.go +++ b/cmd/rosaline/interfaces/uci.go @@ -57,7 +57,7 @@ loop: var bestScore int = math.MinInt for depth := 1; depth <= 4; depth++ { - results := i.searcher.Search(position, depth) + results := i.searcher.Search(&position, depth) if i.searcher.Stopped() { break diff --git a/internal/evaluation/evaluator.go b/internal/evaluation/evaluator.go index 2e5521d..afcd948 100644 --- a/internal/evaluation/evaluator.go +++ b/internal/evaluation/evaluator.go @@ -15,7 +15,7 @@ func NewEvaluator() Evaluator { // - The value of it's pieces. // - The location of it's pieces. // - Bonuses/penalties for advantageous/disadvantageous positions. -func (e Evaluator) evaluateSide(position chess.Position, color chess.Color) int { +func (e Evaluator) evaluateSide(position *chess.Position, color chess.Color) int { score := 0 colorBB := position.GetColorBB(color) @@ -54,7 +54,7 @@ func (e Evaluator) evaluateSide(position chess.Position, color chess.Color) int } // getSquareScore returns the score for the given piece being on the give square. -func (e Evaluator) getSquareScore(position chess.Position, piece chess.Piece, square chess.Square) int { +func (e Evaluator) getSquareScore(position *chess.Position, piece chess.Piece, square chess.Square) int { if position.Phase() == chess.EndgamePhase { scoreBoard, ok := openingScores[piece] if ok { @@ -76,7 +76,7 @@ func (e Evaluator) getSquareScore(position chess.Position, piece chess.Piece, sq // - positive: if white is winning // - zero: if the position is a draw // - negative: if black is winning -func (e Evaluator) Evaluate(position chess.Position) int { +func (e Evaluator) Evaluate(position *chess.Position) int { if position.IsDraw() { return DrawScore } @@ -100,7 +100,7 @@ func (e Evaluator) Evaluate(position chess.Position) int { // - positive: if the player to move is winning // - zero: if the position is a draw // - negative: if the player to move is losing -func (e Evaluator) AbsoluteEvaluation(position chess.Position) int { +func (e Evaluator) AbsoluteEvaluation(position *chess.Position) int { evaluation := e.Evaluate(position) return evaluation * evaluationMultiplier(position.Turn()) } diff --git a/internal/evaluation/evaluator_test.go b/internal/evaluation/evaluator_test.go index 837a92d..f5f8f41 100644 --- a/internal/evaluation/evaluator_test.go +++ b/internal/evaluation/evaluator_test.go @@ -10,7 +10,7 @@ func evaluateBenchmark(b *testing.B, fen string) { evaluator := NewEvaluator() for i := 0; i < b.N; i++ { - evaluator.Evaluate(position) + evaluator.Evaluate(&position) } } diff --git a/internal/search/negamax.go b/internal/search/negamax.go index 82408e2..a5552b3 100644 --- a/internal/search/negamax.go +++ b/internal/search/negamax.go @@ -47,7 +47,7 @@ func NewNegamaxSearcher(evaluator evaluation.Evaluator) NegamaxSearcher { } } -func (s NegamaxSearcher) Search(position chess.Position, depth int) SearchResults { +func (s NegamaxSearcher) Search(position *chess.Position, depth int) SearchResults { s.nodes = 0 s.stop = false @@ -90,7 +90,7 @@ func (s NegamaxSearcher) Search(position chess.Position, depth int) SearchResult } } -func (s NegamaxSearcher) scoreMove(position chess.Position, move chess.Move) int { +func (s NegamaxSearcher) scoreMove(position *chess.Position, move chess.Move) int { turn := position.Turn() if slices.Contains(s.killerMoves[turn], move) { return 1000 @@ -99,7 +99,7 @@ func (s NegamaxSearcher) scoreMove(position chess.Position, move chess.Move) int return 0 } -func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int, depth int, ply int, extensions int) int { +func (s *NegamaxSearcher) doSearch(position *chess.Position, alpha int, beta int, depth int, ply int, extensions int) int { if s.stop { return 0 } @@ -196,7 +196,7 @@ func (s *NegamaxSearcher) doSearch(position chess.Position, alpha int, beta int, return alpha } -func (s NegamaxSearcher) quiescence(position chess.Position, alpha int, beta int) int { +func (s NegamaxSearcher) quiescence(position *chess.Position, alpha int, beta int) int { evaluation := s.evaluator.AbsoluteEvaluation(position) if evaluation >= beta { return beta diff --git a/internal/search/negamax_test.go b/internal/search/negamax_test.go index 3f6bae1..3bd3619 100644 --- a/internal/search/negamax_test.go +++ b/internal/search/negamax_test.go @@ -12,6 +12,6 @@ func BenchmarkNegamax(b *testing.B) { searcher := NewNegamaxSearcher(evaluator) for i := 0; i < b.N; i++ { - searcher.Search(position, 4) + searcher.Search(&position, 4) } }