forked from beldur/libaduk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractboard.go
289 lines (238 loc) · 7.7 KB
/
abstractboard.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package libaduk
import (
"fmt"
"log"
)
// Represents a Go board data structure
type AbstractBoard struct {
BoardSize uint8
data []BoardStatus
undoStack []*Move
zobrist *ZobristHash
}
// Creates new Go Board
func NewBoard(boardSize uint8) (*AbstractBoard, error) {
if boardSize < 1 {
return nil, fmt.Errorf("Boardsize can not be less than 1!")
}
return &AbstractBoard{
boardSize,
make([]BoardStatus, boardSize*boardSize),
make([]*Move, 0),
NewZobristHash(boardSize),
}, nil
}
// Returns a string representation of the current board status
func (board *AbstractBoard) ToString() string {
result := ""
for y := uint8(0); y < board.BoardSize; y++ {
for x := uint8(0); x < board.BoardSize; x++ {
switch board.getStatus(x, y) {
case EMPTY:
result += ". "
case BLACK:
result += "X "
case WHITE:
result += "O "
}
}
result += "\n"
}
return result
}
// Clears the board
func (board *AbstractBoard) Clear() {
for i := 0; i < len(board.data); i++ {
board.data[i] = EMPTY
}
board.zobrist.hash = 0
board.undoStack = []*Move{}
}
// Returns the Top Move of the Undostack
func (board *AbstractBoard) UndostackTopMove() *Move {
return board.undoStack[len(board.undoStack)-1]
}
// Removes last Move from Undostack
func (board *AbstractBoard) UndostackPop() (move *Move) {
if len(board.undoStack) > 0 {
move = board.undoStack[len(board.undoStack)-1]
board.undoStack = board.undoStack[:len(board.undoStack)-1]
}
return
}
// Adds the given Move to the Undostack
func (board *AbstractBoard) UndostackPush(move *Move) {
board.undoStack = append(board.undoStack, move)
}
// Adds a Pass to the Undostack
func (board *AbstractBoard) UndostackPushPass() {
board.UndostackPush(&Move{255, 255, PASS, nil})
}
// Undo `count` moves on the board
func (board *AbstractBoard) Undo(count int) {
for i := 0; i < count; i++ {
if len(board.undoStack) > 0 {
move := board.UndostackPop()
// Remove stone from the board and update hash
if move.Color == BLACK || move.Color == WHITE {
board.zobrist.Hash(move.X, move.Y, move.Color)
board.setStatus(move.X, move.Y, EMPTY)
}
// Add captures back to board if necessary and update hash
for _, capture := range move.Captures {
board.zobrist.Hash(capture.X, capture.Y, move.Color.invert())
board.setStatus(capture.X, capture.Y, move.Color.invert())
}
}
}
}
// Returns current board hash value
func (board *AbstractBoard) GetHash() int64 {
return board.zobrist.GetHash()
}
// Play move on board
func (board *AbstractBoard) PlayMove(move Move) error {
return board.Play(move.X, move.Y, move.Color)
}
// Play stone at given position
func (board *AbstractBoard) Play(x uint8, y uint8, color BoardStatus) error {
log.Printf("Play: X: %v, Y: %v, Color: %v", x, y, color)
// Is move on the board?
if x < 0 || x >= board.BoardSize || y < 0 || y >= board.BoardSize {
return fmt.Errorf("Invalid move position!")
}
// Is already a stone on this position?
if board.getStatus(x, y) != EMPTY {
return fmt.Errorf("Position already occupied!")
}
// Check if move is legal and get captures
captures, err := board.legal(x, y, color)
if err != nil {
return err
}
// Remove captures
for _, capture := range captures {
board.zobrist.Hash(capture.X, capture.Y, color.invert())
board.setStatus(capture.X, capture.Y, EMPTY)
}
// Add them to undostack
board.UndostackPush(&Move{x, y, color, captures})
return nil
}
// Checks if move is legal and returns captured stones if necessary
func (board *AbstractBoard) legal(x uint8, y uint8, color BoardStatus) (captures []Position, err error) {
captures = []Position{}
neighbours := board.getNeighbours(x, y)
// Check if we capture neighbouring stones
for _, neighbour := range neighbours {
// Is neighbour from another color?
if board.getStatus(neighbour.X, neighbour.Y) == color.invert() {
// Get enemy stones with no liberties left
noLibertyStones := board.getNoLibertyStones(neighbour.X, neighbour.Y, Position{x, y})
for _, noLibertyStone := range noLibertyStones {
captures = append(captures, noLibertyStone)
}
}
}
// Place stone on the board and update hash
board.zobrist.Hash(x, y, color)
board.setStatus(x, y, color)
// TODO: Delete Duplicates necessary????
if len(captures) > 0 {
return
}
// Check if the played move has no liberties and therefore is a suicide
selfNoLiberties := board.getNoLibertyStones(x, y, Position{})
if len(selfNoLiberties) > 0 {
// Take move back
board.zobrist.Hash(x, y, color)
board.setStatus(x, y, EMPTY)
err = fmt.Errorf("Invalid move (Suicide not allowed)!")
}
log.SetPrefix("")
return
}
// Get all stones with no liberties left on given position
func (board *AbstractBoard) getNoLibertyStones(x uint8, y uint8, orgPosition Position) (noLibertyStones []Position) {
log.Printf("Get no liberty stones for (%d, %d)", x, y)
noLibertyStones = []Position{}
newlyFoundStones := []Position{Position{x, y}}
foundNew := true
var groupStones []Position = nil
// Search until no new stones are found
for foundNew == true {
foundNew = false
groupStones = []Position{}
for _, newlyFoundStone := range newlyFoundStones {
neighbours := board.getNeighbours(newlyFoundStone.X, newlyFoundStone.Y)
// Check liberties of stone newlyFoundStone.X, newlyFoundStone.Y by checking the neighbours
for _, neighbour := range neighbours {
nbX := neighbour.X
nbY := neighbour.Y
// Has newlyFoundStone a free liberty?
if board.getStatus(nbX, nbY) == EMPTY && !neighbour.isSamePosition(orgPosition) {
// Neighbour is empty and not origPosition so newlyFoundStone has at least one liberty
return noLibertyStones[:0]
} else {
// Is the neighbour of newlyFoundStone.X, newlyFoundStone.Y the same color? Then we have a group here
if board.getStatus(newlyFoundStone.X, newlyFoundStone.Y) == board.getStatus(nbX, nbY) {
foundNewHere := true
nbGroupStone := Position{nbX, nbY}
log.Printf("Found group stone for (%d, %d) at %+v", newlyFoundStone.X, newlyFoundStone.Y, nbGroupStone)
// Check if found stone is already in our group list
for _, groupStone := range groupStones {
if groupStone.isSamePosition(nbGroupStone) {
foundNewHere = false
break
}
}
// Check if found stone is already in result set list
if foundNewHere {
for _, noLibertyStone := range noLibertyStones {
if noLibertyStone.isSamePosition(nbGroupStone) {
foundNewHere = false
break
}
}
}
// If groupStone is not known yet, add it
if foundNewHere {
groupStones = append(groupStones, nbGroupStone)
foundNew = true
}
}
}
}
}
// Add newly found stones to the resultset
noLibertyStones = append(noLibertyStones, newlyFoundStones...)
// Now check the found group stones
newlyFoundStones = groupStones
}
log.Printf("Found these stones with no liberties: %+v", noLibertyStones)
return
}
// Returns the neighbour array positions for a given point
func (board *AbstractBoard) getNeighbours(x uint8, y uint8) (neighbourIndexes []Position) {
neighbourIndexes = []Position{}
// Check for board borders
if x > 0 {
neighbourIndexes = append(neighbourIndexes, Position{(x - 1), y})
}
if x < board.BoardSize-1 {
neighbourIndexes = append(neighbourIndexes, Position{(x + 1), y})
}
if y > 0 {
neighbourIndexes = append(neighbourIndexes, Position{x, y - 1})
}
if y < board.BoardSize-1 {
neighbourIndexes = append(neighbourIndexes, Position{x, y + 1})
}
return
}
func (board *AbstractBoard) getStatus(x uint8, y uint8) BoardStatus {
return board.data[board.BoardSize*x+y]
}
func (board *AbstractBoard) setStatus(x uint8, y uint8, status BoardStatus) {
board.data[board.BoardSize*x+y] = status
}