-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
468 lines (402 loc) · 16.8 KB
/
main.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
"""
Knight Fight is a Chess game written using pygame.
"""
import os
import sys
import traceback
import pygame
import chess
from helpers.conversions import grid_pos_to_move, grid_position_to_label
from ai.lookup import CHESS_SQUARE_TO_POS
from knightfight.board import Board
from knightfight.state import BoardState
from config import config
from knightfight.types import GridPosition, PieceColour, PieceType, TitleChoice
from helpers.log import LOGGER
from sound.playback import play_game_music, play_sound, play_tense_music
from ai.player import AIPlayer
from screens.mainmenu import main_menu
BOARD_BK_COLOUR = (255, 255, 255)
BOARD_BK_COLOUR_BLACK = (0, 0, 0)
# Workaround to get Windows pygame to load audio correctly
# The pygame audio dll does not load correctly, this adds
# the pygame directory to the system path
if os.name == "nt":
# pypy does not find the dlls, so we add package folder to PATH.
pygame_dir = os.path.split(pygame.__file__)[0]
os.environ["PATH"] = os.environ["PATH"] + ";" + pygame_dir
# Fix for the bpo-36085 change in Python3.8 on Windows
os.add_dll_directory(pygame_dir)
class KnightFight:
def __init__(self):
self.dragged_piece = None
self.drag_offset = None
self._tense_mode = False
self.board = None
self.screen = None
@property
def tense_mode(self) -> bool:
return self._tense_mode
@tense_mode.setter
def tense_mode(self, value: bool) -> None:
# only if value has changed
if self._tense_mode != value:
# play tense music if tense mode is enabled
if not value:
play_game_music()
else:
play_tense_music()
self._tense_mode = value
def get_openai_api_key_from_env(self) -> str:
"""
Get the OpenAI API key from the environment variable.
"""
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
LOGGER.warning("OPENAI_API_KEY environment variable is not set.")
return ""
return openai_api_key
def run(self):
# initialize pygame and load config
pygame.init()
config.read_config()
# set up sound volume
music_vol = config.APP_CONFIG["game"]["music_vol"]
sound_vol = config.APP_CONFIG["game"]["sound_vol"]
# set up the window
board_size = config.APP_CONFIG["board"]["size"]
self.screen = pygame.display.set_mode((board_size, board_size))
self.screen.fill(BOARD_BK_COLOUR)
# set window title
pygame.display.set_caption("KNIGHT FIGHT")
pygame.mouse.set_visible(True)
# show splash screen
# self.show_splash_screen(screen)
# players
p1_type = config.APP_CONFIG["game"]["player1"].lower().strip()
p2_type = config.APP_CONFIG["game"]["player2"].lower().strip()
# setup ai player and engines
# max ai players = 2 cpu vs cpu
ai = config.APP_CONFIG["cpu"]["ai"] # use basic / piece_squares ai
complexity = config.APP_CONFIG["cpu"]["complexity"] # ai complexity
engine_path = ""
openai_api_key = ""
if ai == "stockfish":
engine_path = config.APP_CONFIG["cpu"]["stockfish_path"]
if ai == "openai":
openai_api_key = (
config.APP_CONFIG["cpu"]["openai_api_key"]
or self.get_openai_api_key_from_env()
)
ai_white = AIPlayer(
chess.WHITE, sound_vol, ai, complexity, engine_path, openai_api_key
)
ai_black = AIPlayer(
chess.BLACK, sound_vol, ai, complexity, engine_path, openai_api_key
)
AI_PLAYERS = {
PieceColour.White: ai_white,
PieceColour.Black: ai_black,
}
cpu_delay = config.APP_CONFIG["cpu"]["delay"] # delay between moves
# setup board
self.board = Board(self.screen)
# track turn
turn = PieceColour.White
# show main menu
choice = main_menu(config.APP_CONFIG, save_game_func=self.save_game)
self.handle_menu_choice(self.board, choice)
# play game music
play_game_music()
# check if undo is available
undo_last_move_allowed = (
config.APP_CONFIG["game"]["undo_last_move_allowed"] or False
)
try:
# main loop
# check if game is over
game_over_flag = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# get last fen and save to config
self.handle_menu_choice(self.board, TitleChoice.Quit)
elif event.type == pygame.MOUSEBUTTONDOWN:
# check if a piece is clicked
pos = pygame.mouse.get_pos()
try:
clicked_piece = self.board.get_piece_at(pos)[
0
] # only one piece
if clicked_piece and clicked_piece.piece_colour == turn:
# save starting position and start drag-drop event
self.dragged_piece = clicked_piece
self.drag_offset = (
pos[0] - clicked_piece.piece_rect.x,
pos[1] - clicked_piece.piece_rect.y,
)
else:
play_sound("invalid_move.mp3", sound_vol)
except IndexError:
# no piece at position
pass
elif event.type == pygame.MOUSEMOTION:
# update piece position if drag drop is active
if self.dragged_piece:
# update board with possible moves
self.board.populate_move_squares()
# dragged piece to board state to allow rendering
# of piece on top of other pieces
self.board.state.dragged_piece = self.dragged_piece
pos = pygame.mouse.get_pos()
if self.drag_offset:
self.dragged_piece.piece_rect.x = (
pos[0] - self.drag_offset[0]
)
self.dragged_piece.piece_rect.y = (
pos[1] - self.drag_offset[1]
)
elif event.type == pygame.MOUSEBUTTONUP:
# end drag and drop event
if self.dragged_piece:
# clear possible moves
self.board.clear_move_squares()
# update piece position on board
original_pos = self.dragged_piece.grid_pos
pos = pygame.mouse.get_pos()
piece_moved = self.board.move_piece(self.dragged_piece, pos)
moved_pos = self.dragged_piece.grid_pos
self.dragged_piece = None
self.board.state.dragged_piece = None
self.drag_offset = None
turn = self.handle_piece_moved(
self.board,
turn,
original_pos,
moved_pos,
piece_moved,
)
elif event.type == pygame.USEREVENT:
game_over(self.screen, self.board.state)
pygame.time.set_timer(pygame.USEREVENT, 0) # Stop the timer
# handle key presses
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
# show main menu
choice = main_menu(
config.APP_CONFIG,
save_game_func=self.save_game,
)
self.handle_menu_choice(self.board, choice)
elif event.type == pygame.KEYDOWN:
# undo move
if (
event.key == pygame.K_z
and pygame.key.get_mods() & pygame.K_LCTRL
):
if undo_last_move_allowed:
# undo last move user pressed ctrl+z
self.board.undo_last_move()
# change turn
turn = (
PieceColour.White
if self.board.state.engine_state.turn
else PieceColour.Black
)
else:
play_sound("invalid_move.mp3", sound_vol)
# check if game is over
if (
self.board.state.engine_state.is_game_over()
or self.board.state.engine_state.is_stalemate()
or self.board.state.engine_state.is_insufficient_material()
or self.board.state.engine_state.is_seventyfive_moves()
or self.board.state.engine_state.is_fivefold_repetition()
or self.board.state.engine_state.can_claim_fifty_moves()
or self.board.state.game_over == True
):
# pygame timer start to show game over screen
if not game_over_flag:
# show the initial game over screen, then show the
# final game over screen after 10 seconds
pygame.time.set_timer(pygame.USEREVENT, 10000)
game_over(self.screen, self.board.state, True)
# set game over flag
game_over_flag = True
# if either player is cpu, make a move
if (
(p1_type == "cpu" and turn == PieceColour.White)
or (p2_type == "cpu" and turn == PieceColour.Black)
and not game_over_flag
):
self.board.set_status_text("CPU is thinking...", cpu_delay)
self.board.render()
# get random move from list of legal moves
ai_moved = AI_PLAYERS[turn].move(self.board)
original_pos = AI_PLAYERS[turn].original_pos
moved_pos = AI_PLAYERS[turn].new_pos
if ai_moved and original_pos and moved_pos:
# change turn
turn = self.handle_piece_moved(
self.board,
turn,
original_pos,
moved_pos,
ai_moved,
)
# update the display
if not game_over_flag:
self.board.render()
pygame.display.update()
except Exception as exc:
# show stack trace
LOGGER.error(f"Error: {exc} Stack trace: {traceback.format_exc()}")
def save_game(self):
"""
Save the current game state to the config file
"""
# get last fen and save to config
if self.board:
LOGGER.info("Saving game state...")
fen = self.board.state.engine_state.fen()
config.APP_CONFIG["state"]["last_fen"] = str(fen)
config.save_config()
def handle_menu_choice(self, board, choice):
"""
Handle the menu choice
"""
if choice == TitleChoice.Quit:
LOGGER.info("Quitting game")
if self.board:
pygame.quit()
sys.exit()
elif choice == TitleChoice.Load:
# load last saved game
LOGGER.info("Loading last saved game")
board.load_last_game()
else:
# start new game
LOGGER.info("Starting new game")
if self.screen:
self.board = Board(self.screen)
def handle_piece_moved(
self,
board: Board,
turn: PieceColour,
original_pos: GridPosition,
moved_pos: GridPosition,
piece_moved: bool,
):
"""
Handle the piece moved event
"""
sound_vol = config.APP_CONFIG["game"]["sound_vol"]
if piece_moved and original_pos != moved_pos:
frompos = grid_position_to_label(original_pos)
topos = grid_position_to_label(moved_pos)
# go back to normal music, if in tense mode
self.tense_mode = False
# clear any existing highlights
board.clear_highlight_squares()
board_copy = board.state.engine_state.copy()
board_copy.pop() # get rid of last move from copy so we can see if check on last move
move = grid_pos_to_move(original_pos, moved_pos)
# See if move gives check
try:
if board_copy.gives_check(move):
self.handle_check(
board, turn, float(sound_vol), frompos, topos, board_copy
)
except AssertionError:
# move is not legal
LOGGER.info(f"Invalid move {frompos} -> {topos}!")
# See if move gives checkmate
if board.state.engine_state.is_checkmate():
play_sound("check_mate.mp3", sound_vol)
LOGGER.info(f"Checkmate from move {frompos} -> {topos}! GAME OVER!")
board.state.game_over = True
# change turn
turn = (
PieceColour.White
if board.state.engine_state.turn
else PieceColour.Black
)
play_sound("drop.mp3", sound_vol)
else:
play_sound("invalid_move.mp3", sound_vol)
return turn
def handle_check(
self,
board: Board,
turn: PieceColour,
sound_vol: float,
frompos: str,
topos: str,
board_copy: chess.Board,
):
play_sound("check.mp3", sound_vol)
# go into tense mode
self.tense_mode = True
king_square = board_copy.king(
# get king square for opposite colour
True
if turn == PieceColour.Black
else False
)
# highlight square with red background for 5 seconds
if king_square:
board.add_highlight_square(king_square)
LOGGER.info(f"King is in check from move {frompos} -> {topos}")
def game_over(screen: pygame.surface.Surface, state: BoardState, initial: bool = False):
"""
Display game over screen
"""
# set up font
font_name = config.APP_CONFIG["game"]["font_name"]
font = pygame.font.Font(f"assets/fonts/{font_name}", 72)
board_size = config.APP_CONFIG["board"]["size"]
sound_vol = config.APP_CONFIG["game"]["sound_vol"]
winner_piece = None
for piece in state.pieces:
if piece.piece_type == PieceType.King:
winner_piece = piece
break
# set up text
text = font.render("Game Over", True, (255, 255, 255))
text_blk = font.render("Game Over", True, (0, 0, 0))
text_rect = text.get_rect()
text_rect.center = (
board_size // 2,
board_size // 2,
)
# set up font
win_text = "White" if state.winner == PieceColour.White else "Black"
font2 = pygame.font.Font(f"assets/fonts/{font_name}", 48)
text2 = font2.render(f"{win_text} wins!", True, (255, 255, 255))
text_rect2 = text2.get_rect()
text_rect2.center = (
board_size // 2,
board_size // 2 + 80,
)
# show game over screen
if not initial:
screen.fill(BOARD_BK_COLOUR_BLACK)
screen.blit(text, text_rect)
screen.blit(text2, text_rect2)
if winner_piece:
winner_rect = winner_piece.piece_rect
winner_rect.center = (
board_size // 2,
board_size // 2 - 100,
)
screen.blit(winner_piece.piece_image, winner_rect)
else:
screen.blit(text_blk, text_rect)
# play game over sound
play_sound("game_over.mp3", sound_vol)
pygame.display.update()
def start():
chess = KnightFight()
chess.run()
if __name__ == "__main__":
start()