Skip to content

Commit

Permalink
everywhere: pass Position by reference to search and evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 26, 2023
1 parent 5c7ccff commit 92fec32
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/rosaline/interfaces/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/rosaline/interfaces/uci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/evaluation/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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())
}
2 changes: 1 addition & 1 deletion internal/evaluation/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/search/negamax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/search/negamax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 92fec32

Please sign in to comment.