-
Notifications
You must be signed in to change notification settings - Fork 0
/
tak_game.lua
1314 lines (1146 loc) · 32.9 KB
/
tak_game.lua
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'torch'
require 'math'
require 'move_enumerator'
--ffi = require('ffi')
--require 'bit'
local min, max = math.min, math.max
local tak = torch.class('tak')
-- N.B.: The "making_a_copy" argument is used when making a fast clone of a tak game,
-- which is helpful in the AI tree search.
function tak:__init(size,making_a_copy)
self.verbose = false
self.size = size or 5
if self.size == 3 then
self.piece_count = 10
self.cap_count = 0
elseif self.size == 4 then
self.piece_count = 15
self.cap_count = 0
elseif self.size == 5 then
self.piece_count = 21
self.cap_count = 1
elseif self.size == 6 then
self.piece_count = 30
self.cap_count = 1
elseif self.size == 7 then
self.piece_count = 40
self.cap_count = 2
elseif self.size == 8 then
self.piece_count = 50
self.cap_count = 2
else
print 'Invalid size'
return
end
self.carry_limit = self.size
self.max_height = 2*self.piece_count + 1
-- DEBUG AND CLOCKING
self:set_debug_times_to_zero()
if making_a_copy then return end
self.player_pieces = {self.piece_count, self.piece_count}
self.player_caps = {self.cap_count, self.cap_count}
-- Index 1: board position
-- Index 2: height in stack
-- Index 3: player owning piece (1 for player 1, 2 for player 2)
-- Index 4: type of stone on this square (1 for flat, 2 for standing, 3 for cap)
-- values:
-- 0 means 'there is no stone of this description here'
-- 1 means 'there is a stone of this description here'
-- e.g, if self.board[i][1][2][3] = 1, that means,
-- at position (1,3,1), player 2 has a capstone.
self.board = {}
self.em = {0,0,0}
self.f = {1,0,0}
self.s = {0,1,0}
self.c = {0,0,1}
for i=1,self.size*self.size do
self.board[i] = {}
for k=1,self.max_height do
self.board[i][k] = {self.em,self.em}
end
end
self.board_size = self.size*self.size
-- convenience variable for keeping track of the topmost entry in each position
self.heights = {}
for i=1,self.size*self.size do
self.heights[i] = 0
end
self.ply = 0 -- how many plys have elapsed since the start of the game?
self.move_history_ptn = {}
self.board_top = {}
for i=1,self.size*self.size do
self.board_top[i] = {self.em,self.em}
end
self.empty_squares = {}
for i=1,self.size*self.size do
self.empty_squares[i] = 1
end
self.num_empty_squares = self.size*self.size
self.flattening_history = {}
self.legal_moves_by_ply = {}
--sbsd: stacks_by_sum_and_distance
--sbsd1: when the last stone is a cap crushing a wall
self.move2ptn, self.ptn2move, _, _, _, _, _, self.sbsd, self.sbsd1 = ptn_moves(self.carry_limit)
self.top_walls = self:make_filled_table(false)
self.blocks = self:make_filled_table(false)
self.is_up_boundary = {}
self.is_down_boundary = {}
self.is_left_boundary = {}
self.is_right_boundary = {}
for i=1,self.size*self.size do
self.is_up_boundary[i] = false
self.is_down_boundary[i] = false
self.is_right_boundary[i] = false
self.is_left_boundary[i] = false
end
for i=1,self.size do
self.is_up_boundary[self.size*self.size+i] = true
self.is_down_boundary[-i+1] = true
self.is_right_boundary[1 + i*self.size] = true
self.is_left_boundary[self.size*(i-1)] = true
end
self.l2i = {'a','b','c','d','e','f','g','h'}
self.pos = {}
self.pos2index = {}
self.x = {}
self.y = {}
self.has_left = {}
self.has_right = {}
self.has_up = {}
self.has_down = {}
for i=1,self.size do
for j=1,self.size do
self.x[i+self.size*(j-1)] = i
self.y[i+self.size*(j-1)] = j
if i > 1 then
self.has_left[i+self.size*(j-1)] = true
else
self.has_left[i+self.size*(j-1)] = false
end
if i < self.size then
self.has_right[i+self.size*(j-1)] = true
else
self.has_right[i+self.size*(j-1)] = false
end
if j > 1 then
self.has_down[i+self.size*(j-1)] = true
else
self.has_down[i+self.size*(j-1)] = false
end
if j < self.size then
self.has_up[i+self.size*(j-1)] = true
else
self.has_up[i+self.size*(j-1)] = false
end
self.pos[i+self.size*(j-1)] = self.l2i[i] .. j
self.pos2index[self.l2i[i] .. j] = i+self.size*(j-1)
end
end
self.queue = {}
for i=1,self.board_size do self.queue[i] = 0 end
self.explored = {}
self:generate_all_moves()
self:populate_legal_moves_at_this_ply()
self.game_over = false
self.winner = 0
self.win_type = 'NA'
self.island_sums = {{},{}}
self.num_islands = {0,0}
self.island_max_dims = {0,0}
self.island_len_sums = {0,0}
self.player_flats = {0,0}
end
function tak:generate_all_moves()
self.place_flat = {}
self.place_wall = {}
self.place_cap = {}
self.moves = {}
self.moves1 = {}
self.move2pos = {}
self.is_place_flat = {}
self.is_place_wall = {}
self.is_place_cap = {}
self.move2stacksum = {}
self.move2stackdel = {}
self.move2stackstr = {}
self.place_info = {f={self.f,1,0,false,false},s={self.s,1,0,true,true},c={self.c,0,1,true,false}}
local move_type,ptn,stackdir,stackstr
for i=1,#self.move2ptn do
ptn = self.move2ptn[i]
move_type = string.sub(ptn,1,1)
self.is_place_flat[ptn] = move_type == 'f'
self.is_place_wall[ptn] = move_type == 's'
self.is_place_cap[ptn] = move_type == 'c'
self.move2stacksum[ptn] = tonumber(move_type)
if tonumber(move_type) then
stackdir = string.sub(ptn,4,4)
stackstr = string.sub(ptn,5,#ptn)
if stackdir == '<' then self.move2stackdel[ptn] = -1
elseif stackdir == '+' then self.move2stackdel[ptn] = self.size
elseif stackdir == '>' then self.move2stackdel[ptn] = 1
elseif stackdir == '-' then self.move2stackdel[ptn] = -self.size
end
self.move2stackstr[ptn] = stackstr
end
self.move2pos[ptn] = self.pos2index[string.sub(ptn,2,3)]
end
local dirs = {'<','+','>','-'}
for i=1, self.board_size do
self.place_flat[i] = 'f' .. self.pos[i]
self.place_wall[i] = 's' .. self.pos[i]
self.place_cap[i] = 'c' .. self.pos[i]
self.moves[i] = {}
self.moves1[i] = {}
for k=1,4 do
self.moves[i][k] = {}
self.moves1[i][k] = {}
if (k==1 and self.has_left[i]) or (k==2 and self.has_up[i]) or (k==3 and self.has_right[i]) or (k==4 and self.has_down[i]) then
for j=1,self.size do
self.moves[i][k][j] = {}
self.moves1[i][k][j] = {}
for l=1,j do
self.moves[i][k][j][l] = {}
self.moves1[i][k][j][l] = {}
local seqs = self.sbsd[j][l]
for m=1,#seqs do
self.moves[i][k][j][l][m] = seqs[m][1] .. self.pos[i] .. dirs[k] .. seqs[m][2]
end
local seqs1 = self.sbsd1[j][l]
for m=1,#seqs1 do
self.moves1[i][k][j][l][m] = seqs1[m][1] .. self.pos[i] .. dirs[k] .. seqs1[m][2]
end
end
end
end
end
end
end
function tak:get_history()
return self.move_history_ptn
end
function tak:get_i2n(i)
return self.move2ptn[i]
end
function tak:set_debug_times_to_zero()
self.get_legal_moves_time = 0
self.check_stack_moves_time = 0
self.execute_move_time = 0
self.flood_fill_time = 0
self.undo_time = 0
value_of_node_time = 0
end
function tak:print_debug_times()
print('undo time: \t' .. self.undo_time)
print('get legal moves time: \t' .. self.get_legal_moves_time)
print('check stack moves time: \t' .. self.check_stack_moves_time)
print('execute move time: \t' .. self.execute_move_time)
print('flood fill time: \t' .. self.flood_fill_time)
print('value of node time: \t' .. value_of_node_time)
end
function tak:is_terminal()
return self.game_over
end
function tak:undo()
--local start_time = os.clock()
if self.ply == 0 then
return
end
self.ply = self.ply - 1
if self.game_over then
self.game_over = false
self.winner = 0
self.win_type = 'NA'
end
local most_recent_move = self.move_history_ptn[#self.move_history_ptn]
self.move_history_ptn[#self.move_history_ptn] = nil
if #self.legal_moves_by_ply > self.ply + 1 then
self.legal_moves_by_ply[#self.legal_moves_by_ply] = nil
end
self:undo_move(most_recent_move)
--self.undo_time = self.undo_time + os.clock() - start_time
end
function tak:reset()
self:__init(self.size)
end
function tak:clone(deep)
if deep then
return self:deep_clone()
else
return self:fast_clone()
end
end
function tak:fast_clone()
local copy = tak.new(self.size,true)
copy.em = self.em
copy.f = self.f
copy.s = self.s
copy.c = self.c
copy.heights = deepcopy(self.heights)
copy.empty_squares = deepcopy(self.empty_squares)
copy.board = {}
copy.board_top = {}
for i=1,self.size*self.size do
copy.board[i] = {}
for k=1,self.max_height do
copy.board[i][k] = {self.board[i][k][1],self.board[i][k][2]}
end
copy.board_top[i] = {self.board_top[i][1], self.board_top[i][2]}
end
copy.blocks = {}
copy.top_walls = {}
copy.ply = self.ply
copy.player_pieces = deepcopy(self.player_pieces)
copy.player_caps = deepcopy(self.player_caps)
copy.move_history_ptn = {}
copy.legal_moves_by_ply = {}
copy.game_over = self.game_over
copy.winner = self.winner
copy.win_type = self.win_type
copy.move2ptn = self.move2ptn
copy.ptn2move = self.ptn2move
copy.sbsd = self.sbsd
copy.sbsd1 = self.sbsd1
copy.place_flat = self.place_flat
copy.place_wall = self.place_wall
copy.place_cap = self.place_cap
copy.moves = self.moves
copy.moves1 = self.moves1
copy.island_sums = {{},{}}
self.num_islands = {0,0}
copy.islands_minmax = {{},{}}
copy.player_flats = {0,0}
copy.flattening_history = deepcopy(self.flattening_history)
copy.pos = self.pos
copy.pos2index = self.pos2index
copy.x = self.x
copy.y = self.y
copy.has_left = self.has_left
copy.has_right = self.has_right
copy.has_up = self.has_up
copy.has_down = self.has_down
copy.is_left_boundary = self.is_left_boundary
copy.is_right_boundary = self.is_right_boundary
copy.is_up_boundary = self.is_up_boundary
copy.is_down_boundary = self.is_down_boundary
copy.explored = {}
return copy
end
function tak:deep_clone()
local copy = tak.new(self.size)
copy:play_game_from_ptn(self:game_to_ptn(),true)
return copy
end
function tak:check_stack_moves_dry(legal_moves_ptn,player,i)
--local start_time = os.clock()
local hand = min(self.heights[i],self.size)
local top_is_cap = self.board_top[i][player][3] == 1
local seqs, dist, x
local moves, moves1 = self.moves[i], self.moves1[i]
local n = #legal_moves_ptn
if self.has_left[i] then
dist = 0
x = i - 1
while (not(self.is_left_boundary[x]) and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x - 1
end
if top_is_cap and dist<hand and not(self.is_left_boundary[x]) and self.top_walls[x] then
dist = dist + 1
seqs = moves1[1][hand][dist]
else
seqs = moves[1][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
--
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
--
end
end
end
if self.has_down[i] then
dist = 0
x = i - self.size
while (x > 0 and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x - self.size
end
if top_is_cap and dist<hand and x > 0 and self.top_walls[x] then
dist = dist + 1
seqs = moves1[4][hand][dist]
else
seqs = moves[4][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
--
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
--
end
end
end
if self.has_right[i] then
dist = 0
x = i + 1
while (not(self.is_right_boundary[x]) and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x + 1
end
if top_is_cap and dist<hand and not(self.is_right_boundary[x]) and self.top_walls[x] then
dist = dist + 1
seqs = moves1[3][hand][dist]
else
seqs = moves[3][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
--
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
--
end
end
end
if self.has_up[i] then
dist = 0
x = i + self.size
while (not(self.is_up_boundary[x]) and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x + self.size
end
if top_is_cap and dist<hand and not(self.is_up_boundary[x]) and self.top_walls[x] then
dist = dist + 1
seqs = moves1[2][hand][dist]
else
seqs = moves[2][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
--
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
--
end
end
end
--self.check_stack_moves_time = self.check_stack_moves_time + (os.clock() - start_time)
end
function tak:get_legal_moves_dry(player)
local legal_moves_ptn = {}
--local start_time = os.clock()
local empty, player_pieces, player_caps, board_top, pos, ply,board_size = self.empty_squares, self.player_pieces, self.player_caps, self.board_top, self.pos, self.ply, self.board_size
for i=1,board_size do
if empty[i]==1 and ply > 1 then
if player_pieces[player] > 0 then
--
--
end
if player_caps[player] > 0 then
--
end
elseif ply > 1 then
if not(board_top[i][player] == self.em) then
self:check_stack_moves(legal_moves_ptn,player,i)
end
elseif empty[i]==1 then
--
end
end
--self.get_legal_moves_time = self.get_legal_moves_time + (os.clock() - start_time)
return legal_moves_ptn
end
function tak:check_stack_moves(legal_moves_ptn,player,i)
--local start_time = os.clock()
local hand = min(self.heights[i],self.size)
local top_is_cap = self.board_top[i][player][3] == 1
local seqs, dist, x
local moves, moves1 = self.moves[i], self.moves1[i]
local n = #legal_moves_ptn
if self.has_left[i] then
dist = 0
x = i - 1
while (not(self.is_left_boundary[x]) and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x - 1
end
if top_is_cap and dist<hand and not(self.is_left_boundary[x]) and self.top_walls[x] then
dist = dist + 1
seqs = moves1[1][hand][dist]
else
seqs = moves[1][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
legal_moves_ptn[n] = seqs[1]
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
legal_moves_ptn[n] = seqs[m]
end
end
end
if self.has_down[i] then
dist = 0
x = i - self.size
while (x > 0 and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x - self.size
end
if top_is_cap and dist<hand and x > 0 and self.top_walls[x] then
dist = dist + 1
seqs = moves1[4][hand][dist]
else
seqs = moves[4][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
legal_moves_ptn[n] = seqs[1]
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
legal_moves_ptn[n] = seqs[m]
end
end
end
if self.has_right[i] then
dist = 0
x = i + 1
while (not(self.is_right_boundary[x]) and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x + 1
end
if top_is_cap and dist<hand and not(self.is_right_boundary[x]) and self.top_walls[x] then
dist = dist + 1
seqs = moves1[3][hand][dist]
else
seqs = moves[3][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
legal_moves_ptn[n] = seqs[1]
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
legal_moves_ptn[n] = seqs[m]
end
end
end
if self.has_up[i] then
dist = 0
x = i + self.size
while (not(self.is_up_boundary[x]) and not(self.blocks[x]) and dist<hand) do
dist = dist + 1
x = x + self.size
end
if top_is_cap and dist<hand and not(self.is_up_boundary[x]) and self.top_walls[x] then
dist = dist + 1
seqs = moves1[2][hand][dist]
else
seqs = moves[2][hand][dist]
end
if hand == 1 and dist == 1 then
n = n + 1
legal_moves_ptn[n] = seqs[1]
elseif dist>0 then
local N = #seqs
for m=1,N do
n = n + 1
legal_moves_ptn[n] = seqs[m]
end
end
end
--self.check_stack_moves_time = self.check_stack_moves_time + (os.clock() - start_time)
end
function tak:get_legal_moves(player)
local legal_moves_ptn = {}
--local start_time = os.clock()
local empty, player_pieces, player_caps, board_top, pos, ply,board_size = self.empty_squares, self.player_pieces, self.player_caps, self.board_top, self.pos, self.ply, self.board_size
for i=1,board_size do
if empty[i]==1 and ply > 1 then
if player_pieces[player] > 0 then
legal_moves_ptn[#legal_moves_ptn+1] = self.place_flat[i]
legal_moves_ptn[#legal_moves_ptn+1] = self.place_wall[i]
end
if player_caps[player] > 0 then
legal_moves_ptn[#legal_moves_ptn+1] = self.place_cap[i]
end
elseif ply > 1 then
if not(board_top[i][player] == self.em) then
self:check_stack_moves(legal_moves_ptn,player,i)
end
elseif empty[i]==1 then
legal_moves_ptn[#legal_moves_ptn+1] = self.place_flat[i]
end
end
--self.get_legal_moves_time = self.get_legal_moves_time + (os.clock() - start_time)
return legal_moves_ptn
end
function tak:get_legal_move_table()
return self.legal_moves_by_ply[#self.legal_moves_by_ply]
end
function tak:get_legal_move_mask(as_bool)
local mask = torch.zeros(#self.move2ptn)
local legal = self:get_legal_move_table()
for i=1,#legal do mask[self.ptn2move[legal[i]]] = 1 end
if as_bool then mask = mask:byte() end
return mask
end
function tak:get_player()
-- self.ply says how many plys have been played, starts at 0
return self.ply % 2 + 1
end
function tak:populate_legal_moves_at_this_ply()
local player = self:get_player()
if #self.legal_moves_by_ply < self.ply+1 then
local legal_moves_ptn = self:get_legal_moves(player)
table.insert(self.legal_moves_by_ply,legal_moves_ptn)
end
end
function tak:make_move(ptn,flag)
--local start_time = os.clock()
-- on the first turn of each player, they play a piece belonging to
-- the opposite player
local player
if self.ply < 2 then
player = 2 - self.ply
else
player = self:get_player()
end
self.move_history_ptn[#self.move_history_ptn + 1] = ptn
local move_type = string.sub(ptn,1,1)
local i = self.pos2index[string.sub(ptn,2,3)]
--local i = self.move2pos[ptn]
local stacksum = tonumber(move_type)
local flattening_flag = false
if not(stacksum) then
local place_info = self.place_info[move_type]
self.heights[i] = 1
self.board[i][1][player] = place_info[1]
self.player_pieces[player] = self.player_pieces[player] - place_info[2]
self.player_caps[player] = self.player_caps[player] - place_info[3]
self.board_top[i][player] = self.board[i][1][player]
self.empty_squares[i] = 0
self.blocks[i] = place_info[4]
self.top_walls[i] = place_info[5]
else
flattening_flag = self:execute_slide_move(player,move_type,ptn,i)
end
self.ply = self.ply + 1
--self.execute_move_time = self.execute_move_time + (os.clock() - start_time)
if not(flag) then
self:populate_legal_moves_at_this_ply()
end
self:check_victory_conditions()
self.flattening_history[#self.flattening_history+1] = flattening_flag
return true
end
function tak:execute_slide_move(player,move_type,ptn,i)
local flattening_flag = false
local stacksum = tonumber(move_type)
local stackdir = string.sub(ptn,4,4)
local stackstr = string.sub(ptn,5,#ptn)
local h = self.heights[i] - stacksum
self.heights[i] = h
if h == 0 then
self.empty_squares[i] = 1
self.board_top[i][1] = self.em
self.board_top[i][2] = self.em
else
self.board_top[i][1] = self.board[i][h][1]
self.board_top[i][2] = self.board[i][h][2]
end
local del
if stackdir == '<' then
del = -1
elseif stackdir == '+' then
del = self.size
elseif stackdir == '>' then
del = 1
elseif stackdir == '-' then
del = -self.size
end
local x = i + del
local d = 1
local D = tonumber(string.sub(stackstr,d,d))
local m, n
m = 1
for k=1,stacksum do
self.empty_squares[x] = 0
self.heights[x] = self.heights[x] + 1
self.board[x][self.heights[x]][1] = self.board[i][h+k][1]
self.board[x][self.heights[x]][2] = self.board[i][h+k][2]
self.board_top[x][1] = self.board[x][self.heights[x]][1]
self.board_top[x][2] = self.board[x][self.heights[x]][2]
self.board[i][h+k][1] = self.em
self.board[i][h+k][2] = self.em
-- flattening logic
if (k == stacksum and self.board_top[x][player]==self.c and self.heights[x] > 1) then
local h2 = self.heights[x]
if self.board[x][h2-1][player][2] == 1 then
self.board[x][h2-1][player] = self.f
flattening_flag = true
elseif self.board[x][h2-1][3 - player][2] == 1 then
self.board[x][h2-1][3 - player] = self.f
flattening_flag = true
end
self.top_walls[x] = false
end
if m == D then
x = x + del
m = 0
d = d + 1
D = tonumber(string.sub(stackstr,d,d))
end
m = m + 1
end
self.blocks[i] = (self.board_top[i][1] == self.s or self.board_top[i][1] == self.c
or self.board_top[i][2] == self.s or self.board_top[i][2] == self.c)
self.top_walls[i] = (self.board_top[i][1] == self.s or self.board_top[i][2] == self.s)
x = x - del
self.blocks[x] = (self.board_top[x][1] == self.s or self.board_top[x][1] == self.c
or self.board_top[x][2] == self.s or self.board_top[x][2] == self.c)
self.top_walls[x] = (self.board_top[x][1] == self.s or self.board_top[x][2] == self.s)
return flattening_flag
end
function tak:undo_move(ptn)
-- on the first turn of each player, they play a piece belonging to
-- the opposite player
local player
if self.ply < 2 then
player = 2 - self.ply
else
player = self:get_player()
end
local move_type = string.sub(ptn,1,1)
local i = self.move2pos[ptn]
local stacksum = tonumber(move_type)
if not(stacksum) then
local d = 0
if move_type == 'c' then d = 1 end
self.heights[i] = 0
self.board[i][1][player] = self.em
self.player_pieces[player] = self.player_pieces[player] + 1 - d
self.player_caps[player] = self.player_caps[player] + d
self.board_top[i][player] = self.em
self.empty_squares[i] = 1
self.blocks[i] = false
self.top_walls[i] = false
else
self:undo_slide_move(player,move_type,ptn,i)
end
self.flattening_history[#self.flattening_history] = nil
end
function tak:undo_slide_move(player,move_type,ptn,i)
local stacksum = tonumber(move_type)
local stackdir = string.sub(ptn,4,4)
local stackstr = string.sub(ptn,5,#ptn)
self.empty_squares[i] = 0
local h = self.heights[i]
local del
if stackdir == '<' then
del = -1
elseif stackdir == '+' then
del = self.size
elseif stackdir == '>' then
del = 1
elseif stackdir == '-' then
del = -self.size
end
local x = i + del
local d = 1
local D = tonumber(string.sub(stackstr,d,d))
local m, n = D, 0
for k=1,stacksum do
self.board[i][h+n+m][1] = self.board[x][self.heights[x]][1]
self.board[i][h+n+m][2] = self.board[x][self.heights[x]][2]
self.board[x][self.heights[x]][1] = self.em
self.board[x][self.heights[x]][2] = self.em
self.heights[x] = self.heights[x] - 1
-- unflattening logic
if (k==stacksum and self.flattening_history[#self.flattening_history]) then
if self.board[x][self.heights[x]][player][1] == 1 then
self.board[x][self.heights[x]][player] = self.s
elseif self.board[x][self.heights[x]][3 - player][1] == 1 then
self.board[x][self.heights[x]][3 - player] = self.s
end
end
if self.heights[x] > 0 then
self.board_top[x][1] = self.board[x][self.heights[x]][1]
self.board_top[x][2] = self.board[x][self.heights[x]][2]
else
self.board_top[x][1] = self.em
self.board_top[x][2] = self.em
self.empty_squares[x] = 1
end
if m == 1 and d < #stackstr then
x = x + del
d = d + 1
n = n + D
D = tonumber(string.sub(stackstr,d,d))
m = D + 1
end
m = m - 1
end
self.heights[i] = h + stacksum
self.board_top[i][1] = self.board[i][self.heights[i]][1]
self.board_top[i][2] = self.board[i][self.heights[i]][2]
self.blocks[i] = (self.board_top[i][1] == self.s or self.board_top[i][1] == self.c
or self.board_top[i][2] == self.s or self.board_top[i][2] == self.c)
self.top_walls[i] = (self.board_top[i][1] == self.s or self.board_top[i][2] == self.s)
self.blocks[x] = (self.board_top[x][1] == self.s or self.board_top[x][1] == self.c
or self.board_top[x][2] == self.s or self.board_top[x][2] == self.c)
self.top_walls[x] = (self.board_top[x][1] == self.s or self.board_top[x][2] == self.s)
end
function tak:make_zero_table()
return self:make_filled_table(0)
end
function tak:make_filled_table(n)
local ntab = {}
for i=1,self.size*self.size do
ntab[i] = n
end
return ntab
end
function tak:check_victory_conditions()
local player_one_remaining = self.player_pieces[1] + self.player_caps[1]
local player_two_remaining = self.player_pieces[2] + self.player_caps[2]
local empty, board_top = self.empty_squares, self.board_top
local explored = self.explored
self.num_empty_squares = 0
self.player_flats[1] = 0
self.player_flats[2] = 0
for i=1,self.board_size do
explored[i] = false
self.num_empty_squares = self.num_empty_squares + empty[i]
self.player_flats[1] = self.player_flats[1] + board_top[i][1][1]
self.player_flats[2] = self.player_flats[2] + board_top[i][2][1]
end
-- if the game board is full or either player has run out of pieces, trigger end
local end_is_nigh = self.num_empty_squares == 0 or player_one_remaining == 0 or player_two_remaining == 0
-- let's find us some island information
--local start_time = os.clock()
local x, y = self.x, self.y
local has_left, has_right, has_down, has_up = self.has_left, self.has_right, self.has_down, self.has_up
local min_x, max_x, min_y, max_y, sum
local p1_rw, p2_rw = false, false
local queue = self.queue
local function flood_fill(start,player)
local pointer = 1
local end_of_queue = 1
queue[1] = start
repeat
local j = queue[pointer]
if (not(explored[j]) and (board_top[j][player][1] == 1 or board_top[j][player][3] == 1) ) then
explored[j] = true
sum = sum + 1
min_x = min(x[j],min_x)
max_x = max(x[j],max_x)
min_y = min(y[j],min_y)
max_y = max(y[j],max_y)
if has_left[j] and not(explored[j-1]) then
end_of_queue = end_of_queue + 1
queue[end_of_queue] = j - 1
end
if has_right[j] and not(explored[j+1]) then
end_of_queue = end_of_queue + 1
queue[end_of_queue] = j + 1
end
if has_down[j] and not(explored[j-game.size]) then
end_of_queue = end_of_queue + 1
queue[end_of_queue] = j - game.size
end
if has_up[j] and not(explored[j+game.size]) then
end_of_queue = end_of_queue + 1
queue[end_of_queue] = j + game.size
end
end
pointer = pointer + 1
until pointer > end_of_queue
end