forked from tleyden/checkerlution
/
gamestate.go
68 lines (58 loc) · 1.59 KB
/
gamestate.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
package checkerlution
import (
"encoding/json"
"github.com/couchbaselabs/logg"
)
// data structure that corresponds to the checkers:game json doc
type GameState struct {
Teams []Team `json:"teams"`
ActiveTeam int `json:"activeTeam"`
Number int `json:"number"`
Turn int `json:"turn"`
MoveInterval int `json:"moveInterval"`
}
func NewGameStateFromString(jsonString string) GameState {
gameState := &GameState{}
jsonBytes := []byte(jsonString)
err := json.Unmarshal(jsonBytes, gameState)
if err != nil {
logg.LogError(err)
}
return *gameState
}
type Piece struct {
Location int `json:"location"`
King bool `json:"king"`
Captured bool `json:"captured"`
ValidMoves []ValidMove `json:"validMoves"`
PieceId int
}
type Team struct {
Score int `json:"score"`
ParticipantCount int `json:"participantCount"`
Pieces []Piece `json:"pieces"`
}
type ValidMove struct {
Locations []int `json:"locations"`
Captures []Capture `json:"captures"`
King bool `json:"king"`
PieceId int
StartLocation int
}
type Capture struct {
TeamID int `json:"team"`
PieceId int `json:"piece"`
}
func (t Team) AllValidMoves() (validMoves []ValidMove) {
validMoves = make([]ValidMove, 0)
for pieceIndex, piece := range t.Pieces {
for _, validMove := range piece.ValidMoves {
// enhance the validMove from some information
// from the piece
validMove.StartLocation = piece.Location
validMove.PieceId = pieceIndex
validMoves = append(validMoves, validMove)
}
}
return
}