forked from SabakiHQ/Sabaki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardTests.js
276 lines (240 loc) · 10.2 KB
/
boardTests.js
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
const assert = require('assert')
const helper = require('../modules/helper')
const Board = require('../modules/board')
describe('Board', () => {
describe('constructor', () => {
it('should create an empty board', () => {
let board = new Board()
for (let x = 0; x < board.width; x++) {
for (let y = 0; y < board.height; y++) {
assert.equal(board.get([x, y]), 0)
}
}
})
it('should have no capture information', () => {
let board = new Board()
assert.deepEqual(board.captures, {'-1': 0, '1': 0})
})
it('should have no markups and lines', () => {
let board = new Board()
assert.deepEqual(board.lines, [])
assert.deepEqual(board.markups, {})
})
})
describe('clone', () => {
it('should clone board arrangement', () => {
let board = new Board()
;[[0, 1], [1, 0], [1, 2], [2, 0], [2, 2]].forEach(x => board.set(x, 1))
;[[1, 1], [2, 1]].forEach(x => board.set(x, -1))
let clone = board.clone()
assert.deepEqual(board.arrangement, clone.arrangement)
board.set([5, 5], 1)
assert.notDeepEqual(board.arrangement, clone.arrangement)
})
})
describe('hasVertex', () => {
it('should return true when vertex is on board', () => {
let board = new Board()
assert(board.hasVertex([0, 0]))
assert(board.hasVertex([13, 18]))
assert(board.hasVertex([5, 4]))
})
it('should return false when vertex is not on board', () => {
let board = new Board()
assert(!board.hasVertex([-1, -1]))
assert(!board.hasVertex([5, -1]))
assert(!board.hasVertex([board.width, 0]))
assert(!board.hasVertex([board.width, board.height]))
})
})
describe('clear', () => {
it('should clear the stones on the board', () => {
let board = new Board(9, 9)
board.set([0, 0], 1).set([1, 1], -1).set([3, 5], 1)
board.clear()
assert.deepEqual(board.arrangement, new Board(9, 9).arrangement)
})
it('should not clear markups or lines', () => {
let board = new Board(9, 9)
let lines = [
[[0, 0], [8, 8], false],
[[8, 8], [0, 0], true]
]
board.lines = lines
board.markups[[5, 5]] = ['label', 'A']
board.clear()
assert.deepEqual(board.lines, lines)
assert.notDeepEqual(board.markups, new Board(9, 9).markups)
})
})
describe('getDistance', () => {
it('should return the Manhattan distance between two vertices', () => {
let board = new Board()
assert.equal(board.getDistance([1, 2], [8, 4]), 9)
assert.equal(board.getDistance([-1, -2], [8, 4]), 15)
})
})
describe('getCanonicalVertex', () => {
it('should work', () => {
let board = new Board()
assert.deepEqual(board.getCanonicalVertex([3, 4]), [3, 4])
assert.deepEqual(board.getCanonicalVertex([4, 3]), [3, 4])
assert.deepEqual(board.getCanonicalVertex([18, 3]), [0, 3])
assert.deepEqual(board.getCanonicalVertex([15, 5]), [3, 5])
})
})
describe('getSymmetries', () => {
it('should work', () => {
let board = new Board()
assert.deepEqual(
board.getSymmetries([3, 4]).sort(helper.lexicalCompare),
[[3, 4], [3, 14], [4, 3], [4, 15], [14, 3], [14, 15], [15, 4], [15, 14]]
)
assert.deepEqual(
board.getSymmetries([4, 3]).sort(helper.lexicalCompare),
[[3, 4], [3, 14], [4, 3], [4, 15], [14, 3], [14, 15], [15, 4], [15, 14]]
)
assert.deepEqual(
board.getSymmetries([18, 3]).sort(helper.lexicalCompare),
[[0, 3], [0, 3], [0, 15], [0, 15], [3, 0], [3, 0], [15, 0], [15, 0]]
)
assert.deepEqual(
board.getSymmetries([15, 5]).sort(helper.lexicalCompare),
[[3, 5], [3, 13], [5, 3], [5, 15], [13, 3], [13, 15], [15, 5], [15, 13]]
)
})
})
describe('getNeighbors', () => {
it('should return neighbors for vertices in the middle', () => {
let board = new Board()
assert.deepEqual(board.getNeighbors([1, 1]), [[0, 1], [2, 1], [1, 0], [1, 2]])
})
it('should return neighbors for vertices on the side', () => {
let board = new Board()
assert.deepEqual(board.getNeighbors([1, 0]), [[0, 0], [2, 0], [1, 1]])
})
it('should return neighbors for vertices in the corner', () => {
let board = new Board()
assert.deepEqual(board.getNeighbors([0, 0]), [[1, 0], [0, 1]])
})
it('should return empty list for vertices not on board', () => {
let board = new Board()
assert.deepEqual(board.getNeighbors([-1, -1]), [])
})
})
describe('getConnectedComponent', () => {
it('should be able to return the chain of a vertex', () => {
let board = new Board()
;[[0, 1], [1, 0], [1, 2], [2, 0], [2, 2]].forEach(x => board.set(x, 1))
;[[1, 1], [2, 1]].forEach(x => board.set(x, -1))
assert.deepEqual(
board.getConnectedComponent([1, 1], [-1]).sort(helper.lexicalCompare),
[[1, 1], [2, 1]]
)
})
it('should be able to return the stone connected component of a vertex', () => {
let board = new Board()
;[[0, 1], [1, 0], [1, 2], [2, 0], [2, 2]].forEach(x => board.set(x, 1))
;[[1, 1], [2, 1]].forEach(x => board.set(x, -1))
assert.deepEqual(
board.getConnectedComponent([1, 1], [-1, 1]).sort(helper.lexicalCompare),
[[0, 1], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
)
})
})
describe('getLiberties', () => {
it('should return the liberties of the chain of the given vertex', () => {
let board = new Board()
;[[1, 1], [2, 1]].forEach(x => board.set(x, -1))
assert.deepEqual(
board.getLiberties([1, 1]).sort(helper.lexicalCompare),
[[0, 1], [1, 0], [1, 2], [2, 0], [2, 2], [3, 1]]
)
assert.deepEqual(board.getLiberties([1, 2]), [])
})
it('should return empty list for a vertex not on the board', () => {
let board = new Board()
assert.deepEqual(board.getLiberties([-1, -1]), [])
})
})
describe('isValid', () => {
it('should return true for valid board arrangements', () => {
let board = new Board()
assert(board.isValid())
board.set([1, 1], 1).set([1, 2], -1)
assert(board.isValid())
})
it('should return false for non-valid board arrangements', () => {
let board = new Board()
;[[1, 0], [0, 1]].forEach(x => board.set(x, 1))
;[[0, 0]].forEach(x => board.set(x, -1))
assert(!board.isValid())
board = new Board()
;[[0, 1], [1, 0], [1, 2], [2, 0], [2, 2], [3, 1]].forEach(x => board.set(x, 1))
;[[1, 1], [2, 1]].forEach(x => board.set(x, -1))
assert(!board.isValid())
})
})
describe('makeMove', () => {
it('should not mutate board', () => {
let board = new Board()
board.makeMove(1, [5, 5])
assert.deepEqual(board.arrangement, new Board().arrangement)
})
it('should make a move', () => {
let board = new Board()
let move = board.makeMove(1, [5, 5])
board.set([5, 5], 1)
assert.deepEqual(board.arrangement, move.arrangement)
})
it('should remove captured stones', () => {
let board = new Board()
let black = [[0, 1], [1, 0], [1, 2], [2, 0], [2, 2]]
let white = [[1, 1], [2, 1]]
black.forEach(x => board.set(x, 1))
white.forEach(x => board.set(x, -1))
let move = board.makeMove(1, [3, 1])
assert.equal(move.get([3, 1]), 1)
black.forEach(x => assert.equal(move.get(x), 1))
white.forEach(x => assert.equal(move.get(x), 0))
// Edge capture
board = new Board()
board.set([0, 1], 1).set([0, 0], -1)
move = board.makeMove(1, [1, 0])
assert.equal(move.get([0, 0]), 0)
assert.equal(move.get([1, 0]), 1)
assert.equal(move.get([0, 1]), 1)
})
it('should count captures correctly', () => {
let board = new Board()
let black = [[0, 1], [1, 0], [1, 2], [2, 0], [2, 2]]
let white = [[1, 1], [2, 1]]
black.forEach(x => board.set(x, 1))
white.forEach(x => board.set(x, -1))
let move = board.makeMove(1, [3, 1])
assert.equal(move.captures[-1], 0)
assert.equal(move.captures[1], white.length)
board = new Board()
board.set([0, 1], 1).set([0, 0], -1)
move = board.makeMove(1, [1, 0])
assert.equal(move.captures[-1], 0)
assert.equal(move.captures[1], 1)
})
it('should handle suicide correctly', () => {
let board = new Board()
;[[0, 1], [1, 0], [1, 2], [2, 0], [2, 2], [3, 1]].forEach(x => board.set(x, 1))
;[[1, 1]].forEach(x => board.set(x, -1))
let move = board.makeMove(-1, [2, 1])
assert.equal(move.get([1, 1]), 0)
assert.equal(move.get([2, 1]), 0)
assert.equal(move.get([3, 1]), 1)
assert.equal(move.get([1, 2]), 1)
})
it('should make a pass', () => {
let board = new Board()
assert.deepEqual(board.makeMove(1, [-1, -1]).arrangement, board.arrangement)
board.set([1, 1], -1)
assert.deepEqual(board.makeMove(1, [-1, -1]).arrangement, board.arrangement)
})
})
})