-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
85 lines (74 loc) · 1.31 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
package main
import (
"errors"
"fmt"
)
type Game struct {
Board Board `json:"board"`
Turn Piece `json:"turn"`
Komi float32 `json:"komi"`
}
type MoveResult int
const (
Ok = iota
Illegal
Komi
GameOver
)
type GameResult int
const (
WhiteWins = iota
BlackWins
Draw
)
func CreateGame(size int, komi float32) *Game {
board := MakeBoard(size)
game := &Game{
*board,
White,
komi,
}
return game
}
func (game *Game) getMove() (move *Move, err error) {
var x int
var y int
fmt.Printf(game.Board.String(false))
fmt.Printf("%s's turn: ", game.Turn)
_, err = fmt.Scanf("%d %d", &x, &y)
if err != nil {
return nil, errors.New("Invalid move: should be: x, y")
}
return &Move{x, y, game.Turn}, nil
}
func (game *Game) Move(move *Move) (MoveResult, error) {
if move.piece != game.Turn {
return Illegal, errors.New("Not your turn")
}
err := game.Board.Move(move)
if err != nil {
// TODO komi r
return Illegal, err
}
if game.Turn == White {
game.Turn = Black
} else {
game.Turn = White
}
return Ok, nil
}
func (game *Game) Start() {
gameOver := false
for !gameOver {
move, err := game.getMove()
if err != nil {
fmt.Printf("Invalid move: %s", err.Error())
continue
}
result, err := game.Move(move)
if result != Ok {
fmt.Printf("Illegal move. Try again!\n")
continue
}
}
}