-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
37 lines (30 loc) · 757 Bytes
/
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
package game
import (
"errors"
ttt "github.com/shurcooL/tictactoe"
)
// Game represents a game
type Game struct {
ID string `json:"id" example:"123123"`
LastMoveBy uint8 `json:"last_move_by" enums:"1,2" example:"1"`
BoardCells [9]uint8 `json:"board_cells" example:"0,1,0,2,2,0,1,1,0"`
}
// Validate validates the game
func (g *Game) Validate() error {
if g.ID == "" {
return errors.New("invalid game")
}
if ttt.State(g.LastMoveBy) != ttt.O && ttt.State(g.LastMoveBy) != ttt.X {
return errors.New("invalid state")
}
// check each cell in board
for _, cell := range g.BoardCells {
switch ttt.State(cell) {
case ttt.O, ttt.X, ttt.F:
// we are good
default:
return errors.New("invalid board")
}
}
return nil
}