Wait to send quit command
This commit is contained in:
parent
1945642193
commit
a9b97a1c25
1 changed files with 17 additions and 4 deletions
21
gnubg.go
21
gnubg.go
|
@ -8,6 +8,7 @@ import (
|
|||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"code.rocket9labs.com/tslocum/bgammon"
|
||||
)
|
||||
|
@ -149,7 +150,7 @@ func parseMoves(g *bgammon.Game, moves []byte) ([4][2]int8, error) {
|
|||
}
|
||||
|
||||
if to == bgammon.SpaceHomePlayer && bgammon.PlayerCheckers(Game.Game.Board[from], 1) == 0 {
|
||||
for space := from; space > 1; space-- {
|
||||
for space := from - 1; space > 1; space-- {
|
||||
if bgammon.PlayerCheckers(Game.Game.Board[space], 1) != 0 {
|
||||
from = space
|
||||
break
|
||||
|
@ -159,7 +160,7 @@ func parseMoves(g *bgammon.Game, moves []byte) ([4][2]int8, error) {
|
|||
|
||||
for j := 0; j < count; j++ {
|
||||
m[mc][0], m[mc][1] = int8(from), int8(to)
|
||||
ok, _ := Game.AddMoves([][]int8{{int8(from), int8(to)}}, true)
|
||||
ok := Game.AddLocalMove([]int8{int8(from), int8(to)})
|
||||
if !ok {
|
||||
return [4][2]int8{}, fmt.Errorf("failed to add local moves (%d/%d): %s", from, to, moves)
|
||||
}
|
||||
|
@ -206,11 +207,14 @@ func analyze(g *bgammon.Game) ([4][2]int8, error) {
|
|||
}
|
||||
|
||||
var moves []byte
|
||||
var setDice bool
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
buf := scanner.Bytes()
|
||||
if bytes.Contains(buf, []byte("gnubg moves")) {
|
||||
if bytes.Contains(buf, []byte("The dice have been set")) {
|
||||
setDice = true
|
||||
} else if setDice && bytes.Contains(buf, []byte("gnubg moves")) {
|
||||
moves = buf
|
||||
}
|
||||
log.Println(string(buf))
|
||||
|
@ -224,7 +228,16 @@ func analyze(g *bgammon.Game) ([4][2]int8, error) {
|
|||
}
|
||||
}()
|
||||
|
||||
stdin.Write([]byte(fmt.Sprintf("set automatic roll off\nset automatic move off\nnew game\nset board %s\nset turn 0\nset dice %d %d\nplay\nquit\ny\n", gnubgPosition(g), g.Roll1, g.Roll2)))
|
||||
stdin.Write([]byte(fmt.Sprintf("set automatic roll off\nset automatic move off\nnew game\nset board %s\nset turn 0\nset dice %d %d\nplay\n", gnubgPosition(g), g.Roll1, g.Roll2)))
|
||||
t := time.NewTicker(50 * time.Millisecond)
|
||||
for {
|
||||
<-t.C
|
||||
if moves != nil {
|
||||
stdin.Write([]byte("quit\ny\n"))
|
||||
t.Stop()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue