-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnp_board.py
1033 lines (849 loc) · 29.7 KB
/
np_board.py
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
import numpy as np
from queue import Queue
from generate_move_arrays import generate_move_arrays
#Flat White = 2 010
#Flat Black = 3 011
#Wall White = 4 100
#Wall Black = 5 101
#Caps White = 6 110
#Caps Black = 7 111
#Count as road & 2
#Cant play on top & 4
#Color & 1
class TakBoard():
"""docstring for TakBoard"""
def __init__(self, size):
self.capstone_player1 = True
self.capstone_player2 = True
self.player1_turn = True
self.move_number = 0
self.board_size = size
self.max_height = 64
self.black_piece_count = 21
self.white_piece_count = 21
self.board = np.full((self.board_size, self.board_size, self.max_height), 0, dtype='B')
self.encode = {"w": 2, "b": 3, "sw": 4, "sb": 5, "cw": 6, "cb": 7}
self.white_top = np.full((self.board_size, self.board_size), False, dtype=bool)
self.black_top = np.full((self.board_size, self.board_size), False, dtype=bool)
self.white_win = False
self.black_win = False
self.distance_table = [0,5,15,25,30]
###Monte Carlo Functions
def clone(self):
new_ret = TakBoard(self.board_size)
new_ret.capstone_player1 = self.capstone_player1
new_ret.capstone_player2 = self.capstone_player2
new_ret.player1_turn = self.player1_turn
new_ret.move_number = self.move_number
new_ret.board_size = self.board_size
new_ret.board = np.copy(self.board)
new_ret.white_top = np.copy(self.white_top)
new_ret.black_top = np.copy(self.black_top)
new_ret.white_win = self.white_win
new_ret.black_win = self.black_win
new_ret.black_piece_count = self.black_piece_count
new_ret.white_piece_count = self.white_piece_count
return new_ret
def exec_move(self, move_obj):
#print(move_obj)
if move_obj["movetype"] == "m":
self.move(move_obj["start"], move_obj["end"], move_obj["order"])
elif move_obj["movetype"] == "p":
self.place(move_obj["piece"], move_obj["placement"])
else:
raise ValueError("Invalid movetype")
def get_play_index(self, play_obj):
if play_obj["movetype"] == "p":
x,y = self.get_x_y_from_grid(play_obj["placement"])
offset = (x + y*self.board_size) * 3
if play_obj["piece"] == "S":
return 1500 + offset + 1
elif play_obj["piece"] == "C":
return 1500 + offset + 2
return 1500 + offset
elif play_obj["movetype"] == "m":
x,y = self.get_x_y_from_grid(play_obj["start"])
offset = (x + y*self.board_size) * 60
offset2 =0
moves_up = self.distance_table[y]
moves_right = self.distance_table[self.board_size -1 - x]
moves_down = self.distance_table[self.board_size -1 - y]
moves_left = self.distance_table[x]
if play_obj["direction"] == "up":
for index, item in enumerate(generate_move_arrays(y, self.board_size,False)):
if item == play_obj["order"]:
offset2 += index -1
break
elif play_obj["direction"] == "right":
offset2 = moves_up
for index, item in enumerate(generate_move_arrays(self.board_size -1 - x, self.board_size,False)):
if item == play_obj["order"]:
offset2 += index -1
break
elif play_obj["direction"] == "down":
offset2 = moves_up + moves_right
for index, item in enumerate(generate_move_arrays(self.board_size -1 - y, self.board_size,False)):
if item == play_obj["order"]:
offset2 += index -1
break
elif play_obj["direction"] == "left":
offset2 = moves_up + moves_right + moves_down
for index, item in enumerate(generate_move_arrays(x, self.board_size,False)):
if item == play_obj["order"]:
offset2 += index -1
break
else:
raise ValueError("Invalid Direction")
return offset + offset2
else:
raise ValueError("Invalid Movetype")
def get_plays(self):
play_array = []
#First or second play
if self.move_number <= 1:
for x,y in np.ndindex(self.board.shape[:2]):
if np.count_nonzero(self.board[x,y]) == 0:
temp_move = {"movetype": "p", "piece":"", "placement":self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1)}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
return play_array
else:
##Add placements
for x,y in np.ndindex(self.board.shape[:2]):
cell = self.board[x,y]
if np.count_nonzero(cell) == 0:
temp_move = {"movetype": "p", "piece":"", "placement":self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1)}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
temp_move1 = {"movetype": "p", "piece":"S", "placement": temp_move["placement"], "index": temp_move["index"] + 1}
play_array.append(temp_move1)
if (self.player1_turn == True and self.capstone_player1 == True) or (self.player1_turn == False and self.capstone_player2 == True):
temp_move2 = {"movetype": "p", "piece":"C", "placement": temp_move1["placement"], "index": temp_move1["index"] + 1}
play_array.append(temp_move2)
else:
##Add moves
last_index = np.argmax(cell==0) -1
cap = (np.bitwise_and(cell[last_index],6) == 6)
start_index = self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1)
if self.player1_turn == True:
if (np.bitwise_and(cell[last_index],1) == 0):
distance = 0
s_distance = 0
to_move = min(self.board_size, last_index+1)
#Down
for move_x in range(x+1, self.board_size):
cell_m = self.board[move_x,y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1-len(move), self.board_size - y -1), "order": move, "direction": "down"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
distance = 0
s_distance = 0
#Up
for move_x in range(x-1, -1, -1):
cell_m = self.board[move_x,y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1+len(move), self.board_size - y -1), "order": move, "direction": "up"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
distance = 0
s_distance = 0
#Left
for move_y in range(y-1, -1, -1):
cell_m = self.board[x,move_y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1+len(move)), "order": move, "direction": "left"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
distance = 0
s_distance = 0
#Right
for move_y in range(y+1, self.board_size):
cell_m = self.board[x,move_y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1-len(move)), "order": move, "direction": "right"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
else:
#Get top of players color
if (np.bitwise_and(cell[last_index],1) == 1):
distance = 0
s_distance = 0
to_move = min(self.board_size, last_index+1)
#Down
for move_x in range(x+1, self.board_size):
cell_m = self.board[move_x,y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1-len(move), self.board_size - y -1), "order": move, "direction": "down"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
distance = 0
s_distance = 0
#Up
for move_x in range(x-1, -1, -1):
cell_m = self.board[move_x,y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1+len(move), self.board_size - y -1), "order": move, "direction": "up"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
distance = 0
s_distance = 0
#Left
for move_y in range(y-1, -1, -1):
cell_m = self.board[x,move_y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1+len(move)), "order": move, "direction": "left"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
distance = 0
s_distance = 0
#Right
for move_y in range(y+1, self.board_size):
cell_m = self.board[x,move_y]
last_new_index = np.argmax(cell_m==0) -1
if last_new_index < 0:
last_new_index = 0
if np.count_nonzero(cell_m) == 0 or (np.bitwise_and(cell_m[last_new_index],4) != 4):
distance +=1
elif cap and np.bitwise_and(cell_m[last_new_index],6) == 4:
s_distance = distance+1
break
else:
break
move_arrays = generate_move_arrays(distance, to_move, cap and s_distance)
for move in move_arrays:
temp_move = {"movetype": "m", "start":start_index, "end":self.get_index_from_ints(self.board_size - x -1, self.board_size - y -1-len(move)), "order": move, "direction": "right"}
temp_move["index"] = self.get_play_index(temp_move)
play_array.append(temp_move)
return play_array
def winner_place(self, piece, grid_location):
if piece == "" or piece[0] != "S":
#Start at grid location and map road.
x,y = self.get_x_y_from_grid(grid_location)
min_x = x
max_x = x
min_y = y
max_y = y
to_check = Queue()
visited = []
to_check.put([x,y])
while not to_check.empty():
##Add indexes to Queue
x, y = to_check.get()
visited.append([x,y])
if (self.player1_turn == True and self.white_top[y][x] == True) or \
(self.player1_turn == False and self.black_top[y][x] == True):
if x -1 >= 0 and [x -1, y] not in visited:
to_check.put([x -1, y])
visited.append([x -1, y])
if y -1 >= 0 and [x, y -1] not in visited:
to_check.put([x, y -1])
visited.append([x, y -1])
if x +1 < self.board_size and [x +1, y] not in visited:
to_check.put([x +1, y])
visited.append([x +1, y])
if y +1 < self.board_size and [x, y+1] not in visited:
to_check.put([x, y +1])
visited.append([x, y +1])
min_x = min(min_x, x)
min_y = min(min_y, y)
max_x = max(max_x, x)
max_y = max(max_y, y)
#Check for Win
if (max_x - min_x +1) == self.board_size or (max_y - min_y +1) == self.board_size:
if self.player1_turn == True:
self.white_win = True
else:
self.black_win = True
#Placing a standing Stone cannot win
return -1
def winner_move(self, to_check):
#Winner_place from start to end
for x in to_check:
self.winner_place("", x)
self.player1_turn = not self.player1_turn
for x in to_check:
self.winner_place("", x)
self.player1_turn = not self.player1_turn
###Game Functions
def place(self, piece, grid_location):
x,y = self.get_x_y_from_grid(grid_location)
#last_new_index = (self.board[move_x][y]!=0).cumsum().argmax()
cell = self.board[y,x]
if np.count_nonzero(cell) != 0:
raise Exception("Invalid Placement Location: gridlocation={}, currentsquare={}".format(grid_location, cell))
if self.move_number == 0:
color = "b"
elif self.move_number == 1:
color = "w"
elif self.player1_turn == True:
#Is White
color = "w"
else:
color = "b"
if piece == None or piece == "":
place_peice = color
elif piece.lower() == "w" or piece.lower() == "s":
place_peice = "S"+ color
elif piece.lower() == "c":
place_peice = "C"+ color
if self.player1_turn == True:
self.capstone_player1 = False
else:
self.capstone_player2 = False
else:
raise ValueError("Invalid piece: {}".format(piece))
#Place on board
cell[0] = self.encode[place_peice.lower()]
#Update
if place_peice[0] != "S":
if color == "w":
self.white_top[y][x] = True
else:
self.black_top[y][x] = True
#Check Winner
self.winner_place(place_peice, grid_location)
if piece.lower() != 'C':
if color == "w":
self.white_piece_count -= 1
if self.white_piece_count == 0:
self.black_win = True
return
else:
self.black_piece_count -= 1
if self.black_piece_count == 0:
self.white_win = True
return
#Change turn
self.player1_turn = not self.player1_turn
self.move_number +=1
def move(self, start, end, move_array):
#Valid Size
if np.sum(move_array) > self.board_size:
raise Exception("Moving more tiles than board size")
count = np.sum(move_array)
current_square = start
to_check = [start]
#print("Move: s:{}, e:{}".format(start, end))
#Valid Move
if start[0] == end[0]:
#Up and Down
if int(start[1:]) > int(end[1:]):
#Down
#Set Start
x,y = self.get_x_y_from_grid(start)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
pop_array = cell[last_index-count+1:last_index+1].tolist()
#print(last_index-count+1,last_index+1, cell[last_index-count+1:last_index+1])
#Remove from stack
for a in range(count):
cell[last_index-a] = 0
for index, pops in enumerate(move_array):
current_square = current_square[0] + str(int(current_square[1:]) -1)
to_check.append(current_square)
if len(move_array) -1 == index and pops == 1 and len(pop_array) > 0:
self.check_for_wall_crush(current_square, pop_array)
x,y = self.get_x_y_from_grid(current_square)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
#print(x,y,current_square)
for a in range(pops):
if cell[last_index+a] == 0:
cell[last_index+a] = pop_array.pop(0)
else:
cell[last_index+a+1] = pop_array.pop(0)
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
if np.bitwise_and(cell[last_index],6) != 4:
if self.player1_turn == False:
if np.bitwise_and(cell[last_index], 1) == 1:
self.black_top[y][x] = True
else:
self.black_top[y][x] = False
else:
if np.bitwise_and(cell[last_index], 1) == 0:
self.white_top[y][x] = True
else:
self.white_top[y][x] = False
else:
self.white_top[y][x] = False
self.black_top[y][x] = False
else:
#Up
#Set Start
x,y = self.get_x_y_from_grid(start)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
pop_array = cell[last_index-count+1:last_index+1].tolist()
#print(last_index-count+1,last_index+1, cell[last_index-count+1:last_index+1])
#Remove from stack
for a in range(count):
cell[last_index-a] = 0
for index, pops in enumerate(move_array):
current_square = current_square[0] + str(int(current_square[1:]) +1)
to_check.append(current_square)
if len(move_array) -1 == index and pops == 1 and len(pop_array) > 0:
self.check_for_wall_crush(current_square, pop_array)
x,y = self.get_x_y_from_grid(current_square)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
#print(x,y,current_square)
for a in range(pops):
if cell[last_index+a] == 0:
cell[last_index+a] = pop_array.pop(0)
else:
cell[last_index+a+1] = pop_array.pop(0)
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
if np.bitwise_and(cell[last_index],6) != 4:
if self.player1_turn == False:
if np.bitwise_and(cell[last_index], 1) == 1:
self.black_top[y][x] = True
else:
self.black_top[y][x] = False
else:
if np.bitwise_and(cell[last_index], 1) == 0:
self.white_top[y][x] = True
else:
self.white_top[y][x] = False
else:
self.white_top[y][x] = False
self.black_top[y][x] = False
elif start[1:] == end[1:]:
#left and right
if start[0] > end[0]:
#Left
#Set Start
x,y = self.get_x_y_from_grid(start)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
pop_array = cell[last_index-count+1:last_index+1].tolist()
#print(last_index-count+1,last_index+1, cell[last_index-count+1:last_index+1])
#Remove from stack
for a in range(count):
cell[last_index-a] = 0
for index, pops in enumerate(move_array):
current_square = chr(ord(current_square[0]) - 1) + current_square[1:]
to_check.append(current_square)
if len(move_array) -1 == index and pops == 1 and len(pop_array) > 0:
self.check_for_wall_crush(current_square, pop_array)
x,y = self.get_x_y_from_grid(current_square)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
#print(x,y,current_square)
for a in range(pops):
if cell[last_index+a] == 0:
cell[last_index+a] = pop_array.pop(0)
else:
cell[last_index+a+1] = pop_array.pop(0)
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
if np.bitwise_and(cell[last_index],6) != 4:
if self.player1_turn == False:
if np.bitwise_and(cell[last_index], 1) == 1:
self.black_top[y][x] = True
else:
self.black_top[y][x] = False
else:
if np.bitwise_and(cell[last_index], 1) == 0:
self.white_top[y][x] = True
else:
self.white_top[y][x] = False
else:
self.white_top[y][x] = False
self.black_top[y][x] = False
else:
#Right
#Set Start
x,y = self.get_x_y_from_grid(start)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
pop_array = cell[last_index-count+1:last_index+1].tolist()
#print(last_index-count+1,last_index+1, cell[last_index-count+1:last_index+1])
#Remove from stack
for a in range(count):
cell[last_index-a] = 0
for index, pops in enumerate(move_array):
current_square = chr(ord(current_square[0]) + 1) + current_square[1:]
to_check.append(current_square)
if len(move_array) -1 == index and pops == 1 and len(pop_array) > 0:
self.check_for_wall_crush(current_square, pop_array)
x,y = self.get_x_y_from_grid(current_square)
cell = self.board[y,x]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
#print(x,y,current_square)
for a in range(pops):
if cell[last_index+a] == 0:
cell[last_index+a] = pop_array.pop(0)
else:
cell[last_index+a+1] = pop_array.pop(0)
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
if np.bitwise_and(cell[last_index],6) != 4:
if self.player1_turn == False:
if np.bitwise_and(cell[last_index], 1) == 1:
self.black_top[y][x] = True
else:
self.black_top[y][x] = False
else:
if np.bitwise_and(cell[last_index], 1) == 0:
self.white_top[y][x] = True
else:
self.white_top[y][x] = False
else:
self.white_top[y][x] = False
self.black_top[y][x] = False
else:
raise Exception("Move is not up, down, left, or right")
self.update_tops(to_check)
#Check for win
self.winner_move(to_check)
#Change turn
self.player1_turn = not self.player1_turn
self.move_number +=1
def update_tops(self, update):
for current_square in update:
x,y = self.get_x_y_from_grid(current_square)
cell = self.board[x,y]
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
if np.count_nonzero(cell) == 0 or (np.bitwise_and(cell[last_index],6) == 4):
self.white_top[x,y] = False
self.black_top[x,y] = False
elif (np.bitwise_and(cell[last_index],1) == 0):
self.white_top[x,y] = True
self.black_top[x,y] = False
else:
self.black_top[x,y] = True
self.white_top[x,y] = False
def check_for_wall_crush(self, current_square, pop_array):
#If last move and pops is 1
#Check if has capstone in peice
x,y = self.get_x_y_from_grid(current_square)
piece = pop_array[0]
cell = self.board[y,x]
last_index = (cell!=0).cumsum().argmax()
if not np.count_nonzero(cell) == 0:
wall = cell[last_index]
else:
return
if (np.bitwise_and(piece, 6) == 6) and np.bitwise_and(wall, 4) == 4:
#print("Capstone wall crush")
if np.count_nonzero(cell) == 0:
cell[last_index+1] = (int(np.bitwise_and(wall, 1)) +2)
else:
cell[last_index] = (int(np.bitwise_and(wall, 1)) +2)
###Helper Functions
def get_index_from_ints(self, x, y):
index = chr(ord("E") - y)
index += str(x+1)
return index
def get_index_from_int(self, z):
y = z // self.board_size
x = z % self.board_size
index = chr(ord("A") + y)
index += str(self.board_size - x)
return index
def get_current_string_board(self):
return self.board
def get_internal_cell(self, cell):
out_list = []
for element in cell:
for key, value in self.encode.items():
if value == element:
out_list.append(key)
break
return out_list[::-1]
def get_x_y_from_grid(self, grid_location):
#X is A-E
#Y is 1-5
x = (ord(grid_location[0].upper()) - ord("A"))
y = self.board_size - int(grid_location[1:])
return [x,y]
###Deep Learning Functions
def get_string_board(self):
board_array=[]
for row_index, rows in enumerate(self.board):
row_array = []
for cel_index, cell in enumerate(rows):
last_index = np.argmax(cell==0) -1
if last_index < 0:
last_index = 0
row_array.append(list(cell[:last_index]))
board_array.append(row_array)
return board_array
def set_np_game_board(self, move_board, player1_turn):
self.player1_turn = player1_turn
count = 0
#Get Rows
for x, row in enumerate(self.board):
for y, cell in enumerate(row):
move_cell = self.get_internal_cell(move_board[x][y])
count += len(move_cell)
self.board[x,y] = move_cell
self.move_number = count
def pack_move(self, move):
out = [0,0,0,0,0,0,0,0,0,0,0,0]
if move["movetype"] == "p":
#Move Type
out[0] = 1
#Piece
out[1] = self.encode[move["piece"].lower()]
temp_move = self.get_x_y_from_grid(move["placement"])
#X,Y placement
out[2] = temp_move[0]
out[3] = temp_move[1]
elif move["movetype"] == "m":
#Move Type
out[0] = 2
temp_move = self.get_x_y_from_grid(move["start"])
#X,Y Start stack
out[4] = temp_move[0]
out[5] = temp_move[1]
#Direction
out[6] = self.get_direction_from_start_end(move["start"], move["end"])
#Number of pieces
for x in range(len(move["order"])):
out[7+x] = move["order"][x]
else:
raise Exception("Invalid Move Type Result")
return out
def get_result_from_new_board(self, move_board):
move = self.get_move_from_new_board(move_board)
return self.pack_move(move)
def get_direction_from_start_end(self, start, end):
size = 5
#direction lowest is bottom left
start_int = size * int(start[1:]) + (ord('a') - ord(start[0].lower()))
end_int = size * int(end[1:]) + (ord('a') - ord(end[0].lower()))
if start_int > end_int:
# Move Down or Left
if end_int > start_int - size:
#Move Down
return 3
else:
#Move Left
return 4
else:
#Move Up or Right
if end_int >= start_int + size:
#Move Up
return 1
else:
#Move Right
return 2
def get_move_from_new_board(self, move_board):
changes = []
#Get Rows
for x, row in enumerate(self.board):
for y, cell in enumerate(row):
#Convert cell to be compared
move_cell = self.get_internal_cell(move_board[x][y])
if len(cell) == len(move_cell):
if cell != move_cell:
#print("Change in the elements at the index x:{}, y:{}".format(x, y))
#print("MoveCell: {}".format(move_cell))
#print("Cell: {}".format(cell))
changes.append({'x':x,'y':y, "move_cell": move_cell, "cell": cell, "index": self.get_index_from_ints(x,y), "diff": len(cell) - len(move_cell)})
else:
#print("Change in number of elements at index x:{}, y:{}".format(x, y))
#print("MoveCell: {}".format(move_cell))
#print("Cell: {}".format(cell))
changes.append({'x':x,'y':y, "move_cell": move_cell, "cell": cell, "index": self.get_index_from_ints(x,y), "diff": len(cell) - len(move_cell)})
#Place
#print(changes)
if len(changes) == 1:
change = changes[0]
if len(change["move_cell"]) == 1:
#print("[Place] {} {}".format("", change["index"]))
self.place("", change["index"])
else:
#print("[Place] {} {}".format(change["move_cell"][0], change["index"]))
self.place(change["move_cell"][0], change["index"])
return {"movetype": "p", "piece": change["move_cell"][0], "placement":change["index"]}
else:
#Move
movement_array = [row for row in changes]
start = ""
end = ""
reverse = False
for index, change in enumerate(changes):
if change["diff"] > 0:
#print("Start is " + change["index"])
start = change["index"]
movement_array.pop(index)
if index == 0:
movement_array = movement_array[::-1]
end = changes[-1]["index"]
else:
end = changes[0]["index"]
#print(changes[0])
break
count_array = []
for elem in movement_array:
count_array.append(abs(elem["diff"]))
#print("[Move] Start: {}, End: {}, Array: {}".format(start, end, count_array))
self.move(start, end, count_array)
return {"movetype": "m", "start": start, "end": end, "order": count_array}
###Tests
def game1():
p= TakBoard(5)
p.place("", "D4")
p.place("", "C3")
p.place("C", "D3")
p.place("C", "C4")
p.place("S", "D5")
p.place("S", "B4")
#for x in p.get_current_string_board():
# print(x)
for x in p.get_plays():
print(x)
p.move("D5", "D4", [1])
#for x in p.get_current_string_board():
# print(x)
p.move("C4", "B4", [1])
p.place("", "C4")
p.move("B4", "D4", [1, 1])
print(p.white_win)
print(p.black_win)
def game2():
p= TakBoard(5)
p.place("", "E1")
p.place("", "D1")
p.place("", "D2")
p.place("", "D3")
p.place("", "C2")
#for x in p.get_plays():
# print(x)
p.place("", "E2")
p.place("", "E3")
p.place("", "D4")
p.place("", "B2")