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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ _testmain.go

# Benchmarking
cpu.out
.idea
.idea
stockfish
10 changes: 8 additions & 2 deletions uci/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,19 @@
if len(parts) <= 1 {
return errors.New("best move not found " + text)
}
bestMove, err := chess.UCINotation{}.Decode(nil, parts[1])
var position *chess.Position
if e.position != nil {
position = e.position.Position
} else {
position = nil
}
bestMove, err := chess.UCINotation{}.Decode(position, parts[1])

Check warning on line 304 in uci/cmd.go

View check run for this annotation

Codecov / codecov/patch

uci/cmd.go#L298-L304

Added lines #L298 - L304 were not covered by tests
if err != nil {
return err
}
results.BestMove = bestMove
if len(parts) >= maxParts {
ponderMove, decodeErr := chess.UCINotation{}.Decode(nil, parts[3])
ponderMove, decodeErr := chess.UCINotation{}.Decode(position, parts[3])

Check warning on line 310 in uci/cmd.go

View check run for this annotation

Codecov / codecov/patch

uci/cmd.go#L310

Added line #L310 was not covered by tests
if decodeErr != nil {
return decodeErr
}
Expand Down
27 changes: 17 additions & 10 deletions uci/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
// Engine represents a UCI compliant chess engine (e.g. Stockfish, Shredder, etc.).
// Engine is safe for concurrent use.
type Engine struct {
cmd *exec.Cmd
in *io.PipeWriter
out *io.PipeReader
logger *log.Logger
id map[string]string
options map[string]Option
mu *sync.RWMutex
results SearchResults
debug bool
cmd *exec.Cmd
in *io.PipeWriter
out *io.PipeReader
logger *log.Logger
id map[string]string
options map[string]Option
mu *sync.RWMutex
position *CmdPosition
results SearchResults
debug bool
}

// Debug is an option for the New function to add logging for debugging. This will
Expand Down Expand Up @@ -51,7 +52,7 @@
cmd := exec.Command(path)
cmd.Stdin = rIn
cmd.Stdout = wOut
e := &Engine{cmd: cmd, in: wIn, out: rOut, mu: &sync.RWMutex{}, logger: log.New(os.Stdout, "uci", log.LstdFlags)}
e := &Engine{cmd: cmd, in: wIn, out: rOut, mu: &sync.RWMutex{}, logger: log.New(os.Stdout, "uci", log.LstdFlags), position: &CmdPosition{}}

Check warning on line 55 in uci/engine.go

View check run for this annotation

Codecov / codecov/patch

uci/engine.go#L55

Added line #L55 was not covered by tests
for _, opt := range opts {
opt(e)
}
Expand Down Expand Up @@ -169,6 +170,12 @@
if _, err := fmt.Fprintln(e.in, cmd.String()); err != nil {
return err
}
if posCmd, ok := cmd.(*CmdPosition); ok {
e.position = posCmd
}
if posCmd, ok := cmd.(CmdPosition); ok {
e.position = &posCmd
}

Check warning on line 178 in uci/engine.go

View check run for this annotation

Codecov / codecov/patch

uci/engine.go#L173-L178

Added lines #L173 - L178 were not covered by tests
if err := cmd.ProcessResponse(e); err != nil {
return err
}
Expand Down
36 changes: 36 additions & 0 deletions uci/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uci_test

import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -22,6 +23,41 @@ func init() {
StockfishPath = filepath.Join(dir, "..", "stockfish")
}

func Test_UCIMovesTags(t *testing.T) {
t.SkipNow()
eng, err := uci.New(StockfishPath, uci.Debug)
if err != nil {
t.Fatal(err)
}
defer eng.Close()
setOpt := uci.CmdSetOption{Name: "UCI_Elo", Value: "1500"}
setPos := uci.CmdPosition{Position: chess.StartingPosition()}
setGo := uci.CmdGo{MoveTime: time.Second / 10}
if err := eng.Run(uci.CmdUCI, uci.CmdIsReady, setOpt, uci.CmdUCINewGame, setPos, setGo); err != nil {
t.Fatal("failed to run command", err)
}

game := chess.NewGame()
notation := chess.AlgebraicNotation{}

for game.Outcome() == chess.NoOutcome {
cmdPos := uci.CmdPosition{Position: game.Position()}
cmdGo := uci.CmdGo{MoveTime: time.Second / 100}
if err2 := eng.Run(cmdPos, cmdGo); err2 != nil {
t.Fatal("failed to run command", err2)
}

move := eng.SearchResults().BestMove
pos := game.Position()
san := notation.Encode(pos, move)

err = game.PushMove(san, nil)
if err != nil {
t.Fatal(fmt.Sprintf("failed to push move %s - %s - %v. Pos: %s", san, move.String(), move.HasTag(chess.Capture), pos.String()), err)
}
}
}

func TestLogger(t *testing.T) {
t.SkipNow()

Expand Down
Loading