-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchess_main.py
More file actions
325 lines (270 loc) · 10.8 KB
/
chess_main.py
File metadata and controls
325 lines (270 loc) · 10.8 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
"""
@author - thisIsShubhamSharma
Date - 03-01-2021
Main driver file - Displays board and handels user input.
"""
import os
import sys
import pygame as p
import chess_engine
import chess_ai
p.init()
WIDTH = HEIGHT = 512
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}
'''
initialize a global dict of images.
'''
def load_images():
pieces = ["bP", "bR", "bN", "bB", "bQ", "bK", "bB", "bN",
"bR", "wP", "wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]
for piece in pieces:
IMAGES[piece] = p.transform.scale(p.image.load(os.path.join(
'images', piece+'.png')), (SQ_SIZE, SQ_SIZE))
'''
Main function
'''
def main():
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = chess_engine.GameState()
valid_moves = gs.get_valid_moves()
move_made = False # flag for if a new move has been made
animate = False # flag for animation
load_images()
running = True
sq_selected = () # keeps track of last user click. tuple(row,col)
# keeps track of user clicks. list[tuple(row,col), tuple(row,col)]
player_clicks = []
game_over = False
white_did_check = ""
black_did_check = ""
last_move_printed = False
moves_list = []
turn = 1
player_one = True # if true human is playing white
player_two = False # if true human is playing black
while running:
human_turn = (gs.white_to_move and player_one) or (
not gs.white_to_move and player_two)
for e in p.event.get():
if e.type == p.QUIT:
running = False
p.quit()
sys.exit()
elif e.type == p.MOUSEBUTTONDOWN:
if not game_over and human_turn:
location = p.mouse.get_pos() # (x,y) location of mouse
col = location[0]//SQ_SIZE
row = location[1]//SQ_SIZE
if sq_selected == (row, col): # user clicked the same square
sq_selected = () # clear selection
player_clicks = [] # clear the list
else:
sq_selected = (row, col)
# append the clicks to the list
player_clicks.append(sq_selected)
if len(player_clicks) == 2: # 1st and 2nd click
move = chess_engine.Move(
player_clicks[0], player_clicks[1], gs.board)
# prints the move in chess notation
print(move.get_chess_notation())
for i in range(len(valid_moves)):
if move == valid_moves[i]:
gs.make_move(valid_moves[i])
move_made = True
sq_selected = ()
player_clicks = []
if not move_made:
player_clicks = [sq_selected]
elif e.type == p.KEYDOWN:
if e.key == p.K_z: # undo when z is pressed
if gs.white_to_move:
if turn > 1:
turn -= 1
gs.undo_move()
move_made = True
animate = False
game_over = False
last_move_printed = False
if e.key == p.K_r: # reset the game when r is pressed
gs = chess_engine.GameState()
valid_moves = gs.get_valid_moves()
sq_selected = ()
player_clicks = []
move_made = False
animate = False
game_over = False
turn = 1
last_move_printed = False
moves_list = []
# AI move finder
if not game_over and not human_turn:
AI_move = chess_ai.findBestMoveNegaMaxAlphaBeta(gs, valid_moves)
if AI_move is None:
AI_move = chess_ai.findRandomMove(valid_moves)
gs.make_move(AI_move, True)
move_made = True
animate = True
if move_made:
if gs.check_for_pins_and_checks()[0]:
if not gs.white_to_move:
white_did_check = "+"
else:
black_did_check = "+"
if gs.white_to_move:
moves_list.append(
f"\n{turn}. {gs.move_log[-2].get_chess_notation()}{white_did_check} {gs.move_log[-1].get_chess_notation()}{black_did_check}")
print(
f"\n{turn}. {gs.move_log[-2].get_chess_notation()}{white_did_check} {gs.move_log[-1].get_chess_notation()}{black_did_check}", end="")
turn += 1
white_did_check = ""
black_did_check = ""
if animate:
animate_move(gs.move_log[-1], screen, gs.board, clock)
valid_moves = gs.get_valid_moves()
move_made = False
animate = False
draw_game_state(screen, gs, valid_moves, sq_selected)
if gs.checkmate:
game_over = True
if gs.white_to_move:
draw_text(screen, "Black wins by checkmate")
if not last_move_printed:
moves_list[-1] += "+"
moves_list.append("result: 0-1")
print("+")
print("result: 0-1")
last_move_printed = True
save_game(moves_list)
else:
draw_text(screen, "White wins by checkmate")
if not last_move_printed:
moves_list.append(
f"\n{turn}. {gs.move_log[-1].get_chess_notation()}++")
moves_list.append("result: 1-0")
print(
f"\n{turn}. {gs.move_log[-1].get_chess_notation()}++")
print("result: 1-0")
last_move_printed = True
save_game(moves_list)
elif gs.stalemate:
game_over = True
draw_text(screen, "Stalemate")
if not last_move_printed:
if not gs.white_to_move:
moves_list.append(
f"\n{turn}. {gs.move_log[-1].get_chess_notation()}")
moves_list.append("result: 1/2-1/2")
print(f"\n{turn}. {gs.move_log[-1].get_chess_notation()}")
print("result: 1/2-1/2")
last_move_printed = True
save_game(moves_list)
clock.tick(MAX_FPS)
p.display.flip()
def save_game(moves_list):
result = moves_list.pop()
turns_dict = {}
for i in range(len(moves_list)-1, -1, -1):
try:
if int(moves_list[i][1]) not in turns_dict:
turns_dict[moves_list[i][1]] = moves_list[i][1:]+"\n"
except:
pass
file = open("game_logs.txt", "w")
for turn in sorted(turns_dict.keys()):
file.write(turns_dict[turn])
file.write(result)
file.close()
def drawText(screen, text):
font = p.font.SysFont("Bahnschrift", 32, True, False)
text_object = font.render(text, 0, p.Color("gray"))
text_location = p.Rect(0, 0, WIDTH, HEIGHT).move(
WIDTH / 2 - text_object.get_width() / 2, HEIGHT / 2 - text_object.get_height() / 2)
screen.blit(text_object, text_location)
text_object = font.render(text, 0, p.Color('black'))
screen.blit(text_object, text_location.move(2, 2))
def highlight_squares(screen, gs, valid_moves, sq_selected):
'''
Highlight square selected and moves for piece selected.
'''
if (len(gs.move_log)) > 0:
last_move = gs.move_log[-1]
s = p.Surface((SQ_SIZE, SQ_SIZE))
s.set_alpha(100)
s.fill(p.Color('blue'))
screen.blit(s, (last_move.end_col*SQ_SIZE, last_move.end_row*SQ_SIZE))
if sq_selected != ():
row, col = sq_selected
# square_selected is a piece that can be moved
if gs.board[row][col][0] == ('w' if gs.white_to_move else 'b'):
# highlight selected square
s = p.Surface((SQ_SIZE, SQ_SIZE))
# transparency value 0 -> transparent, 255 -> opaque
s.set_alpha(100)
s.fill(p.Color('green'))
screen.blit(s, (col*SQ_SIZE, row*SQ_SIZE))
# highlight moves from that square
s.fill(p.Color('yellow'))
for move in valid_moves:
if move.start_row == row and move.start_col == col:
screen.blit(
s, (move.end_col*SQ_SIZE, move.end_row*SQ_SIZE))
def draw_game_state(screen, gs, valid_moves, sq_selected):
draw_board(screen)
highlight_squares(screen, gs, valid_moves, sq_selected)
draw_pieces(screen, gs.board)
'''
Draws the board.
'''
def draw_board(screen):
global colors
colors = [p.Color("white"), p.Color("gray")]
for row in range(DIMENSION):
for column in range(DIMENSION):
# adds rows and columns gets reminder if reminder is 1 draws a dark block else if reminder is 0 draws white.
color = colors[((row+column) % 2)]
p.draw.rect(screen, color, p.Rect(
column*SQ_SIZE, row*SQ_SIZE, SQ_SIZE, SQ_SIZE))
'''
Draws the pieces on board.
'''
def draw_pieces(screen, board):
for row in range(DIMENSION):
for column in range(DIMENSION):
piece = board[row][column]
if piece != "--":
screen.blit(IMAGES[piece], p.Rect(
column*SQ_SIZE, row*SQ_SIZE, SQ_SIZE, SQ_SIZE))
def animate_move(move, screen, board, clock):
'''
Animating a move
'''
global colors
d_row = move.end_row - move.start_row
d_col = move.end_col - move.start_col
frames_per_square = 10 # frames to move one square
frame_count = (abs(d_row) + abs(d_col)) * frames_per_square
for frame in range(frame_count + 1):
row, col = (move.start_row + d_row * frame / frame_count,
move.start_col + d_col * frame / frame_count)
draw_board(screen)
draw_pieces(screen, board)
# erease the piece moved from its ending square
color = colors[(move.end_row + move.end_col) % 2]
end_square = p.Rect(move.end_col*SQ_SIZE,
move.end_row*SQ_SIZE, SQ_SIZE, SQ_SIZE)
p.draw.rect(screen, color, end_square)
# draw captured piece onto rectangle
if move.piece_captured != '--':
screen.blit(IMAGES[move.piece_captured], end_square)
# draw moving piece
screen.blit(IMAGES[move.piece_moved], p.Rect(
col*SQ_SIZE, row*SQ_SIZE, SQ_SIZE, SQ_SIZE))
p.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()