Skip to content

Commit

Permalink
everywhere: remove Engine struct and use evaluator and searcher directly
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 22, 2023
1 parent 16ff8c2 commit 249d7d0
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 91 deletions.
43 changes: 27 additions & 16 deletions cmd/rosaline/interfaces/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@ import (
"fmt"
"os"
"rosaline/internal/chess"
"rosaline/internal/engine"
"rosaline/internal/evaluation"
"rosaline/internal/perft"
"rosaline/internal/search"
"rosaline/internal/utils"
"strconv"
"strings"
)

type cliInterface struct {
searcher search.NegamaxSearcher
evaluator evaluation.Evaluator
}

func NewCliProtocolHandler() cliInterface {
return cliInterface{}
evaluator := evaluation.NewEvaluator()
return cliInterface{
searcher: search.NewNegamaxSearcher(evaluator),
evaluator: evaluator,
}
}

func (i cliInterface) Loop() {
scanner := bufio.NewScanner(os.Stdin)

engine := engine.NewEngine()
engine.NewGame(chess.StartingFen)
position, _ := chess.NewPosition(chess.StartingFen)

for {
fmt.Print(engine.Game.Position.Turn())
fmt.Print(position.Turn())
fmt.Print("> ")

scanner.Scan()
Expand All @@ -35,7 +41,7 @@ func (i cliInterface) Loop() {
if cmd == "quit" {
break
} else if cmd == "display" {
engine.Game.Position.Print()
position.Print()
} else if cmd == "perft" {
depth := 1
if len(args) > 0 {
Expand All @@ -47,9 +53,9 @@ func (i cliInterface) Loop() {
}
}

perft.Perft(engine.Game.Position, depth, true)
perft.Perft(position, depth, true)
} else if cmd == "moves" {
moves := engine.Game.Position.GenerateMoves(chess.LegalMoveGeneration)
moves := position.GenerateMoves(chess.LegalMoveGeneration)
for _, move := range moves {
fmt.Println(move)
}
Expand All @@ -59,14 +65,14 @@ func (i cliInterface) Loop() {
continue
}

err := engine.Game.MakeUciMove(args[0])
err := position.MakeUciMove(args[0])
if err != nil {
fmt.Println(err)
}
} else if cmd == "undo" {
engine.Game.Position.Undo()
position.Undo()
} else if cmd == "go" {
depth := engine.DefaultDepth
depth := DefaultDepth
if len(args) >= 1 {
var err error
depth, err = strconv.Atoi(args[0])
Expand All @@ -76,17 +82,18 @@ func (i cliInterface) Loop() {
}
}

move := engine.Search(depth)
move := i.searcher.Search(position, depth)
fmt.Println("best move:", move)
fmt.Println("score:", move.Score)
} else if cmd == "evaluate" {
score := engine.Evaluate()
score := i.evaluator.Evaluate(position)
fmt.Println("score:", score)
} else if cmd == "play" {
move := engine.PlayBestMove()
move := i.searcher.Search(position, DefaultDepth)
position.MakeMove(move.Move)
fmt.Println("played:", move.Move)
} else if cmd == "fen" {
fmt.Println(engine.Game.Position.Fen())
fmt.Println(position.Fen())
} else if cmd == "setfen" {
if len(args) < 1 {
fmt.Println("setfen requires a fen as an argument")
Expand All @@ -98,10 +105,14 @@ func (i cliInterface) Loop() {
fen = chess.StartingFen
}

err := engine.NewGame(fen)
p, err := chess.NewPosition(fen)
if err != nil {
fmt.Println(err)
continue
}

i.searcher.Reset()
position = p
} else if cmd == "help" {
fmt.Println("display displays the current position")
fmt.Println("fen displays the current positions fen")
Expand Down
5 changes: 5 additions & 0 deletions cmd/rosaline/interfaces/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package interfaces

const (
DefaultDepth = 4
)
23 changes: 16 additions & 7 deletions cmd/rosaline/interfaces/uci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ import (
"fmt"
"os"
"rosaline/internal/chess"
"rosaline/internal/engine"
"rosaline/internal/evaluation"
"rosaline/internal/search"
"rosaline/internal/utils"
)

type uciInterface struct {
searcher search.NegamaxSearcher
evaluator evaluation.Evaluator
}

func NewUciProtocolHandler() uciInterface {
return uciInterface{}
evaluator := evaluation.NewEvaluator()
return uciInterface{
searcher: search.NewNegamaxSearcher(evaluator),
evaluator: evaluator,
}
}

func (i uciInterface) Loop() {
engine := engine.NewEngine()

scanner := bufio.NewScanner(os.Stdin)

position, _ := chess.NewPosition(chess.StartingFen)

loop:
for {
scanner.Scan()
Expand All @@ -36,14 +43,16 @@ loop:
fmt.Println("readyok")
break
case "ucinewgame":
engine.NewGame(chess.StartingFen)
i.searcher.Reset()
position, _ = chess.NewPosition(chess.StartingFen)
break
case "position":
lastMove := args[len(args)-1]
engine.Game.MakeUciMove(lastMove)
position.MakeUciMove(lastMove)
break
case "go":
move := engine.PlayBestMove()
move := i.searcher.Search(position, DefaultDepth)
position.MakeMove(move.Move)
fmt.Println("bestmove", move)
break
case "quit":
Expand Down
67 changes: 0 additions & 67 deletions internal/engine/engine.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package search
import (
"math"
"rosaline/internal/chess"
"rosaline/internal/engine/evaluation"
"rosaline/internal/evaluation"
)

const (
Expand Down
File renamed without changes.

0 comments on commit 249d7d0

Please sign in to comment.