-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.py
149 lines (136 loc) · 5.42 KB
/
play.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
import chess.polyglot
import pygame
import chess
from eval import get_legal_moves_scores, its_draw
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((1024, 1024))
pygame.display.set_caption("Chess Engine")
screen.fill("WHITE")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Load images
chessboard = pygame.image.load('graph/chessboard.png').convert_alpha()
King = pygame.image.load('graph/WKing.png').convert_alpha()
king = pygame.image.load('graph/BKing.png').convert_alpha()
Knight = pygame.image.load('graph/WKnight.png').convert_alpha()
knight = pygame.image.load('graph/BKnight.png').convert_alpha()
Rook = pygame.image.load('graph/WRook.png').convert_alpha()
rook = pygame.image.load('graph/BRook.png').convert_alpha()
Queen = pygame.image.load('graph/WQueen.png').convert_alpha()
queen = pygame.image.load('graph/BQueen.png').convert_alpha()
Bishop = pygame.image.load('graph/WBishop.png').convert_alpha()
bishop = pygame.image.load('graph/BBishop.png').convert_alpha()
Pawn = pygame.image.load('graph/WPawn.png').convert_alpha()
pawn = pygame.image.load('graph/BPawn.png').convert_alpha()
def draw(fen, col, rank, board):
if fen == 'K':
if board.turn == chess.WHITE and board.is_check():
pygame.draw.circle(screen, RED, (rank + 67, col + 67), 60)
screen.blit(King, (rank, col))
elif fen == 'k':
if board.turn == chess.BLACK and board.is_check():
pygame.draw.circle(screen, RED, (rank + 67, col + 67), 60)
screen.blit(king, (rank, col))
elif fen == 'Q':
screen.blit(Queen, (rank, col))
elif fen == 'q':
screen.blit(queen, (rank, col))
elif fen == 'R':
screen.blit(Rook, (rank, col))
elif fen == 'r':
screen.blit(rook, (rank, col))
elif fen == 'N':
screen.blit(Knight, (rank, col))
elif fen == 'n':
screen.blit(knight, (rank, col))
elif fen == 'B':
screen.blit(Bishop, (rank, col))
elif fen == 'b':
screen.blit(bishop, (rank, col))
elif fen == 'P':
screen.blit(Pawn, (rank, col))
elif fen == 'p':
screen.blit(pawn, (rank, col))
def show(FEN, board):
screen.blit(chessboard, (0, 0))
col = 0
rank = 0
for fen in FEN:
if fen == '/':
col = col + 1
rank = 0
elif fen in ('1', '2', '3', '4', '5', '6', '7', '8'):
rank = rank + int(fen)
elif fen in ('K', 'k', 'Q', 'q', 'R', 'r', 'N', 'n', 'B', 'b', 'P', 'p'):
draw(fen, col * 127, rank * 127, board)
rank = rank + 1
pygame.display.update()
# Convert screen coordinates to board coordinates
def screen_to_board(x, y):
return chess.square(x // 127, 7 - y // 127)
# Convert board coordinates to screen coordinates
def board_to_screen(square):
rank = chess.square_rank(square)
file = chess.square_file(square)
return (file * 127, (7 - rank) * 127)
# Function to display the promotion options
def display_promotion_options(square):
options = [Queen, Rook, Bishop, Knight]
option_names = [chess.QUEEN, chess.ROOK, chess.BISHOP, chess.KNIGHT]
x, y = board_to_screen(square)
y_offset = 127 if b.turn == chess.WHITE else -127
for i, option in enumerate(options):
screen.blit(option, (x, y + i * y_offset))
pygame.display.update()
return option_names
# Function to handle the promotion selection
def get_promotion_choice(square):
option_names = display_promotion_options(square)
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
choice_index = (y - board_to_screen(square)[1]) // 127
if b.turn == chess.WHITE:
if 0 <= choice_index < len(option_names):
return option_names[choice_index]
else:
if -len(option_names) < choice_index <= 0:
return option_names[choice_index + len(option_names)]
b = chess.Board()
selected_square = None
while not b.is_game_over() and not its_draw(b):
if b.turn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
square = screen_to_board(x, y)
if selected_square is None:
# Select a piece
if b.piece_at(square):
selected_square = square
else:
# Move a piece
if b.piece_at(selected_square).piece_type == 1 and chess.square_rank(square) in [0, 7]:
promotion_piece = get_promotion_choice(square)
move = chess.Move(selected_square, square, promotion=promotion_piece)
b.push(move)
selected_square = None
else:
move = chess.Move(selected_square, square)
if move in b.legal_moves:
b.push(move)
selected_square = None
else:
try:
with chess.polyglot.open_reader('performance.bin') as reader:
b.push(list(reader.find_all(b))[0].move)
except:
scores = get_legal_moves_scores(b, 2)
b.push(scores[1][scores[0].index(min(scores[0]))])
show(b.fen(), b)