-
Notifications
You must be signed in to change notification settings - Fork 4
/
board.go
179 lines (155 loc) · 3.17 KB
/
board.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"errors"
"fmt"
)
// OutOfRangeError tells if the given position is out of range.
type OutOfRangeError error
// N is the amount of cells per row and amount of columns.
const N int = 6
// Cell contains the state of one board cell.
type Cell int
const (
// Empty indicates that the cell is empty.
Empty Cell = -1
// OO is an O.
OO Cell = 0
// XX is an X.
XX Cell = 1
)
// GameRole indicates the type the player is
type GameRole int
const (
// Order gamerole
Order GameRole = 0
// Chaos gamerole
Chaos GameRole = 1
)
func (role GameRole) String() string {
if role == Order {
return "order"
}
return "chaos"
}
// Board contains the state of the game board
type Board struct {
Cells [N * N]Cell
Onturn GameRole
}
// MakeBoard creates a new board
func MakeBoard(startPlayer GameRole) *Board {
bd := &Board{
Onturn: startPlayer,
}
for i := 0; i < N*N; i++ {
bd.Cells[i] = Empty
}
return bd
}
// ApplyMove applies the given move
func (bd *Board) ApplyMove(stone Cell, pos int) error {
if pos < 0 || pos >= N*N {
return OutOfRangeError(errors.New("Pos out of range in ApplyMove"))
}
if stone == Empty {
return errors.New("Cannot set cell to empty in ApplyMove")
}
if bd.Cells[pos] != Empty {
return errors.New("Target cell not empty in ApplyMove")
}
bd.Cells[pos] = stone
if bd.Onturn == Order {
bd.Onturn = Chaos
} else {
bd.Onturn = Order
}
return nil
}
// IsEmpty checks if the cell at the given position is empty.
func (bd *Board) IsEmpty(pos int) (bool, error) {
if pos < 0 || pos >= N*N {
return false, OutOfRangeError(errors.New("Pos out of range in IsEmpty"))
}
return bd.Cells[pos] == Empty, nil
}
// CheckWin checks if the game is over and if so who has won the game.
func (bd *Board) CheckWin() (winner GameRole, gameOver bool) {
full := true
for i := 0; i < N*N; i++ {
if bd.Cells[i] == Empty {
full = false
break
}
}
if full {
return Chaos, true
}
var k int
for i := 0; i < N; i++ {
for j := 0; j < 2; j++ {
stone := bd.Cells[N*i+j] // Horizontal
if stone != Empty {
for k = 1; k < N-1; k++ {
if bd.Cells[N*i+j+k] != stone {
break
}
}
if k == N-1 {
return Order, true
}
}
stone = bd.Cells[N*j+i] // Vertical
if stone != Empty {
for k = 1; k < N-1; k++ {
if bd.Cells[N*j+i+k] != stone {
break
}
}
if k == N-1 {
return Order, true
}
}
}
}
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
stone := bd.Cells[N*i+j] // Diagonal \
if stone != Empty {
for k = 1; k < N-1; k++ {
if bd.Cells[N*(i+j)+j+k] != stone {
break
}
}
if k == N-1 {
return Order, true
}
}
stone = bd.Cells[N*i+N-1-j] // Diagonal /
if stone != Empty {
for k = 1; k < N-1; k++ {
if bd.Cells[N*(i+j)+N-1-j-k] != stone {
break
}
}
if k == N-1 {
return Order, true
}
}
}
}
return GameRole(-1), false // Nobody
}
func (bd *Board) printBoard() {
for y := 0; y < N; y++ {
for x := 0; x < N; x++ {
if bd.Cells[N*y+x] == OO {
fmt.Print("O ")
} else if bd.Cells[N*y+x] == XX {
fmt.Print("X ")
} else {
fmt.Print(". ")
}
}
fmt.Print("\n")
}
}