-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_test.clj
476 lines (408 loc) · 19.4 KB
/
board_test.clj
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
(ns hive.test.domain.board-test)
(require '[clojure.test :refer [deftest testing is]])
(require '[hive.core.domain.position :as position])
(require '[hive.core.domain.piece :as piece])
(require '[hive.core.domain.board :as board])
(deftest place-piece-test
; place piece
(testing "place-piece, empty board"
(is (=
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}]}}
(board/place-piece
board/create
(piece/create :white :queen-bee)
(position/create 0 0)) )))
(testing "place-piece, board w/ single piece"
(is (=
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
(board/place-piece
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}]}}
{:color :black, :type :beetle}
(position/create 0 0)) )))
(testing "place-piece, empty board, nil piece"
(is (=
board/create
(board/place-piece
board/create
nil
(position/create 0 0)) )))
)(deftest remove-piece-test
; remove piece
(testing "remove-piece, empty board"
(is (=
board/create
(board/remove-piece
board/create
(position/create 0 0)) )))
(testing "remove-piece, board w/ single piece"
(is (=
board/create
(board/remove-piece
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}]}}
(position/create 0 0)) )))
(testing "remove-piece, board w/ stack of two pieces"
(is (=
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}]}}
(board/remove-piece
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
(position/create 0 0)) )))
)(deftest move-piece-test
; move piece
(testing "move-piece, empty board"
(is (=
board/create
(board/move-piece
board/create
(position/create 0 0)
(position/create -2 0)) )))
(testing "move-piece, board with one piece"
(is (=
{:pieces {{:row -2, :col 0} [{:color :white, :type :queen-bee}]}}
(board/move-piece
{:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}]}}
{:row 0, :col 0}
{:row -2, :col 0}) )))
)(deftest count-pieces-test
; count pieces
(testing "count-pieces, empty board"
(is (=
0
(board/count-pieces board/create) )) )
(testing "count-pieces, board with one piece"
(is (=
1
(board/count-pieces {:pieces {{:row 0, :col 0} [{:color :white, :type :queen-bee}]}}) )) )
(testing "count-pieces, board with three pieces"
(is (=
3
(board/count-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row -2, :col 0} [{:color :black, :type :queen-bee}]
{:row 2, :col 0} [{:color :white, :type :soldier-ant}] }}) )))
(testing "count-pieces, filtered by color, board with three pieces"
(is (=
2
(board/count-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row -2, :col 0} [{:color :black, :type :queen-bee}]
{:row 2, :col 0} [{:color :white, :type :soldier-ant}] }}
:white nil) ) ))
(testing "count-pieces, filtered by type, board with three pieces"
(is (=
1
(board/count-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row -2, :col 0} [{:color :black, :type :queen-bee}]
{:row 2, :col 0} [{:color :white, :type :soldier-ant}] }}
nil :soldier-ant) ) ))
)(deftest search-pieces-test
(testing "search-pieces, unfiltered, board with two pieces"
(is (=
'({:position {:row 0, :col 0}, :layer 0, :piece {:color :white, :type :queen-bee}}
{:position {:row 0, :col 0}, :layer 1, :piece {:color :black, :type :beetle}})
(board/search-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
nil nil) )))
(testing "search-pieces, filtered by color, board with two pieces"
(is (=
'({:position {:row 0, :col 0}, :layer 0, :piece {:color :white, :type :queen-bee}})
(board/search-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
:white nil) )))
(testing "search-pieces, filtered by type, board with two pieces"
(is (=
'({:position {:row 0, :col 0}, :layer 1, :piece {:color :black, :type :beetle}})
(board/search-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
nil :beetle) )))
)(deftest search-top-pieces-test
(testing "search-top-pieces, unfiltered, board with two pieces"
(is (=
'({:position {:row 0, :col 0}, :layer 1, :piece {:color :black, :type :beetle}})
(board/search-top-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
nil nil) )))
(testing "search-top-pieces, filtered by color, board with two pieces"
(is (=
'()
(board/search-top-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
:white nil) )))
(testing "search-top-pieces, filtered by type, board with two pieces"
(is (=
'({:position {:row 0, :col 0}, :layer 1, :piece {:color :black, :type :beetle}})
(board/search-top-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]}}
nil :beetle) )))
(testing "search-top-pieces, board with three stacks"
(is (=
'({:position {:row 0, :col 0}, :layer 0, :piece {:color :white, :type :queen-bee}}
{:position {:row 2, :col 0}, :layer 1, :piece {:color :white, :type :beetle}}
{:position {:row -2 :col 0}, :layer 1, :piece {:color :black, :type :beetle}})
(board/search-top-pieces {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row 2, :col 0} [{:color :black, :type :soldier-ant}, {:color :white, :type :beetle}]
{:row -2, :col 0} [{:color :white, :type :grasshopper}, {:color :black, :type :beetle}] }}
nil nil) )))
)(deftest lookup-occupied-positions-test
(testing "lookup-occupied-positions, empty board"
(is (=
#{}
(board/lookup-occupied-positions {:pieces {}}) )))
(testing "lookup-occupied-positions, small board"
(is (=
#{{:row 0, :col 0}}
(board/lookup-occupied-positions
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
}}) )))
)(deftest lookup-piece-stack-test
(testing "lookup-piece-stack, empty board"
(is (=
nil
(board/lookup-piece-stack {:pieces {}} {:row 0, :col 0}) )))
(testing "lookup-piece-stack, small board"
(is (=
[{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
(board/lookup-piece-stack
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
}} {:row 0, :col 0}) )))
)(deftest lookup-piece-stack-height-test
(testing "lookup-piece-stack-height, empty board"
(is (=
0
(board/lookup-piece-stack-height {:pieces {}} {:row 0, :col 0}) )))
(testing "lookup-piece-stack-height, small board"
(is (=
2
(board/lookup-piece-stack-height
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
}} {:row 0, :col 0}) )))
)(deftest lookup-piece-test
(testing "lookup-piece, empty board"
(is (=
nil
(board/lookup-piece {:pieces {}} {:row 0, :col 0}) )))
(testing "lookup-piece, small board"
(is (=
{:color :black, :type :beetle}
(board/lookup-piece
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
}} {:row 0, :col 0}) )))
)(deftest lookup-piece-at-height-test
(testing "lookup-piece-at-height, empty board"
(is (=
nil
(board/lookup-piece-at-height {:pieces {}} {:row 0, :col 0} 0) )))
(testing "lookup-piece-at-height, small board, height 0"
(is (=
{:color :white, :type :queen-bee}
(board/lookup-piece-at-height
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
}} {:row 0, :col 0} 0) )))
(testing "lookup-piece-at-height, small board, height 1"
(is (=
{:color :black, :type :beetle}
(board/lookup-piece-at-height
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}]
}} {:row 0, :col 0} 1) )))
)(deftest lookup-adjacent-positions-test
(testing "lookup-adjacent-positions, empty board"
(is (=
{ 0 {:contents nil, :direction 0, :height 0, :position {:col 0, :row -2}},
60 {:contents nil, :direction 60, :height 0, :position {:col 1, :row -1}},
120 {:contents nil, :direction 120, :height 0, :position {:col 1, :row 1}},
180 {:contents nil, :direction 180, :height 0, :position {:col 0, :row 2}},
240 {:contents nil, :direction 240, :height 0, :position {:col -1, :row 1}},
300 {:contents nil, :direction 300, :height 0, :position {:col -1, :row -1}}}
(board/lookup-adjacent-positions {:pieces {}} {:row 0, :col 0}) )))
(testing "lookup-adjacent-positions, medium board"
(is (=
{ 0 {:direction 0, :position {:row -2, :col 0}, :height 2, :contents [{:color :white, :type :grasshopper}, {:color :black, :type :beetle}]}
60 {:direction 60, :position {:row -1, :col 1}, :height 0, :contents nil}
120 {:direction 120, :position {:row 1, :col 1}, :height 0, :contents nil}
180 {:direction 180, :position {:row 2, :col 0}, :height 2, :contents [{:color :black, :type :soldier-ant}, {:color :white, :type :beetle}]}
240 {:direction 240, :position {:row 1, :col -1}, :height 0, :contents nil}
300 {:direction 300, :position {:row -1, :col -1}, :height 0, :contents nil} }
(board/lookup-adjacent-positions
{:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row 2, :col 0} [{:color :black, :type :soldier-ant}, {:color :white, :type :beetle}]
{:row -2, :col 0} [{:color :white, :type :grasshopper}, {:color :black, :type :beetle}] }}
{:row 0, :col 0}) )))
)(deftest generate-can-slide-lookup-table-test
(testing "generate-can-slide-lookup-table"
(is (=
{ ; can-slide-lookup-table (written by hand in javascript and ported to clojure, then moved to test
"......" "......" ; island cannot move
".....1" "1...1." ; slide around single piece
"....1." "...1.1" ; slide around single piece
"....11" "1..1.." ; slide alongside pair of adjacent pieces
"...1.." "..1.1." ; slide around single piece
"...1.1" "1.1..." ; slide up and out of crater
"...11." "..1..1" ; slide alongside pair of adjacent pieces
"...111" "1.1..." ; slide up and out of crater
"..1..." ".1.1.." ; slide around single piece
"..1..1" "11.11." ; slide between friends
"..1.1." ".1...1" ; slide up and out of crater
"..1.11" "11...." ; slide out of corner
"..11.." ".1..1." ; slide alongside pair of adjacent pieces
"..11.1" "11...." ; slide out of corner
"..111." ".1...1" ; slide up and out of crater
"..1111" "11...." ; slide to escape from pit
".1...." "1.1..." ; slide around single piece
".1...1" "..1.1." ; slide up and out of crater
".1..1." "1.11.1" ; slide between friends
".1..11" "..11.." ; slide out of corner
".1.1.." "1...1." ; slide up and out of crater
".1.1.1" "......" ; nearly-surrounded piece cannot move
".1.11." "1....1" ; slide out of corner
".1.111" "......" ; nearly-surrounded piece cannot move
".11..." "1..1.." ; slide alongside pair of adjacent pieces
".11..1" "...11." ; slide out of corner
".11.1." "1....1" ; slide out of corner
".11.11" "......" ; nearly-surrounded piece cannot move
".111.." "1...1." ; slide up and out of crater
".111.1" "......" ; nearly-surrounded piece cannot move
".1111." "1....1" ; slide to escape from pit
".11111" "......" ; nearly-surrounded piece cannot move
"1....." ".1...1" ; slide around single piece
"1....1" ".1..1." ; slide alongside pair of adjacent pieces
"1...1." ".1.1.." ; slide up and out of crater
"1...11" ".1.1.." ; slide up and out of crater
"1..1.." ".11.11" ; slide between friends
"1..1.1" ".11..." ; slide out of corner
"1..11." ".11..." ; slide out of corner
"1..111" ".11..." ; slide to escape from pit
"1.1..." "...1.1" ; slide up and out of crater
"1.1..1" "...11." ; slide out of corner
"1.1.1." "......" ; nearly-surrounded piece cannot move
"1.1.11" "......" ; nearly-surrounded piece cannot move
"1.11.." "....11" ; slide out of corner
"1.11.1" "......" ; nearly-surrounded piece cannot move
"1.111." "......" ; nearly-surrounded piece cannot move
"1.1111" "......" ; nearly-surrounded piece cannot move
"11...." "..1..1" ; slide alongside pair of adjacent pieces
"11...1" "..1.1." ; slide up and out of crater
"11..1." "..11.." ; slide out of corner
"11..11" "..11.." ; slide to escape from pit
"11.1.." "....11" ; slide out of corner
"11.1.1" "......" ; nearly-surrounded piece cannot move
"11.11." "......" ; nearly-surrounded piece cannot move
"11.111" "......" ; nearly-surrounded piece cannot move
"111..." "...1.1" ; slide up and out of crater
"111..1" "...11." ; slide to escape from pit
"111.1." "......" ; nearly-surrounded piece cannot move
"111.11" "......" ; nearly-surrounded piece cannot move
"1111.." "....11" ; slide to escape from pit
"1111.1" "......" ; nearly-surrounded piece cannot move
"11111." "......" ; nearly-surrounded piece cannot move
"111111" "......" ; completely surrounded piece cannot move
}
(board/generate-can-slide-lookup-table board/can-slide-lookup-table-seed) )))
)(deftest encode-slide-lookup-key-from-adjacencies-test
(testing "encode-slide-lookup-key-from-adjacencies, medium board"
(is (=
"1..1.."
(board/encode-slide-lookup-key-from-adjacencies
(board/lookup-adjacent-positions {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row 2, :col 0} [{:color :black, :type :soldier-ant}, {:color :white, :type :beetle}]
{:row -2, :col 0} [{:color :white, :type :grasshopper}, {:color :black, :type :beetle}] }}
{:row 0, :col 0})) )))
)(deftest render-valid-positions-from-slide-lookup-val-test
(testing "render-valid-positions-from-slide-lookup-val, lookup val with two positions"
(is (=
'({:row -2, :col 0}, {:row 2, :col 0})
(board/render-valid-positions-from-slide-lookup-val "1..1.." {:row 0, :col 0}) )))
)(deftest lookup-adjacent-slide-positions-test
(testing "lookup-adjacent-slide-positions, empty board"
(is (=
#{}
(board/lookup-adjacent-slide-positions {:pieces {}} {:row 0, :col 0}) )))
(testing "lookup-adjacent-slide-positions, the girl next door"
(is (=
#{{:row -1, :col 1}, {:row -1, :col -1}}
(board/lookup-adjacent-slide-positions
(board/remove-piece {:pieces {
{:row 0, :col 0} [{:color :white, :type :spider}]
{:row -2, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}] }} {:row 0, :col 0})
{:row 0, :col 0}) )))
(testing "lookup-adjacent-slide-positions, climb out of the pit"
(is (=
#{{:row -2, :col 0}, {:row -1, :col 1}}
(board/lookup-adjacent-slide-positions
(board/remove-piece {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row 1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 2, :col 0} [{:color :black, :type :soldier-ant}]
{:row 1, :col -1} [{:color :black, :type :soldier-ant}]
{:row -1, :col -1} [{:color :black, :type :soldier-ant}] }} {:row 0, :col 0})
{:row 0, :col 0}) )))
(testing "lookup-adjacent-slide-positions, imminent loss"
(is (=
#{}
(board/lookup-adjacent-slide-positions
(board/remove-piece {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row -2, :col 0} [{:color :black, :type :soldier-ant}]
{:row -1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 2, :col 0} [{:color :black, :type :spider}]
{:row 1, :col -1} [{:color :black, :type :spider}]
{:row -1, :col -1} [{:color :black, :type :beetle}] }} {:row 0, :col 0})
{:row 0, :col 0}) )))
)(deftest lookup-adjacent-climb-positions-test
(testing "lookup-adjacent-climb-positions, empty board"
(is (=
#{}
(board/lookup-adjacent-climb-positions {:pieces {}} {:row 0, :col 0}) )))
(testing "lookup-adjacent-climb-positions, the girl next door"
(is (=
#{{:row -2, :col 0}}
(board/lookup-adjacent-climb-positions
(board/remove-piece {:pieces {
{:row 0, :col 0} [{:color :white, :type :spider}]
{:row -2, :col 0} [{:color :white, :type :queen-bee}, {:color :black, :type :beetle}] }} {:row 0, :col 0})
{:row 0, :col 0}) )))
(testing "lookup-adjacent-climb-positions, climb out of the pit"
(is (=
#{{:row 1, :col 1} {:row 2, :col 0} {:row 1, :col -1} {:row -1, :col -1}}
(board/lookup-adjacent-climb-positions
(board/remove-piece {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row 1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 2, :col 0} [{:color :black, :type :soldier-ant}]
{:row 1, :col -1} [{:color :black, :type :soldier-ant}]
{:row -1, :col -1} [{:color :black, :type :soldier-ant}] }} {:row 0, :col 0})
{:row 0, :col 0}) )))
(testing "lookup-adjacent-climb-positions, imminent loss"
(is (=
#{{:row -2, :col 0} {:row -1, :col 1} {:row 1, :col 1} {:row 2, :col 0} {:row 1, :col -1} {:row -1, :col -1}}
(board/lookup-adjacent-climb-positions
(board/remove-piece {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row -2, :col 0} [{:color :black, :type :soldier-ant}]
{:row -1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 2, :col 0} [{:color :black, :type :spider}]
{:row 1, :col -1} [{:color :black, :type :spider}]
{:row -1, :col -1} [{:color :black, :type :beetle}] }} {:row 0, :col 0})
{:row 0, :col 0}) )))
)(deftest board-movement-meta-test
)(deftest lookup-occupied-adjacencies-test
(testing "lookup-occupied-adjacencies, U-shape"
(is (=
#{{:row 1, :col 1} {:row 2, :col 0} {:row 1, :col -1} {:row -1, :col -1}}
(board/lookup-occupied-adjacencies {:pieces {
{:row 0, :col 0} [{:color :white, :type :queen-bee}]
{:row 1, :col 1} [{:color :black, :type :soldier-ant}]
{:row 2, :col 0} [{:color :black, :type :soldier-ant}]
{:row 1, :col -1} [{:color :black, :type :soldier-ant}]
{:row -1, :col -1} [{:color :black, :type :soldier-ant}] }} {:row 0, :col 0}) )))
)