This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
277 lines (223 loc) · 11.3 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
import pygame, chess
from random import choice
from traceback import format_exc
from sys import stderr
from time import strftime
from copy import deepcopy
pygame.init()
SQUARE_SIDE = 50
AI_SEARCH_DEPTH = 2
RED_CHECK = (240, 150, 150)
WHITE = (255, 255, 255)
BLUE_LIGHT = (140, 184, 219)
BLUE_DARK = (91, 131, 159)
GRAY_LIGHT = (240, 240, 240)
GRAY_DARK = (200, 200, 200)
CHESSWEBSITE_LIGHT = (212, 202, 190)
CHESSWEBSITE_DARK = (100, 92, 89)
LICHESS_LIGHT = (240, 217, 181)
LICHESS_DARK = (181, 136, 99)
LICHESS_GRAY_LIGHT = (164, 164, 164)
LICHESS_GRAY_DARK = (136, 136, 136)
BOARD_COLORS = [(GRAY_LIGHT, GRAY_DARK),
(BLUE_LIGHT, BLUE_DARK),
(WHITE, BLUE_LIGHT),
(CHESSWEBSITE_LIGHT, CHESSWEBSITE_DARK),
(LICHESS_LIGHT, LICHESS_DARK),
(LICHESS_GRAY_LIGHT, LICHESS_GRAY_DARK)]
BOARD_COLOR = choice(BOARD_COLORS)
BLACK_KING = pygame.image.load('images/black_king.png')
BLACK_QUEEN = pygame.image.load('images/black_queen.png')
BLACK_ROOK = pygame.image.load('images/black_rook.png')
BLACK_BISHOP = pygame.image.load('images/black_bishop.png')
BLACK_KNIGHT = pygame.image.load('images/black_knight.png')
BLACK_PAWN = pygame.image.load('images/black_pawn.png')
BLACK_JOKER = pygame.image.load('images/black_joker.png')
WHITE_KING = pygame.image.load('images/white_king.png')
WHITE_QUEEN = pygame.image.load('images/white_queen.png')
WHITE_ROOK = pygame.image.load('images/white_rook.png')
WHITE_BISHOP = pygame.image.load('images/white_bishop.png')
WHITE_KNIGHT = pygame.image.load('images/white_knight.png')
WHITE_PAWN = pygame.image.load('images/white_pawn.png')
WHITE_JOKER = pygame.image.load('images/white_joker.png')
CLOCK = pygame.time.Clock()
CLOCK_TICK = 15
SCREEN = pygame.display.set_mode((8 * SQUARE_SIDE, 8 * SQUARE_SIDE), pygame.RESIZABLE)
SCREEN_TITLE = 'Chess Game'
pygame.display.set_icon(pygame.image.load('images/chess_icon.ico'))
pygame.display.set_caption(SCREEN_TITLE)
def resize_screen(square_side_len):
global SQUARE_SIDE
global SCREEN
SCREEN = pygame.display.set_mode((8 * square_side_len, 8 * square_side_len), pygame.RESIZABLE)
SQUARE_SIDE = square_side_len
def print_empty_board():
SCREEN.fill(BOARD_COLOR[0])
paint_dark_squares(BOARD_COLOR[1])
def paint_square(square, square_color):
col = chess.FILES.index(square[0])
row = 7 - chess.RANKS.index(square[1])
pygame.draw.rect(SCREEN, square_color, (SQUARE_SIDE * col, SQUARE_SIDE * row, SQUARE_SIDE, SQUARE_SIDE), 0)
def paint_dark_squares(square_color):
for position in chess.single_gen(chess.DARK_SQUARES):
paint_square(chess.bb2str(position), square_color)
def get_square_rect(square):
col = chess.FILES.index(square[0])
row = 7 - chess.RANKS.index(square[1])
return pygame.Rect((col * SQUARE_SIDE, row * SQUARE_SIDE), (SQUARE_SIDE, SQUARE_SIDE))
def coord2str(position, color=chess.WHITE):
if color == chess.WHITE:
file_index = int(position[0] / SQUARE_SIDE)
rank_index = 7 - int(position[1] / SQUARE_SIDE)
return chess.FILES[file_index] + chess.RANKS[rank_index]
if color == chess.BLACK:
file_index = 7 - int(position[0] / SQUARE_SIDE)
rank_index = int(position[1] / SQUARE_SIDE)
return chess.FILES[file_index] + chess.RANKS[rank_index]
def print_board(board, color=chess.WHITE):
if color == chess.WHITE:
printed_board = board
if color == chess.BLACK:
printed_board = chess.rotate_board(board)
print_empty_board()
if chess.is_check(board, chess.WHITE):
paint_square(chess.bb2str(chess.get_king(printed_board, chess.WHITE)), RED_CHECK)
if chess.is_check(board, chess.BLACK):
paint_square(chess.bb2str(chess.get_king(printed_board, chess.BLACK)), RED_CHECK)
for position in chess.colored_piece_gen(printed_board, chess.KING, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_KING, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.QUEEN, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_QUEEN, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.ROOK, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_ROOK, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.BISHOP, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_BISHOP, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.KNIGHT, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_KNIGHT, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.PAWN, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_PAWN, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.JOKER, chess.BLACK):
SCREEN.blit(pygame.transform.scale(BLACK_JOKER, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.KING, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_KING, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.QUEEN, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_QUEEN, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.ROOK, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_ROOK, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.BISHOP, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_BISHOP, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.KNIGHT, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_KNIGHT, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.PAWN, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_PAWN, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
for position in chess.colored_piece_gen(printed_board, chess.JOKER, chess.WHITE):
SCREEN.blit(pygame.transform.scale(WHITE_JOKER, (SQUARE_SIDE, SQUARE_SIDE)),
get_square_rect(chess.bb2str(position)))
pygame.display.flip()
def set_title(title):
pygame.display.set_caption(title)
pygame.display.flip()
def make_AI_move(game, color):
set_title(SCREEN_TITLE + ' - Calculating move...')
new_game = chess.make_move(game, chess.get_AI_move(game, AI_SEARCH_DEPTH))
set_title(SCREEN_TITLE)
print_board(new_game.board, color)
return new_game
def try_move(game, attempted_move):
for move in chess.legal_moves(game, game.to_move):
if move == attempted_move:
game = chess.make_move(game, move)
return game
def play_as(game, color):
run = True
ongoing = True
joker = 0
try:
while run:
CLOCK.tick(CLOCK_TICK)
print_board(game.board, color)
if chess.game_ended(game):
set_title(SCREEN_TITLE + ' - ' + chess.get_outcome(game))
ongoing = False
if ongoing and game.to_move == chess.opposing_color(color):
game = make_AI_move(game, color)
if chess.game_ended(game):
set_title(SCREEN_TITLE + ' - ' + chess.get_outcome(game))
ongoing = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
leaving_square = coord2str(event.pos, color)
if event.type == pygame.MOUSEBUTTONUP:
arriving_square = coord2str(event.pos, color)
if ongoing and game.to_move == color:
move = (chess.str2bb(leaving_square), chess.str2bb(arriving_square))
game = try_move(game, move)
print_board(game.board, color)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == 113:
run = False
if event.key == 104 and ongoing: # H key
game = make_AI_move(game, color)
if event.key == 117: # U key
game = chess.unmake_move(game)
game = chess.unmake_move(game)
set_title(SCREEN_TITLE)
print_board(game.board, color)
ongoing = True
if event.key == 99: # C key
global BOARD_COLOR
new_colors = deepcopy(BOARD_COLORS)
new_colors.remove(BOARD_COLOR)
BOARD_COLOR = choice(new_colors)
print_board(game.board, color)
if event.key == 112 or event.key == 100: # P or D key
print(game.get_move_list() + '\n')
print('\n'.join(game.position_history))
if event.key == 101: # E key
print('eval = ' + str(chess.evaluate_game(game) / 100))
if event.key == 106: # J key
joker += 1
if joker == 13 and chess.get_queen(game.board, color):
queen_index = chess.bb2index(chess.get_queen(game.board, color))
game.board[queen_index] = color | chess.JOKER
print_board(game.board, color)
if event.type == pygame.VIDEORESIZE:
if SCREEN.get_height() != event.h:
resize_screen(int(event.h / 8.0))
elif SCREEN.get_width() != event.w:
resize_screen(int(event.w / 8.0))
print_board(game.board, color)
except:
print(format_exc(), file=stderr)
bug_file = open('bug_report.txt', 'a')
bug_file.write('----- ' + strftime('%x %X') + ' -----\n')
bug_file.write(format_exc())
bug_file.write('\nPlaying as WHITE:\n\t' if color == chess.WHITE else '\nPlaying as BLACK:\n\t')
bug_file.write(game.get_move_list() + '\n\t')
bug_file.write('\n\t'.join(game.position_history))
bug_file.write('\n-----------------------------\n\n')
bug_file.close()
def play_as_white(game=chess.Game()):
return play_as(game, chess.WHITE)
def play_as_black(game=chess.Game()):
return play_as(game, chess.BLACK)
def play_random_color(game=chess.Game()):
color = choice([chess.WHITE, chess.BLACK])
play_as(game, color)
# chess.verbose = True
play_random_color()