-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
337 lines (288 loc) · 13.7 KB
/
gui.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
import ctypes
import random
import time
from tkinter import *
import chess
import pygame
import guppy
import guppy as engine
class App:
def __init__(self, show_info, play_as=None):
pygame.font.init()
self.show_info = show_info
self.play_as = play_as
# CONSTANTS
self.FONT_SIZE = 12
self.FONT = pygame.font.Font(pygame.font.get_default_font(), self.FONT_SIZE)
self.WHITE = (238, 238, 210)
self.BLACK = (118, 150, 86)
self.TILE_SIZE = 60
self.TEXT_MARGIN = 1
# SCREENS
self.screen = pygame.display.set_mode((8 * self.TILE_SIZE, 8 * self.TILE_SIZE))
self.chess_grid_surface = pygame.Surface((8 * self.TILE_SIZE, 8 * self.TILE_SIZE))
# CHESS VARIABLES
self.board = None
self.done = False
self.player_side = None
self.tile_on_focus = None
self.pieces = []
self.hist = []
self.time_start = 0
self.promotion_piece = None
def run(self):
pygame.init()
pygame.display.set_caption('GUI')
program_icon = pygame.image.load("img/black_p.png")
pygame.display.set_icon(program_icon)
self.board = chess.Board()
self.initialize_player_side()
self.initialize_chess_grid_surface()
self.set_piece_data()
self.blit_pieces()
self.refresh_chess_grid_surface()
self.info("Main Loop Started")
while not self.done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
elif event.type == pygame.MOUSEBUTTONDOWN and self.board.turn == self.player_side:
self.tile_on_focus = self.get_tile_under_mouse(event.pos)
elif event.type == pygame.MOUSEBUTTONUP and self.board.turn == self.player_side:
self.make_move(f"{self.tile_on_focus}{self.get_tile_under_mouse(event.pos)}", 'string')
self.tile_on_focus = None
if self.board.turn != self.player_side:
self.think()
if self.board.outcome() == None:
self.refresh_chess_grid_surface()
else:
self.blit_pieces()
self.refresh_chess_grid_surface()
self.display_outcome_message()
pygame.display.flip()
def initialize_player_side(self):
self.player_side = random.choice([chess.WHITE, chess.BLACK])
if self.play_as != None:
self.player_side = self.play_as
self.info("Player Side Initialized: White" if self.player_side else "Player Side Initialized: Black")
def initialize_chess_grid_surface(self):
text_margin = 1
self.chess_grid_surface.fill(self.WHITE)
for x in range(1, 9, 2):
for y in range(0, 8, 2):
pygame.draw.rect(self.chess_grid_surface, self.BLACK,
(x * self.TILE_SIZE, y * self.TILE_SIZE, self.TILE_SIZE, self.TILE_SIZE))
for x in range(0, 8, 2):
for y in range(1, 9, 2):
pygame.draw.rect(self.chess_grid_surface, self.BLACK,
(x * self.TILE_SIZE, y * self.TILE_SIZE, self.TILE_SIZE, self.TILE_SIZE))
if self.player_side == chess.WHITE:
for x, file in enumerate("abcdefgh"):
if x % 2 == 0:
temp_text = self.FONT.render(file, True, self.WHITE)
else:
temp_text = self.FONT.render(file, True, self.BLACK)
self.chess_grid_surface.blit(temp_text,
((x + 1) * self.TILE_SIZE - self.FONT_SIZE - self.TEXT_MARGIN,
8 * self.TILE_SIZE - self.FONT_SIZE - self.TEXT_MARGIN))
for y in [8, 7, 6, 5, 4, 3, 2, 1]:
if y % 2 == 1:
temp_text = self.FONT.render(str(y), True, self.WHITE)
else:
temp_text = self.FONT.render(str(y), True, self.BLACK)
self.chess_grid_surface.blit(temp_text, (self.TEXT_MARGIN, (8 - y) * self.TILE_SIZE + self.TEXT_MARGIN))
elif self.player_side == chess.BLACK:
for x, file in enumerate("hgfedcba"):
if x % 2 == 0:
temp_text = self.FONT.render(file, True, self.WHITE)
else:
temp_text = self.FONT.render(file, True, self.BLACK)
self.chess_grid_surface.blit(temp_text,
((x + 1) * self.TILE_SIZE - self.FONT_SIZE - self.TEXT_MARGIN,
8 * self.TILE_SIZE - self.FONT_SIZE - self.TEXT_MARGIN))
for y in range(0, 8):
if y % 2 == 1:
temp_text = self.FONT.render(str(y + 1), True, self.WHITE)
else:
temp_text = self.FONT.render(str(y + 1), True, self.BLACK)
self.chess_grid_surface.blit(temp_text, (self.TEXT_MARGIN, y * self.TILE_SIZE + self.TEXT_MARGIN))
self.info("Chess Grid Initialized")
def set_piece_data(self):
self.pieces = []
fen = self.board.fen()
temp = fen.split(" ")
position = temp.pop(0)
for y, row in enumerate(position.split("/")):
rank = 8 - y
new_position_string = ""
for char in row:
try:
new_position_string += "." * int(char)
except ValueError:
new_position_string += char
for x, type in enumerate(new_position_string):
file = 'abcdefgh'[x]
if type != ".":
if type.isupper():
type = f"white_{type}"
else:
type = f"black_{type}"
pos = f"{file}{rank}"
self.pieces.append(self.Piece(self, type, pos))
def refresh_chess_grid_surface(self):
self.screen.blit(self.chess_grid_surface, self.chess_grid_surface.get_rect())
self.blit_pieces()
class Piece:
def __init__(self, parent, type, filerank):
self.parent = parent
self.type = type
self.colour = self.get_colour()
self.filerank = filerank
self.file = filerank[0]
self.rank = int(filerank[1])
self.image = pygame.image.load(f"img/{self.type}.png")
self.xy = self.get_xy()
def get_xy(self):
files = "abcdefgh"
if self.parent.player_side == chess.BLACK:
return ((7 - files.index(self.file)) * self.parent.TILE_SIZE, (self.rank - 1) * self.parent.TILE_SIZE)
elif self.parent.player_side == chess.WHITE:
return (files.index(self.file) * self.parent.TILE_SIZE, (8 - self.rank) * self.parent.TILE_SIZE)
def get_colour(self):
if str(self.type).isupper():
return chess.WHITE
else:
return chess.BLACK
def make_engine_move(self):
self.time_start = time.time()
depth = 2
qsearch = True
if not self.player_side == chess.WHITE:
move_val = engine.generate_move(self.board, depth, True, qsearch)
elif not self.player_side == chess.BLACK:
move_val = engine.generate_move(self.board, depth, False, qsearch)
if self.board.outcome() == None:
# random_move = random.choice([move for move in self.board.legal_moves])
# self.make_move(random_move, "move")
self.make_move(move_val[0], "move")
# searcher = engine.Searcher()
# start = time.time()
# for _depth, move, score in searcher.search(hist[-1], hist):
# if time.time() - start > 1:
# break
# make_move(move, "move")
# hist.append(hist[-1].move(move))
def blit_pieces(self):
for piece in self.pieces:
if self.tile_on_focus == piece.filerank:
mouse_pos = pygame.mouse.get_pos()
center = (mouse_pos[0] - self.TILE_SIZE / 2, mouse_pos[1] - self.TILE_SIZE / 2)
self.screen.blit(piece.image, center)
else:
self.screen.blit(piece.image, piece.xy)
def get_tile_under_mouse(self, pos):
x = pos[0] // self.TILE_SIZE
y = pos[1] // self.TILE_SIZE
if self.player_side == chess.WHITE:
file_order = "abcdefgh"
rank_order = "87654321"
elif self.player_side == chess.BLACK:
file_order = "hgfedcba"
rank_order = "12345678"
return f"{file_order[x]}{rank_order[y]}"
def make_move(self, s, format):
try:
if format == "move":
move = s
elif format == "string":
move = chess.Move.from_uci(s)
if self.player_side == chess.WHITE:
if chess.square_rank(move.to_square) == 7 and self.board.piece_type_at(move.from_square) == 1:
self.promote(chess.WHITE)
move.promotion = self.promotion_piece
if self.player_side == chess.BLACK:
if chess.square_rank(move.to_square) == 0 and self.board.piece_type_at(
move.from_square) == chess.PAWN:
self.promote(chess.BLACK)
move.promotion = self.promotion_piece
if move in self.board.legal_moves:
if (self.board.turn == chess.WHITE) and (self.player_side == chess.WHITE):
self.info(f"\n{move}: {guppy.move_value(self.board, move)}")
elif (self.board.turn == chess.WHITE) and (self.player_side == chess.BLACK):
self.info(
f"\n{move}: {guppy.move_value(self.board, move)} (elapsed: {time.time() - self.time_start:.2f}s)")
elif (self.board.turn == chess.BLACK) and (self.player_side == chess.WHITE):
self.info(
f"{move}: {guppy.move_value(self.board, move)} (elapsed: {time.time() - self.time_start:.2f}s)")
elif (self.board.turn == chess.BLACK) and (self.player_side == chess.BLACK):
self.info(f"{move}: {guppy.move_value(self.board, move)}")
else:
print(self.board.turn, self.player_side)
self.board.push(move)
self.set_piece_data()
self.hist.append(self.board.fen())
except ValueError:
pass
def promote(self, c):
global promotion_window
small_font = ("helvetica", 20)
promotion_window = Tk()
promotion_window.title("Select")
promotion_window.geometry("207x55")
promotion_window.resizable(False, False)
promotion_piece = chess.QUEEN
if c == chess.WHITE:
Button(promotion_window, text="♘", font=small_font,
command=lambda p=chess.KNIGHT, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=1)
Button(promotion_window, text="♗", font=small_font,
command=lambda p=chess.BISHOP, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=2)
Button(promotion_window, text="♖", font=small_font,
command=lambda p=chess.ROOK, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=3)
Button(promotion_window, text="♕", font=small_font,
command=lambda p=chess.QUEEN, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=4)
elif c == chess.BLACK:
Button(promotion_window, text="♞", font=small_font,
command=lambda p=chess.KNIGHT, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=1)
Button(promotion_window, text="♝", font=small_font,
command=lambda p=chess.BISHOP, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=2)
Button(promotion_window, text="♜", font=small_font,
command=lambda p=chess.ROOK, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=3)
Button(promotion_window, text="♛", font=small_font,
command=lambda p=chess.QUEEN, root=promotion_window: self.promote_to(p, root)).grid(row=0, column=4)
promotion_window.mainloop()
def promote_to(self, p, root):
self.promotion_piece = p
root.destroy()
def display_outcome_message(self):
outcome = self.board.outcome()
termination = str(outcome.termination)[12:].capitalize()
prompt = None
title = None
if outcome.winner == None:
prompt = f"Draw by {termination}\n Rematch?"
title = "Draw!"
elif outcome.winner:
prompt = f"White wins by {termination}\n Rematch?"
title = "White wins!"
elif not outcome.winner:
prompt = f"Black wins by {termination}\n Rematch?"
title = "Black wins!"
user_choice = ctypes.windll.user32.MessageBoxW(0, prompt, title, 4)
if user_choice == 6: # ID YES
self.board = chess.Board()
self.tile_on_focus = None
self.pieces = []
self.hist = []
self.promotion_piece = None
self.initialize_player_side()
self.set_piece_data()
self.blit_pieces()
self.refresh_chess_grid_surface()
elif user_choice == 7: # ID NO
quit()
def info(self, s):
if self.show_info:
print(s)
if __name__ == "__main__":
app = App(show_info=True, play_as=chess.BLACK)
app.run()