-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
87 lines (72 loc) · 1.45 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package discordchess
import (
"time"
"github.com/notnil/chess"
"github.com/notnil/chess/uci"
)
type game struct {
*chess.Game
whiteID, blackID string
// TODO: {lpf} this can be used later to bust the game if stuck
createdAt time.Time
lastMoveAt time.Time
drawWhite bool
drawBlack bool
// optional uci engine
eng *uci.Engine
}
func newGame(whiteID, blackID string, initUCI bool) (*game, error) {
var eng *uci.Engine
if initUCI {
e, err := uci.New("stockfish")
if err != nil {
return nil, err
}
if err := e.Run(uci.CmdUCI, uci.CmdIsReady, uci.CmdUCINewGame); err != nil {
return nil, err
}
eng = e
}
g := &game{
whiteID: whiteID,
blackID: blackID,
eng: eng,
createdAt: time.Now().UTC(),
lastMoveAt: time.Now().UTC(),
Game: chess.NewGame(chess.UseNotation(chess.AlgebraicNotation{})),
}
return g, nil
}
func (g *game) Close() {
if g.eng != nil {
g.eng.Close()
}
}
func (g *game) MoveStr(m string) error {
g.drawWhite = false
g.drawBlack = false
g.lastMoveAt = time.Now().UTC()
return g.Game.MoveStr(m)
}
func (g *game) draw(id string) bool {
switch id {
case g.whiteID:
g.drawWhite = true
case g.blackID:
g.drawBlack = true
}
return g.drawWhite && g.drawBlack
}
func (g *game) turn() string {
return g.player(g.Position().Turn())
}
func (g *game) player(c chess.Color) string {
switch c {
case chess.White:
return g.whiteID
case chess.Black:
return g.blackID
default:
return ""
}
}