Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions uci/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -118,6 +119,29 @@ var (
CmdQuit = cmdNoOptions{Name: "quit", F: func(_ *Engine) error {
return nil
}}

// CmdEval is a non-standard command that requests the engine's static evaluation of the current position.
CmdEval = cmdNoOptions{Name: "eval", F: func(e *Engine) error {
scanner := bufio.NewScanner(e.out)
for scanner.Scan() {
text := e.readLine(scanner)
if strings.Contains(text, "error") {
return errors.New("eval command not supported")
}
if strings.HasPrefix(text, "Final evaluation") {
parts := strings.Fields(text)
if len(parts) >= 3 {
evalStr := parts[2]
eval, err := strconv.ParseFloat(evalStr, 64)
if err == nil {
e.eval = int(math.Round(eval * 100))
}
break
}
}
}
return nil
}}
)

// CmdSetOption corresponds to the "setoption" command:
Expand Down
7 changes: 7 additions & 0 deletions uci/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Engine struct {
mu *sync.RWMutex
position *CmdPosition
results SearchResults
eval int
debug bool
}

Expand Down Expand Up @@ -124,6 +125,12 @@ func (e *Engine) SearchResults() SearchResults {
return e.results
}

func (e *Engine) Eval() int {
e.mu.RLock()
defer e.mu.RUnlock()
return e.eval
}

// Run runs the set of Cmds in the order given and returns an error if
// any of the commands fails. Except for CmdStop (usually paired with
// CmdGo's infinite option) all commands block via mutux until completed.
Expand Down
40 changes: 40 additions & 0 deletions uci/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ func isEngineAvailable(engine string) bool {
return err == nil
}

func Test_EngineEval(t *testing.T) {
for _, name := range engines {
fenStr := "4k3/8/8/8/8/8/8/4K2R w - - 0 1"

t.Run("EngineEval_"+name, func(t *testing.T) {
if !isEngineAvailable(name) {
t.Skipf("engine %s not available", name)
}

pos := &chess.Position{}
if err := pos.UnmarshalText([]byte(fenStr)); err != nil {
t.Fatal("failed to parse FEN", err)
}

eng, err := uci.New(name, uci.Debug)
if err != nil {
t.Fatal(err)
}
defer eng.Close()

cmdPos := uci.CmdPosition{Position: pos}
err = eng.Run(uci.CmdUCI, uci.CmdIsReady, uci.CmdUCINewGame, cmdPos, uci.CmdEval)

if name == "stockfish" {
if err != nil {
t.Fatal("failed to run command", err)
}

if eng.Eval() < 500 {
t.Errorf("expected an eval greater than or equal to 500, got %d", eng.Eval())
}
} else if name == "lc0" {
if err == nil {
t.Fatal("expected an error", err)
}
}
})
}
}

func Test_EngineInfo(t *testing.T) {
for _, name := range engines {
fenStr := "r1bq1rk1/ppp2ppp/2n2n2/3pp3/3P4/2P1PN2/PP1N1PPP/R1BQ1RK1 w - - 0 8"
Expand Down
Loading