-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
523 lines (426 loc) · 17.3 KB
/
GUI.py
File metadata and controls
523 lines (426 loc) · 17.3 KB
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
import os
import io
import random
import sys
import threading
"""
TODO
1. Fix Promotion. Check.
2. Add window sound. Check.
3. Add animations. No need.
4. Multiple piece styles. No need.
5. Add info bar + flip button + resign, restart, draw
6. end screen
7. Add smarter, yet deterministic agent.
8. Go full RL.
"""
import pygame
from Board import Board
from Piece import Piece, PieceType, Team
from Location import Location
from Agent import RandomAgent, StockfishAgent, LeelaAgent
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
os.chdir(sys._MEIPASS)
# Set up the drawing window
BOARD_SIZE = 704
MENU_BAR_SIZE = (BOARD_SIZE // 16)
MENU_SIZE = (BOARD_SIZE // 3)
is_menu_open = False
SQUARE_SIZE = (BOARD_SIZE // 8)
WHITE = (233, 211, 176)
BLACK = (180, 136, 99)
GRAY = (61, 58, 55)
LIGHT_GRAY = (159,156,153,102)
GREEN = (45, 138, 44, 150)
PURPLE = (164, 164, 205)
YELLOW = (247,235,118,150)
rows = ['a','b','c','d','e','f','g','h']
SPRITES = {}
TEXTS = {}
BUTTON_OFFSETS = {}
BUTTONS = {}
SOUNDS = {}
selectedLoc = None
pieceStyle = 'frugale'
autoQueen = False
animating = [] #hold tuples of (piece, startLoc, endLoc)
set_orientation = 'random'
orientation = None
starting_orientation = None
players = ['leela','stockfish'] # [top, bottom]
agent_delay = (0.5,2) # around 1 second for AI to respond
audio = True
stockfish_params = {
'depth' : 18,
'elo' : 1000,
'thinking_time' : 400,
}
leela_params = {
'depth' : 18,
#'elo' : 2000,
'thinking_time' : 3000,
}
def main():
global BOARD_SIZE
global SQUARE_SIZE
global SOUNDS
# initialize window
pygame.init()
pygame.display.set_caption('Chess() by Nihal')
pygame.display.set_icon(pygame.image.load(os.path.join('resources','icon.png')))
# choose board orientation
reset_orientation()
# create screen and clock
screen = screen_init() # creates screen and also sprites
clock = pygame.time.Clock()
# create board object
board = Board()
# create agent arrays
load_agents(board, players)
# import sounds
SOUNDS['move'] = pygame.mixer.Sound(os.path.join('resources','sounds', 'move-self.wav'))
SOUNDS['take'] = pygame.mixer.Sound(os.path.join('resources','sounds', 'capture.wav'))
SOUNDS['castle'] = pygame.mixer.Sound(os.path.join('resources','sounds', 'castle.wav'))
SOUNDS['check'] = pygame.mixer.Sound(os.path.join('resources','sounds', 'move-check.wav'))
SOUNDS['game_over'] = pygame.mixer.Sound(os.path.join('resources','sounds', 'game-end.wav'))
# Run until the user asks to quit
running = True
visualDelta = True
move_return = None
since_move = 0
delay = 0
move = None
move_task = None
while running:
#if not animating:
dt = clock.tick(60)
# Did the user click the window close button?
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#elif event.type == pygame.FULLSCREEN:
#BOARD_SIZE = min(event.size[0], event.size[1])
#SQUARE_SIZE = (BOARD_SIZE // 8)
#screen = screen_init('fullscreen')
#visualDelta = True
elif event.type == pygame.VIDEORESIZE:
BOARD_SIZE = min(event.size[0], event.size[1])
SQUARE_SIZE = (BOARD_SIZE // 8)
screen = screen_init()
visualDelta = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
move_return = click(event, board, screen, move_return)
visualDelta = bool(move_return)
if visualDelta:
render(screen, board, move_return)
visualDelta = False
# do agent
if getMovingAgent(board) in agents.keys():
if not board.gameOver:
since_move += dt
if not delay:
delay = random.uniform(agent_delay[0], agent_delay[1]) * 1000
if not move_task:
move = []
move_task = threading.Thread(target=get_agent_move, args=[board,move,], daemon=True)
move_task.start()
else:
if not move_task.is_alive():
if since_move > delay:
if move:
move_return = board.move(move[0])
visualDelta = True
since_move = 0
delay = 0
move = None
move_task = None
else:
pass
#still waiting, pass
# Done! Time to quit.
pygame.quit()
def get_agent_move(board, move):
global agents
# do agent
if getMovingAgent(board) in agents.keys():
if not board.gameOver:
move.append(agents[getMovingAgent(board)].getMove())
def render(screen, board, move_return):
# Fill the background with white
screen.fill(GRAY)
# draw squares
for i in range(8):
for j in range(8):
pygame.draw.rect(screen, BLACK if (i + j) % 2 == 1 else WHITE,
(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
# draw notations
for i, rank in enumerate(['a','b','c','d','e','f','g','h']):
screen.blit(TEXTS[rank], (((i+1)if orientation is Team.WHITE else (8-i))*SQUARE_SIZE-round(SQUARE_SIZE/7), 7*SQUARE_SIZE+round(SQUARE_SIZE*11/15)))
for i in range(1,9):
screen.blit(TEXTS[str(i)], (round(SQUARE_SIZE/15), (8-i if orientation is Team.WHITE else i-1) * SQUARE_SIZE))
# draw past move highlight
if board.moveLog:
fromLoc, toLoc, _ = board.convertSAN(board.moveLog[-1])
screen.blit(SPRITES['pH'], translateLoc(fromLoc))
screen.blit(SPRITES['pH'], translateLoc(toLoc))
# draw selected highlight
if selectedLoc:
screen.blit(SPRITES['sH'], translateLoc(selectedLoc))
if board.getPiece(selectedLoc):
if board.getPiece(selectedLoc).color is board.getActiveTeam():
for _, toLoc, _ in board.getPiece(selectedLoc).getLegalMoves(mode='loc'):
if board.getPiece(toLoc):
screen.blit(SPRITES['tH'], translateLoc(toLoc))
else:
screen.blit(SPRITES['cH'], translateLoc(toLoc))
# draw pieces
for piece in board.getPieces('both'):
screen.blit(SPRITES[('w' if piece.color == Team.WHITE else 'b') + piece.type.toString()], translateLoc(piece))
# draw promoting menu
if move_return == 'need_promote':
draw_promote(screen, board)
# draw menu
draw_menu(screen, board)
#play sound
if audio:
if move_return in ['moved', 'promoted']:
pygame.mixer.Sound.play(SOUNDS['move'])
elif move_return == 'captured':
pygame.mixer.Sound.play(SOUNDS['take'])
elif move_return == 'castled':
pygame.mixer.Sound.play(SOUNDS['castle'])
elif move_return == 'checked':
pygame.mixer.Sound.play(SOUNDS['check'])
elif move_return in ['1-0', '0-1', '0.5-0.5']:
pygame.mixer.Sound.play(SOUNDS['game_over'])
# Flip the display
pygame.display.flip()
def draw_menu(screen, board):
if is_menu_open:
pass
else:
for key in BUTTON_OFFSETS:
screen.blit(TEXTS[key], BUTTON_OFFSETS[key])
def draw_over(screen, board):
pass
def sprite_gen():
global SPRITES
SPRITES = {}
for s in ['wK', 'wQ', 'wR', 'wB', 'wN', 'wP', 'bK', 'bQ', 'bR', 'bB', 'bN', 'bP']:
path = os.path.join('resources','piece_sprites',pieceStyle, f'{s}.')
if os.path.exists(path+'svg'):
SPRITES[s] = svg_load((path+'svg'), SQUARE_SIZE).convert_alpha()
elif os.path.exists(path+'png'):
SPRITES[s] = pygame.image.load(path+'png').convert_alpha()
SPRITES[s] = pygame.transform.smoothscale(SPRITES[s], (SQUARE_SIZE, SQUARE_SIZE))
else:
raise FileNotFoundError("Sprites not found.")
# highlights
# selectedSquareHighlight
SPRITES['sH'] = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
SPRITES['sH'].fill(GREEN)
SPRITES['sH'].convert_alpha()
# possibleMovesCircleHighlight
SPRITES['cH'] = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
pygame.draw.circle(SPRITES['cH'], GREEN, (SQUARE_SIZE/2, SQUARE_SIZE/2), SQUARE_SIZE/6)
SPRITES['cH'].convert_alpha()
# possibleTakesCircleHighlight
SPRITES['tH'] = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
SPRITES['tH'].fill(GREEN)
pygame.draw.circle(SPRITES['tH'], (0, 0, 0, 0), (SQUARE_SIZE/2, SQUARE_SIZE/2), SQUARE_SIZE/1.8)
#pygame.draw.circle(SPRITES['tH'], GREEN, (SQUARE_SIZE/2, SQUARE_SIZE/2), SQUARE_SIZE/2)
#pygame.draw.circle(SPRITES['tH'], (0, 0, 0, 0), (SQUARE_SIZE/2, SQUARE_SIZE/2), SQUARE_SIZE/2.7)
SPRITES['tH'].convert_alpha()
# pastMoveHighlight
SPRITES['pH'] = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
SPRITES['pH'].fill(YELLOW)
SPRITES['pH'].convert_alpha()
def font_gen():
global TEXTS
global BUTTON_OFFSETS
global BUTTONS
# import font
font = pygame.font.Font(os.path.join('resources','fonts', 'Segoe UI.ttf'), SQUARE_SIZE//5)
icons = pygame.font.Font(os.path.join('resources','fonts', 'chessglyph.ttf'), round(SQUARE_SIZE/1.7))
for i, file in enumerate(rows):
TEXTS[file] = font.render(file, True, WHITE if i%2 == orientation.value else BLACK)
for i in range(1,9):
TEXTS[str(i)] = font.render(str(i), True, BLACK if i%2==orientation.value else WHITE)
TEXTS['settings'] = icons.render('·', True, LIGHT_GRAY)
TEXTS['flip'] = icons.render('f', True, LIGHT_GRAY)
TEXTS['resign'] = icons.render('Y', True, LIGHT_GRAY)
TEXTS['reset'] = icons.render('L', True, LIGHT_GRAY)
TEXTS['right_arrow'] = icons.render('…', True, LIGHT_GRAY)
TEXTS['draw'] = pygame.transform.smoothscale(icons.render('+', True, LIGHT_GRAY),TEXTS['right_arrow'].get_size())
# recompute button offsets
BUTTON_OFFSETS = {
'settings': (BOARD_SIZE + round(SQUARE_SIZE / 13), -round(SQUARE_SIZE / 10)),
'flip': (BOARD_SIZE + round(SQUARE_SIZE / 14), round(SQUARE_SIZE / 2.5)),
'right_arrow': (BOARD_SIZE + round(SQUARE_SIZE / 10), round(SQUARE_SIZE * 3.575)),
'draw': (BOARD_SIZE + round(SQUARE_SIZE / 13), round(SQUARE_SIZE * 6.25)),
'resign': (BOARD_SIZE + round(SQUARE_SIZE / 14), round(SQUARE_SIZE * 6.8)),
'reset': (BOARD_SIZE + round(SQUARE_SIZE / 14), round(SQUARE_SIZE * 7.3)),
}
if not os.name == 'nt':
for key, item in BUTTON_OFFSETS.items():
BUTTON_OFFSETS[key] = (item[0],item[1]+round(SQUARE_SIZE / 10))
# store rect of button locations
for key in BUTTON_OFFSETS:
BUTTONS[key] = TEXTS[key].get_bounding_rect().move(BUTTON_OFFSETS[key])
def svg_load(filename, scale):
svg_string = open(filename, "rt").read()
start = svg_string.find('<svg')
if start > 0:
if os.name == 'nt':
svg_string = svg_string[:start + 4] + f' transform="scale({scale/50})"' + svg_string[start + 4:]
windexStart = svg_string.find('width="', start)
windexEnd = svg_string.find('"', windexStart+len('width="'))
if windexStart > 0 and windexEnd > 0:
svg_string = svg_string[:windexStart+len('width="')] + f'{scale}px' + svg_string[windexEnd:]
hindexStart = svg_string.find('height="', start)
hindexEnd = svg_string.find('"', hindexStart+len('height="'))
if hindexStart > 0 and hindexEnd > 0:
svg_string = svg_string[:hindexStart+len('height="')] + f'{scale}px' + svg_string[hindexEnd:]
return pygame.image.load(io.BytesIO(svg_string.encode()))
def screen_init(s=None):
global BOARD_SIZE
global SQUARE_SIZE
global MENU_BAR_SIZE
global MENU_SIZE
# discrete board size to avoid white borders
if BOARD_SIZE % 8 != 0:
BOARD_SIZE -= BOARD_SIZE % 8
SQUARE_SIZE = (BOARD_SIZE // 8)
MENU_BAR_SIZE = (BOARD_SIZE // 16)
MENU_SIZE = (BOARD_SIZE // 3)
#init screen
#if s == 'fullscreen':
# screen = pygame.display.set_mode([BOARD_SIZE, BOARD_SIZE], pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE | pygame.FULLSCREEN)
#else:
screen = pygame.display.set_mode([BOARD_SIZE+(MENU_SIZE if is_menu_open else MENU_BAR_SIZE), BOARD_SIZE], pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
# load piece sprites
sprite_gen()
# font gen
font_gen()
return screen
def click(event, board, screen, move_return):
if event.pos[0] < BOARD_SIZE:
return board_click(event, board, screen, move_return)
else:
return menu_click(event, board, screen, move_return)
def board_click(event, board, screen, move_return):
global selectedLoc
if orientation is Team.WHITE:
mouseLoc = Location(event.pos[0]//SQUARE_SIZE+1, 8-event.pos[1]//SQUARE_SIZE)
else:
mouseLoc = Location(8-event.pos[0]//SQUARE_SIZE, event.pos[1]//SQUARE_SIZE+1)
if getMovingAgent(board) == 'manual':
if move_return == 'need_promote':
x = (board.getPromotingPawn(board.getActiveTeam()).x)
if mouseLoc.x == x and mouseLoc.y in [1,8]:
modifier = 'Q'
elif mouseLoc.x == x and mouseLoc.y in [2,7]:
modifier = 'R'
elif mouseLoc.x == x and mouseLoc.y in [3,6]:
modifier = 'B'
elif mouseLoc.x == x and mouseLoc.y in [4,5]:
modifier = 'N'
else:
modifier = ''
selectedLoc = None
return board.move(board.getPromotingPawn(board.getActiveTeam()).loc, board.getPromotingPawn(board.getActiveTeam()).loc, modifier=modifier)
if selectedLoc:
if getMovingAgent(board) == 'manual':
return_value = board.move(selectedLoc, mouseLoc, modifier=('Q' if autoQueen else None))
selectedLoc = None
return return_value
else:
selectedLoc = None
if board.getPiece(mouseLoc):
selectedLoc = mouseLoc
return 'highlighted'
else:
selectedLoc = None
return 'unselected'
return True
def menu_click(event, board, screen, move_return):
if BUTTONS['flip'].collidepoint(event.pos):
flipBoard()
return True
if BUTTONS['reset'].collidepoint(event.pos):
reset(board)
return True
if BUTTONS['resign'].collidepoint(event.pos):
if getMovingAgent(board) == 'manual':
board.move('resign')
return True
if BUTTONS['draw'].collidepoint(event.pos):
if getMovingAgent(board) == 'manual':
board.move('draw')
return True
if BUTTONS['settings'].collidepoint(event.pos):
reset(board)
return True
if BUTTONS['right_arrow'].collidepoint(event.pos) and board.gameOver:
pass
def draw_promote(screen, board):
mask = pygame.Surface((BOARD_SIZE, BOARD_SIZE), pygame.SRCALPHA)
mask.fill((0,0,0,150))
screen.blit(mask, (0,0))
x = (board.getPromotingPawn(board.getActiveTeam()).x-1) if orientation is Team.WHITE else (8-board.getPromotingPawn(board.getActiveTeam()).x)
y = (0 if board.getActiveTeam() is orientation else 4)
for i in range(4):
pygame.draw.rect(screen, PURPLE,
(x * SQUARE_SIZE, (y+i) * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE), border_radius=SQUARE_SIZE//4)
for i, piece in enumerate(['Q','R','B','N']):
screen.blit(SPRITES[('w' if board.getActiveTeam() is Team.WHITE else 'b') + piece],
(x * SQUARE_SIZE, (i if board.getActiveTeam() is orientation else 7-i) * SQUARE_SIZE))
def translateLoc(loc, y=None):
if y:
if orientation is Team.WHITE:
return ((loc - 1) * SQUARE_SIZE, (8 - y) * SQUARE_SIZE)
else:
return ((8-loc) * SQUARE_SIZE, (y - 1) * SQUARE_SIZE)
if orientation is Team.WHITE:
return ((loc.x - 1) * SQUARE_SIZE, (8 - loc.y) * SQUARE_SIZE)
else:
return ((8-loc.x) * SQUARE_SIZE, (loc.y-1) * SQUARE_SIZE)
def getMovingAgent(board):
return players[(board.getActiveTeam().value + starting_orientation.value+1) % 2]
def flipBoard():
global orientation
orientation = orientation.opponent()
font_gen()
def reset_orientation():
global orientation
global starting_orientation
if set_orientation == 'random':
starting_orientation = Team(random.randint(0,1))
orientation = starting_orientation
elif set_orientation.lower() == 'white':
starting_orientation = Team(0)
orientation = Team(0)
elif set_orientation.lower() == 'black':
starting_orientation = Team(1)
orientation = Team(1)
def reset(board):
board.reset()
reset_orientation()
font_gen()
def load_agents(board, players):
global agents
agents = {}
if 'random' in players:
agents['random'] = RandomAgent(board)
if 'stockfish' in players:
agents['stockfish'] = StockfishAgent(board, **stockfish_params)
if 'leela' in players:
agents['leela'] = LeelaAgent(board, **leela_params)
if 'maia' in players:
pass
if 'centurion' in players:
pass
return agents
if __name__ == "__main__":
main()