forked from marielean/AnalogChess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unified_file.py
2299 lines (1977 loc) · 83.5 KB
/
unified_file.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
import random, math, pygame
from pygame import gfxdraw
class Board:
def __init__(self, pieces = True, granularity = 1):
'''
### init method to create new Board instance.
pieces: boolean flag. It must be true if you want to create a new board with all the pieces, false otherway. Default=True.
granularity: number of points for each direction. Default=1.
'''
self.pieces = []
self.granularity = granularity
if pieces:
self.new_board()
else:
self.finite_pieces()
self.whiteTurn = True
def set_pieces(self, new_pieces):
'''
Board.set_pieces(self, new_pieces)
Metodo che setta i pezzi della Board a un nuovo stato.
new_pieces: lista di istanze Piece presenti nel nuovo stato
'''
self.pieces = []
for p in new_pieces:
if isinstance(p, Pawn):
self.pieces.append(Pawn(p.start_x, p.start_y, p.color))
elif isinstance(p, Rook):
self.pieces.append(Rook(p.start_x, p.start_y, p.color))
elif isinstance(p, Knight):
self.pieces.append(Knight(p.start_x, p.start_y, p.color))
elif isinstance(p, Bishop):
self.pieces.append(Bishop(p.start_x, p.start_y, p.color))
elif isinstance(p, Queen):
self.pieces.append(Queen(p.start_x, p.start_y, p.color))
elif isinstance(p, King):
self.pieces.append(King(p.start_x, p.start_y, p.color))
else:
print("ERROR: Piece not found")
# metodo che resituisce se la partita è finita
def is_terminal(self):
cont = 0
for p in self.get_pieces():
if isinstance(p, King) and p.deleted == False:
cont += 1
print("cont: ", cont)
return True if cont < 2 else False
def apply_move(self, move):
'''
## applica una mossa sulla scacchiera cambiando lo stato della board modificando le coordinate di un pezzo\n
board: stato della scacchiera\n
restituisce la stessa istanza della board ma con le coordinate modificate del pezzo interessato
'''
pieces = self.get_pieces()
# print(pieces)
for i in range(len(pieces)):
if (pieces[i].id == move[0]) and (pieces[i].color == move[1]) and (pieces[i].start_x == move[2][0]) and (pieces[i].start_y == move[2][1]):
# Move the piece
pieces[i].x = move[3][0]
pieces[i].y = move[3][1]
pieces[i].start_x = move[3][0]
pieces[i].start_y = move[3][1]
for j in range(len(pieces)):
if (j != i) and (pieces[i].color != pieces[j].color) and (((pieces[i].start_x - pieces[j].start_x)**2 + (pieces[i].start_y - pieces[j].start_y)**2) < (2*pieces[i].radius)**2):
pieces[j].deleted = True
pieces[j].start_x, pieces[j].x = 100, 100 # Render outside the board
break
self.__update_state() # Update state
def __update_state(self):
new_pieces_list = []
for p in self.get_pieces():
if p.can_promote():
new_pieces_list.append(Queen(p.start_x, p.start_y, p.color))
else:
new_pieces_list.append(p)
self.set_pieces(new_pieces_list)
for p in self.get_pieces():
p.calc_paths(self.get_pieces())
def set_turn(self, whiteTurn: bool):
self.whiteTurn = whiteTurn
def is_white_turn(self):
return self.whiteTurn
def finite_pieces(self):
self.pieces = [
King(1.5,1.5,white),
King(6.5,6.5,black),
Bishop(4,2,white),
Bishop(3,7,black),
]
def new_board(self):
self.pieces = [
Rook(0.5, 0.5, white),
Rook(7.5, 0.5, white),
Knight(1.5, 0.5, white),
Knight(6.5, 0.5, white),
Bishop(5.5, 0.5, white),
Bishop(2.5, 0.5, white),
King(4.5, 0.5, white),
Queen(3.5, 0.5, white),
Pawn(0.5, 1.5, white),
Pawn(1.5, 1.5, white),
Pawn(2.5, 1.5, white),
Pawn(3.5, 1.5, white),
Pawn(4.5, 1.5, white),
Pawn(5.5, 1.5, white),
Pawn(6.5, 1.5, white),
Pawn(7.5, 1.5, white),
Rook(0.5, 7.5, black),
Rook(7.5, 7.5, black),
Knight(1.5, 7.5, black),
Knight(6.5, 7.5, black),
Bishop(5.5, 7.5, black),
Bishop(2.5, 7.5, black),
King(4.5, 7.5, black),
Queen(3.5, 7.5, black),
Pawn(0.5, 6.5, black),
Pawn(1.5, 6.5, black),
Pawn(2.5, 6.5, black),
Pawn(3.5, 6.5, black),
Pawn(4.5, 6.5, black),
Pawn(5.5, 6.5, black),
Pawn(6.5, 6.5, black),
Pawn(7.5, 6.5, black),
]
# self.pieces = [
# Rook(0.5, 0.5, white),
# Knight(1.5, 0.5, white),
# King(4.5, 0.5, white),
# Pawn(0.5, 1.5, white),
# Rook(0.5, 7.5, black),
# Knight(6.5, 7.5, black),
# King(4.5, 7.5, black),
# Pawn(0.5, 6.5, black)
# ]
#restituisce lo stato della scacchiera con le posizioni di tutti i pezzi ancora in gioco diviso per colori
def get_chess_board_status(self):
white_status = []
black_status = []
for piece in self.get_pieces():
if piece.color == white and piece.deleted == False:
white_status.append([piece.id, piece.start_x, piece.start_y])
elif piece.color == black and piece.deleted == False:
black_status.append([piece.id, piece.start_x, piece.start_y])
return white_status, black_status
def get_pieces(self):
return self.pieces
# grazie a questa funzione si ottengono tutte le mosse possibili per un pezzo in base alla sua direzione
# granularity è il numero di punti che si vogliono ottenere per ogni direzione
def get_points_from_distance(self, x_start, y_start, x_end, y_end, knight_flag=False):
'''
get_points_from_distance(x_start, y_start, x_end, y_end, knight_flag=False)
The function returns all the possible moves in the following format:
[(x1, y1), (x2, y2), ...]
x_start: starting x of the path (current x position if the piece is a knight)
y_start: starting y of the path (current y position if the piece is a knight)
x_end: final x of the path (start angle if the piece is a knight)
y_end: final y of the path (final angle if the piece is a knight)
knight_flag: boolean flag. It must be true if the piece is a knight, false otherway. Default=False.
'''
list_points = []
x_new, y_new = None, None
if not knight_flag:
# If the piece is not a knight, the movement can be on a point in a line.
# In this case (x_start, y_start) are the coordinate of the first point of the line and (x_end, y_end) is the final point
dx = (x_end - x_start)/self.granularity
dy = (y_end - y_start)/self.granularity
x_new = dx*np.arange(1, self.granularity+1) + x_start
y_new = dy*np.arange(1, self.granularity+1) + y_start
else:
# If the piece is a knight, the movements are on a circonference arc with center (x_start, y_start).
# The variables x_end, y_end in this case are not really x and y but are the angles (in rad) of the first point of arc and of the last.
# In this sense we will rename them for a better readability. Finally, note that the angles are calculated from the positive semi-axis of y.
start_ang, end_ang = x_end, y_end
radius = math.sqrt(5) # I remember that the radius of the knight is sqrt(5) but you can change it if it is wrong
if self.granularity == 1:
x_new = np.cos(-(np.array([end_ang])-np.pi/2))*radius + x_start
y_new = np.sin(-(np.array([end_ang])-np.pi/2))*radius + y_start
else:
delta = (end_ang - start_ang)/(self.granularity-1) # Angles variation (Let's do an example to explain why I have used granularity-1 as denominator. In the simplest case with only 2 points for arc, I want that the points are the first and the last points of the arc. Then, the delta have to be the total length of the arc.)
x_new = np.cos(-(np.arange(self.granularity)*delta+start_ang-np.pi/2))*radius + x_start
y_new = np.sin(-(np.arange(self.granularity)*delta+start_ang-np.pi/2))*radius + y_start
# The two previous lines calculate the coordinates x and y of the avaiable moves from the possible angles.
# The main problems are that the angles are calculated from the positive y semi-axis and that are considered positives the clock-wise angles (the opposite of "normal" algebra).
# Then the formulas are not very trivial... The idea is to firstly transform the angles in a "conventional" representation and then compute the sin or cos.
list_points = [(x_new[i], y_new[i]) for i in range(len(x_new))]
return list_points
# restituisce tutte le mosse possibili per tutti i pezzi presenti nella list_pieces nella forma [id, colore, (x_start, y_start), [(x_end, y_end), (x_end, y_end), ...]]
def get_all_moves_from_distance(self, list_pieces):
#print("list_pieces: ", list_pieces)
list_moves = []
for curr_piece in list_pieces:
# curr_piece is [piece_name, color, current_position, final_positions_list]
list_moves.append([curr_piece[0], curr_piece[1], curr_piece[2], []]) # the last empty list will contain the possible future moves
for final_pos in curr_piece[3]:
is_knight = (curr_piece[0]==knight)
list_point = self.get_points_from_distance(curr_piece[2][0], curr_piece[2][1], final_pos[0], final_pos[1], knight_flag=is_knight)
list_moves[-1][-1] += list_point # list_moves
return list_moves
# restituisce tutte le direzioni di tutti i pezzi presenti diviso per colori nella forma [id, colore, (x_start, y_start), [(x_end, y_end), (x_end, y_end), ...]]
def get_all_directions_all_in_one(self):
'''
DA FARE: non ha senso passare pieces visto che siamo nella classe board.
Dopo aver adattato tutto si può togliere. Nel mentre ho messo un valore
default e si può non usare il parametro (scelta migliore).
'''
pieces = self.get_pieces()
list_directions_white = []
list_directions_black = []
for p in pieces:
if not p.deleted:
if p.color == black:
direction = p.get_all_directions_per_piece(pieces)
list_directions_black.append([p.id, p.color, (p.start_x, p.start_y), direction])
elif p.color == white:
direction = p.get_all_directions_per_piece(pieces)
list_directions_white.append([p.id, p.color, (p.start_x, p.start_y), direction])
return list_directions_white, list_directions_black
def get_all_moves(self, turn):
'''
board.get_all_moves(self, turn)
This method returns all the possible moves for the selected player in
the following format: [id, color, (x_start, y_start), [(x_end, y_end),
(x_end, y_end), ...]]
turn: boolean that is true if is white turn, false if it is black turn
'''
white_directions, black_directions = self.get_all_directions_all_in_one()
if turn:
'''
white turn
'''
return self.get_all_moves_from_distance(white_directions)
'''
black turn
'''
return self.get_all_moves_from_distance(black_directions)
class IA:
def __init__(self, utility=custom_heuristic_0, algorithm = 'AlphaBeta', depth = 1):
'''
Possible algorithms: MiniMax, AlphaBeta, Random
'''
self.whiteTurn = True
self.utility = utility
self.algorithm = algorithm
self.depth = depth
def set_turn(self, whiteTurn):
self.whiteTurn = whiteTurn
# function for best move
def get_best_move(self, board):
if self.algorithm == "MiniMax":
return self.__minimax_search(board)
elif self.algorithm == "AlphaBeta":
return self.__alphabeta_search(board)
elif self.algorithm == "Random":
return self.__random_search(board)
def __random_search(self, board: Board):
"""
Selects a random move from the valid moves for the current players turn
:param board: the current board being used for the game (pieces)
:return: list representing move; format: [id, color, (x_start, y_start), (x_end, y_end)]
"""
list_moves = board.get_all_moves(board.is_white_turn())
random_piece = random.choice(list_moves)
r_move = random.choice(random_piece[-1])
move = [random_piece[0], random_piece[1], random_piece[2], r_move]
return move
def __minimax_search(self, board: Board):
'''
Metodo che implementa la funzione minimax.
board: current board instance (current state)
depth: maximum depth of the algorithm
'''
max_player = board.is_white_turn()
depth = self.depth
def max(curr_board, depth):
if (depth == 0 or curr_board.is_terminal()):
return None, self.utility(curr_board, max_player)
# initialization
max_value = -np.inf
max_move = None
# algorithm iteration (max is the turn player)
possible_moves = curr_board.get_all_moves(max_player)
for piece in possible_moves:
# print(piece)
for next_position in piece[3]:
move = [piece[0],piece[1],piece[2],next_position]
next_board = Board(curr_board.get_pieces())
next_board.apply_move(move)
_, value = min(next_board, depth-1)
if value > max_value:
max_value = value
max_move = move
return max_move, max_value
def min(curr_board, depth):
if (depth == 0 or curr_board.is_terminal()):
return None, self.utility(curr_board, max_player)
# initialization
min_value = np.inf
min_move = None
# algorithm iteration (min is the turn player)
possible_moves = curr_board.get_all_moves(not max_player)
for piece in possible_moves:
for next_position in piece[3]:
move = [piece[0],piece[1],piece[2],next_position]
next_board = Board(curr_board.get_pieces())
next_board.apply_move(move)
_, value = max(next_board, depth-1)
if value > min_value:
min_value = value
min_move = move
return min_move, min_value
move, value = max(board, depth)
return move
def __alphabeta_search(self, board: Board):
'''
Metodo che implementa l'algoritmo alpha-beta.
board: current board instance (current state)
'''
max_player = board.is_white_turn()
def max(curr_board, alpha, beta, depth):
if (depth == 0 or curr_board.is_terminal()):
return None, self.utility(curr_board, max_player)
# initialization
max_value = -np.inf
max_move = None
# algorithm iteration (max is the turn player)
possible_moves = curr_board.get_all_moves(max_player)
for piece in possible_moves:
# print(piece)
for next_position in piece[3]:
move = [piece[0],piece[1],piece[2],next_position]
next_board = Board(curr_board.get_pieces())
next_board.apply_move(move)
_, value = min(next_board, alpha, beta, depth-1)
if value > max_value:
max_value = value
max_move = move
if max_value >= beta:
return max_move, max_value
if max_value >= alpha:
alpha = max_value
return max_move, max_value
def min(curr_board, alpha, beta, depth):
if (depth == 0 or curr_board.is_terminal()):
return None, self.utility(curr_board, max_player)
# initialization
min_value = np.inf
min_move = None
# algorithm iteration (min is the turn player)
possible_moves = curr_board.get_all_moves(not max_player)
for piece in possible_moves:
for next_position in piece[3]:
move = [piece[0],piece[1],piece[2],next_position]
next_board = Board(curr_board.get_pieces())
next_board.apply_move(move)
_, value = max(next_board, alpha, beta, depth-1)
if value < min_value:
min_value = value
min_move = move
if min_value <= alpha:
return min_move, min_value
if min_value < beta:
beta = min_value
return min_move, min_value
move, value = max(board, -np.inf, np.inf, self.depth)
return move
class Piece:
width, height = 640, 640
size = (width, height)
def dist(p1, p2):
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def to_game_coords(p):
return (p[0] / width * 8, 8 - p[1] / height * 8)
def to_screen_coords(p):
return (p[0] / 8 * width, height - p[1] / 8 * width)
def clamp(n, smallest, largest):
return max(smallest, min(n, largest))
black = (0, 0, 0)
white = (255, 255, 255)
light_gray = (255, 222, 173)
dark_gray = (222, 184, 135)
RED_HIGHLIGHT = (240, 50, 50, 150)
GREEN_HIGHLIGHT = (0, 255, 0, 80)
pawn = "P"
rook = "R"
knight = "K"
bishop = "B"
queen = "Q"
king = "Ki"
depth_size = 1
Radius = math.sqrt(5)
screen = pygame.display.set_mode(size)
see_through = pygame.Surface((width, height)).convert_alpha()
see_through2 = pygame.Surface((width, height)).convert_alpha()
see_through.fill((0, 0, 0, 0))
def get_fontname():
# Clever way to get the best font for the system (from @andychase)
# font_options = ["segoeuisymbol", "applesymbols", "DejaVuSans"]
# font_to_use = font_options[0]
font_to_use = "DejaVuSans"
# for font in font_options:
# if font in pygame.font.get_fonts():
# font_to_use = font
return font_to_use
def draw_checkers():
for i in range(8):
for j in range(8):
size = width // 8
color = dark_gray
if (i + j) % 2 == 0:
color = light_gray
pygame.draw.rect(screen, color, (i * size, j * size, size, size))
def draw_circle(surface, x, y, radius, color):
gfxdraw.aacircle(surface, x, y, radius, color)
gfxdraw.filled_circle(surface, x, y, radius, color)
def pygame_draw_circle(surface, color, screen_coords, radius, **kwargs):
pygame.draw.circle(surface, color, screen_coords, radius, **kwargs)
def draw_circle_outline(surface, x, y, radius, color):
gfxdraw.aacircle(surface, x, y, radius, color)
gfxdraw.circle(
surface, x, y, radius, (255 - color[0], 255 - color[1], 255 - color[2])
)
def draw_center_text(text):
screen.blit(
text,
(
width // 2 - text.get_width() // 2,
height // 2 - text.get_height() // 2,
),
)
def draw_line_round_corners_polygon(surf, p1, p2, c, w):
if p1 != p2:
p1v = pygame.math.Vector2(p1)
p2v = pygame.math.Vector2(p2)
lv = (p2v - p1v).normalize()
lnv = pygame.math.Vector2(-lv.y, lv.x) * w // 2
pts = [p1v + lnv, p2v + lnv, p2v - lnv, p1v - lnv]
pygame.draw.polygon(surf, c, pts)
pygame.draw.circle(surf, c, p1, round(w / 2))
pygame.draw.circle(surf, c, p2, round(w / 2))
else:
pygame.draw.circle(surf, c, p1, round(w / 2))
def getpolygon(origin, radius, N, start=0, end=None):
out = []
x, y = origin
Nf = float(N)
if end is None:
end = math.pi * 2
for i in range(N):
xp = x + radius * math.sin(end * i / Nf + start)
yp = y - radius * math.cos(end * i / Nf + start)
out.append((xp, yp))
return out
def arc(surf, color, origin, radius, start=0, end=None, width=0, N=64):
if width == 0 or width >= radius * 0.5:
p2 = [origin]
else:
p2 = getpolygon(origin, radius - width, N, start=start, end=end)
p2.reverse()
p1 = getpolygon(origin, radius, N, start=start, end=end)
p1.extend(p2)
r = pygame.draw.polygon(surf, color, p1)
return r
def mean_path(piece):
'''
This function simpy returns the mean path length for piece given the piece. I write
this information here because the mean path length is not a deterministic
value but depends on the specific game. The expected value returned by this
function is an hypothesis and technically represents an hyperparameter of
the AI algorithm.
'''
if isinstance(piece, Pawn): return 1.2
elif isinstance(piece, King): return 0.5
elif isinstance(piece, Rook): return 10
elif isinstance(piece, Queen): return 15
elif isinstance(piece, Knight): return 7
elif isinstance(piece, Bishop): return 10
def total_path_len(piece, edge_positions):
'''
total_path_len(curr_pos, edge_positions, weight, is_knight)
This function calculates the total length of the avaiable path for a given
piece. The length is simply the length of a line for all pieces except for
the knight. In the last case the length is the length of the correspondents
arc.
piece: current piece object
edge_positions: list of the edge positions [(x1, y1), (x2, y2), ...]. For
the knight are the edge angles (NB. the list is returned by the method
get_all_directions_per_piece of piece class)
'''
curr_pos = (piece.x, piece.y)
weight = piece.weight
is_knight = isinstance(piece, Knight)
total_len = 0
if is_knight:
radius = np.sqrt(5)
for edge_pos in edge_positions:
total_len += weight*(radius*np.abs(edge_pos[1]-edge_pos[0]))/mean_path(piece) # A possibility is to set a penality. For example it can be related to the possiblity that the piece could be eatten (it is not easy to do)
else:
for edge_pos in edge_positions:
total_len += weight*np.sqrt((curr_pos[0] - edge_pos[0])**2 + (curr_pos[1] - edge_pos[1])**2)/mean_path(piece)
return total_len
def custom_heuristic_1(board, player):
'''
custom_heuristic_1(pieces, player)
This function calculates a score for players based on a custom heuristic
function. The function is based on the total length of the avaiable path per
piece, weighted on the piece weight, and normalized on the mean path of the piece
'''
pieces = board.get_pieces()
white_score = 0 # white score
black_score = 0 # white score
for piece in pieces:
if piece.color == white and piece.deleted == False:
white_score += total_path_len(piece, piece.get_all_directions_per_piece(pieces))
if piece.color == black and piece.deleted == False:
black_score += total_path_len(piece, piece.get_all_directions_per_piece(pieces))
return white_score-black_score if player else black_score-white_score
def custom_heuristic_0(board, player):
'''
custom_heuristic_0(pieces, player)
This function calculates a score for players based on a custom heuristic
function. The function is based on the pieces weight difference
'''
pieces = board.get_pieces()
white_score = 0
black_score = 0
for piece in pieces:
if piece.color == white and piece.deleted == False:
white_score += piece.weight
if piece.color == black and piece.deleted == False:
black_score += piece.weight
return white_score-black_score if player else black_score-white_score
# x pos and y pos are on a grid of size 8, normal cartesian coordinates
def __init__(self, x_pos, y_pos, color):
diameter = 0.7
self.x = x_pos
self.y = y_pos
self.radius = diameter / 2
self.grabbed = False
self.targeted = False
self.color = color
self.start_x = self.x
self.start_y = self.y
text_scale = 0.85
self.letter = "X"
self.id = "XX"
self.font = pygame.font.SysFont(
get_fontname(), int(diameter / 8 * 640 * text_scale)
)
self.text = self.font.render(self.letter, True, (255, 255, 255))
self.direction = False
self.targeted = False
self.turn = 0
self.deleted = False
self.weight = 0
self.white_turn = True
def delete(self):
del self
def set_id(self, id):
self.id = id
def get_turn(self):
return self.white_turn
def set_weight(self, weight):
self.weight = weight
def set_letter(self, letter):
self.letter = letter
if not self.grabbed:
self.text = self.font.render(
self.letter,
True,
(255 - self.color[0], 255 - self.color[1], 255 - self.color[2]),
)
else:
self.text = self.font.render(self.letter, True, (0, 255, 0))
def can_promote(self):
return False
#this is only used by the Knight so we can only calculate paths once instead of every frame
def calc_paths(self,pieces):
pass
def draw_paths(self, pieces):
pass
def target(self):
self.targeted = True
self.text = self.font.render(self.letter, True, (255, 0, 0))
def untarget(self):
self.targeted = False
self.set_letter(self.letter)
def draw(self):
x = int(self.x / 8 * width)
y = height - int(self.y / 8 * height)
# draw_circle(screen,x,y,int(self.radius/8*width),(255-self.color[0],255-self.color[1],255-self.color[2]))
draw_circle(screen, x, y, int(self.radius / 8 * width), self.color)
screen.blit(
self.text,
(x - self.text.get_width() // 2, y - 2 - self.text.get_height() // 2),
)
def try_grab(self, pos):
if dist(pos, (self.x, self.y)) < self.radius:
self.grabbed = True
self.text = self.font.render(self.letter, True, (0, 255, 0))
def cancel(self, pieces):
if self.grabbed:
self.grabbed = False
for piece in pieces:
if piece.targeted:
piece.untarget()
self.direction = False
self.text = self.font.render(
self.letter,
True,
(255 - self.color[0], 255 - self.color[1], 255 - self.color[2]),
)
self.x = self.start_x
self.y = self.start_y
def confirm(self, pieces):
new_pieces = []
if self.grabbed:
self.grabbed = False
for piece in pieces:
if piece.targeted:
piece.deleted = True
piece.x = 100
piece.start_x = 100
piece.delete()
else:
new_pieces.append(piece)
self.direction = False
self.text = self.font.render(
self.letter,
True,
(255 - self.color[0], 255 - self.color[1], 255 - self.color[2]),
)
self.start_x = self.x
self.start_y = self.y
self.turn += 1
pieces = new_pieces
return new_pieces
def ungrab(self, pieces):
if self.grabbed:
if (
abs(self.x - self.start_x) < 1 / 1000
and abs(self.y - self.start_y) < 1 / 1000
):
self.cancel(pieces)
return
font = pygame.font.SysFont("oldenglishtext", int(80))
confirm_text = font.render("Confirm?", True, black)
draw_center_text(confirm_text)
pygame.display.flip()
# while not done:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONUP:
if (dist(to_game_coords(pygame.mouse.get_pos()), (self.x, self.y)) < self.radius):
self.confirm(pieces)
print("Pezzo giocato", self.id)
if self.white_turn and self.color == white:
#print("white turn, next turn black")
self.white_turn = False
for one_piece in pieces:
one_piece.white_turn = False
return True
elif not self.white_turn and self.color == black:
#print("black turn, next turn white")
self.white_turn = True
for one_piece in pieces:
one_piece.white_turn = True
return True
# print("confirm", self.white_turn)
return True
else:
self.cancel(pieces)
# print("cancel", self.white_turn)
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
self.confirm(pieces)
print("confirm")
return True
elif event.key == pygame.K_ESCAPE:
self.cancel(pieces)
print("cancel")
return False
def overlaps(self, piece):
return dist((self.x, self.y), (piece.x, piece.y)) < self.radius * 2
def apply_granularity(self, coordinate, granularity=1):
rounded = round(coordinate, granularity)
return rounded
# math shit
def slide(self, dx, dy, pieces, capture=True, fake=False):
dx = self.apply_granularity(dx)
dy = self.apply_granularity(dy)
all_pieces = pieces
if capture:
pieces = [
p
for p in pieces
if (p.x - self.start_x) * dx + (p.y - self.start_y) * dy > 0
and p != self
and p.color == self.color
]
if fake:
pieces = [
p
for p in pieces
if (p.x - self.start_x) * dx + (p.y - self.start_y) * dy > 0
and p != self
and p.color == self.color
and p.targeted == False
]
else:
pieces = [
p
for p in pieces
if (p.x - self.start_x) * dx + (p.y - self.start_y) * dy > 0
and p != self
]
angle = math.atan2(dy, dx)
# resolve wall collisions
# dont do this if the piece is off the board it wont work right
if 0 <= self.start_x <= 8 and 0 <= self.start_y <= 8:
if abs(dx) > 0:
if self.start_x + dx + self.radius > 8:
ratio = dy / dx
dx = (8 - self.start_x) - self.radius
dy = ratio * ((8 - self.start_x) - self.radius)
if self.start_x + dx - self.radius < 0:
ratio = dy / dx
dx = -self.start_x + self.radius
dy = ratio * (-self.start_x + self.radius)
if abs(dy) > 0:
if self.start_y + dy + self.radius > 8:
ratio = dx / dy
dy = (8 - self.start_y) - self.radius
dx = ratio * ((8 - self.start_y) - self.radius)
if self.start_y + dy - self.radius < 0:
ratio = dx / dy
dy = -self.start_y + self.radius
dx = ratio * (-self.start_y + self.radius)
first_block = False
block_dist = 99999999
block_perp_dist = 999999999
full_dist = math.sqrt(dx**2 + dy**2)
new_dist = full_dist
# find first piece that intersects with the line of travel. Move it back behind this piece.
for piece in pieces:
# formula for distance from point to line
h = abs(
math.cos(angle) * (self.y - piece.y)
- math.sin(angle) * (self.x - piece.x)
)
if h < piece.radius * 2:
proj_dist = math.sqrt(
dist((self.start_x, self.start_y), (piece.x, piece.y)) ** 2 - h**2
)
if proj_dist < block_dist:
block_dist = proj_dist
block_perp_dist = h
first_block = piece
hit_first_block = False
if first_block:
distance = dist(
(first_block.x, first_block.y), (self.start_x + dx, self.start_y + dy)
)
if math.sqrt(dx**2 + dy**2) > block_dist:
hit_first_block = True
new_dist = block_dist - math.sqrt(
4 * self.radius**2 - block_perp_dist**2
)
if abs(full_dist) > 0:
self.x = self.start_x + dx * new_dist / full_dist
self.y = self.start_y + dy * new_dist / full_dist
new_new_dist = new_dist
first_hit_piece = False
# Still could be colliding with pieces, check collisions with all other pieces and move it behind minimum distance collision
for piece in pieces:
if self.overlaps(piece):
block_perp_dist = abs(
math.cos(angle) * (self.y - piece.y)
- math.sin(angle) * (self.x - piece.x)
)
block_dist = math.sqrt(
dist((self.start_x, self.start_y), (piece.x, piece.y)) ** 2
- block_perp_dist**2
)
new_new_dist = block_dist - math.sqrt(
4 * self.radius**2 - block_perp_dist**2
)
if new_new_dist < new_dist:
new_dist = new_new_dist
first_hit_piece = piece
if abs(full_dist) > 0:
self.x = self.start_x + dx * new_dist / full_dist
self.y = self.start_y + dy * new_dist / full_dist
else:
self.x = self.start_x
self.y = self.start_y
if capture:
self.slide_attack(
(self.x - self.start_x), self.y - self.start_y, all_pieces, fake=fake
)
'''print("letter, ", self.letter, "start_x ", self.start_x, "start_y, ", self.start_y, "dx ", dx,
", ", "dy ", dy, ", \n", "new_dist ", new_dist, ", ", "new_new_dist", new_new_dist,
"full_dist ", full_dist, "first_hit_piece ", first_hit_piece,
"\nfirst_block ", first_block, "hit_first_block ", hit_first_block)'''
def slide_attack(self, dx, dy, pieces, fake=False):
angle = math.atan2(dy, dx)
all_pieces = pieces
pieces = [
p
for p in pieces
if (p.x - self.start_x) * dx + (p.y - self.start_y) * dy > 0
and p != self
and p.color != self.color
]
first_piece_hit = False
first_hit_dist = 99999999
perp_dist = 999999999
full_dist = math.sqrt(dx**2 + dy**2)
new_dist = full_dist
# find piece that will be hit first
for piece in pieces:
# formula for distance from point to line
h = abs(
math.cos(angle) * (self.y - piece.y)
- math.sin(angle) * (self.x - piece.x)
)
if h < piece.radius * 2:
d = dist((piece.x, piece.y), (self.start_x, self.start_y))
hit_dist = math.sqrt(d**2 - h**2) - math.sqrt(
4 * piece.radius**2 - h**2
)