-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessMain.py
253 lines (216 loc) · 9.22 KB
/
ChessMain.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
"""
Main driver file responsible for handling user input and displaying the current GameState object
"""
import pygame as p
from Chess import ChessEngine, SmartMoveFinder
from multiprocessing import Process, Queue
BOARD_WIDTH = BOARD_HEIGHT = 512
MOVE_LOG_PANEL_WIDTH = 200
MOVE_LOG_PANEL_HEIGHT = BOARD_HEIGHT
DIMENSION = 8
SQ_SIZE = BOARD_HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}
returnQueue = Queue()
"""
Global dictionary of images to be called once in main.py
"""
def load_images():
pieces = ["wp", "wB", "wN", "wR", "wQ", "wK", "bp", "bB", "bN", "bR", "bQ", "bK"]
for piece in pieces:
IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), size=(SQ_SIZE, SQ_SIZE))
"""
User input and updating graphics
"""
def main():
global returnQueue
p.init()
screen = p.display.set_mode((BOARD_WIDTH + MOVE_LOG_PANEL_WIDTH, BOARD_HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
moveLogFont = p.font.SysFont("Arial", 14, True, False)
gs = ChessEngine.GameState()
valid_moves = gs.get_valid_moves()
move_made = False
animate = False
load_images()
running = True
sq_selected = ()
player_clicks = []
game_over = False
playerOne = True
playerTwo = False
AIThinking = False
moveFinderProcess = None
moveUndone = False
while running:
humanTurn = (gs.WhiteToMove and playerOne) or (not gs.WhiteToMove and playerTwo)
for e in p.event.get():
if e.type == p.QUIT:
running = False
elif e.type == p.MOUSEBUTTONDOWN:
if not game_over:
location = p.mouse.get_pos()
col = location[0] // SQ_SIZE
row = location[1] // SQ_SIZE
if sq_selected == (row, col) or col >= 8:
sq_selected = ()
player_clicks = []
else:
sq_selected = (row, col)
player_clicks.append(sq_selected)
if len(player_clicks) == 2 and humanTurn:
move = ChessEngine.Move(player_clicks[0], player_clicks[1], gs.board)
print(move.get_chess_notation())
for valid_move in valid_moves:
if move == valid_move:
gs.make_move(valid_move)
move_made = True
animate = True
sq_selected = ()
player_clicks = []
if not move_made:
player_clicks = [sq_selected]
elif e.type == p.KEYDOWN:
if e.key == p.K_z:
gs.undo_move()
sq_selected = ()
player_clicks = []
game_over = False
move_made = True
animate = False
if AIThinking:
moveFinderProcess.terminate()
AIThinking = False
moveUndone = True
if e.key == p.K_r:
gs = ChessEngine.GameState()
valid_moves = gs.get_valid_moves()
sq_selected = ()
player_clicks = []
move_made = False
animate = False
game_over = False
if AIThinking:
moveFinderProcess.terminate()
AIThinking = False
moveUndone = True
if not game_over and not humanTurn and not moveUndone:
if not AIThinking:
AIThinking = True
print("Thinking..")
returnQueue = Queue()
moveFinderProcess = Process(target=SmartMoveFinder.findBestMoveMinMax, args=(gs, valid_moves, returnQueue))
moveFinderProcess.start()
if not moveFinderProcess.is_alive():
print("Done thinking")
AIMove = returnQueue.get()
if AIMove is None:
AIMove = SmartMoveFinder.findRandomMove(valid_moves)
gs.make_move(AIMove)
move_made = True
animate = True
AIThinking = False
if move_made:
if animate:
animate_move(gs.moveLog[-1], screen, gs.board, clock)
valid_moves = gs.get_valid_moves()
move_made = False
animate = False
moveUndone = False
draw_game_state(screen, gs, valid_moves, sq_selected, moveLogFont)
if gs.checkMate:
game_over = True
if gs.WhiteToMove:
drawEndGameText(screen, "Black wins by Checkmate.")
else:
drawEndGameText(screen, "White wins by Checkmate.")
elif gs.staleMate:
game_over = True
drawEndGameText(screen, "Game drawn by Stalemate/")
clock.tick(MAX_FPS)
p.display.flip()
def highlight_squares(screen, gs, valid_moves, sq_selected):
if sq_selected != ():
r, c = sq_selected
if gs.board[r][c][0] == ('w' if gs.WhiteToMove else 'b'):
s = p.Surface((SQ_SIZE, SQ_SIZE))
s.set_alpha(100)
s.fill(p.Color('blue'))
screen.blit(s, (c * SQ_SIZE, r * SQ_SIZE))
s.fill(p.Color('yellow'))
for move in valid_moves:
if move.start_row == r and move.start_col == c:
screen.blit(s, (move.end_col * SQ_SIZE, move.end_row * SQ_SIZE))
def draw_game_state(screen, gs, valid_moves, sq_selected, moveLogFont):
draw_board(screen)
draw_pieces(screen, gs.board)
highlight_squares(screen, gs, valid_moves, sq_selected)
drawMoveLog(screen, gs, moveLogFont)
def draw_board(screen):
colors = [p.Color("white"), p.Color("gray")]
for r in range(DIMENSION):
for c in range(DIMENSION):
color = colors[((r + c) % 2)]
p.draw.rect(screen, color, p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))
def draw_pieces(screen, board):
for r in range(DIMENSION):
for c in range(DIMENSION):
piece = board[r][c]
if piece != "--":
screen.blit(IMAGES[piece], p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))
def drawMoveLog(screen, gs, font):
moveLogRect = p.Rect(BOARD_WIDTH, 0, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
p.draw.rect(screen, p.Color("black"), moveLogRect)
moveLog = gs.moveLog
moveTexts = []
for i in range(0, len(moveLog), 2):
moveString = str(i//2 + 1) + ". " + str(moveLog[i]) + " "
if i+1 < len(moveLog):
moveString += str(moveLog[i+1]) + " "
moveTexts.append(moveString)
movesPerRow = 3
padding = 5
textY = padding
lineSpacing = 2
for i in range(0, len(moveTexts), movesPerRow):
text = ""
for j in range(movesPerRow):
if i + j < len(moveTexts):
text += moveTexts[i+j]
textObject = font.render(text, True, p.Color('white'))
textLocation = moveLogRect.move(padding, textY)
screen.blit(textObject, textLocation)
textY += textObject.get_height() + lineSpacing
def animate_move(move, screen, board, clock):
colors = [p.Color("white"), p.Color("gray")]
dR = move.end_row - move.start_row
dC = move.end_col - move.start_col
framesPerSquare = 10
frameCount = (abs(dR) + abs(dC)) * framesPerSquare
for frame in range(frameCount + 1):
r, c = (move.start_row + dR * frame / frameCount, move.start_col + dC * frame / frameCount)
draw_board(screen)
draw_pieces(screen, board)
color = colors[(move.end_row + move.end_col) % 2]
endSquare = p.Rect(move.end_col * SQ_SIZE, move.end_row * SQ_SIZE, SQ_SIZE, SQ_SIZE)
p.draw.rect(screen, color, endSquare)
if move.piece_captured != '--':
if move.EnPassant:
EnPassantRow = move.end_row + 1 if move.piece_captured[0] == 'b' else move.end_row - 1
endSquare = p.Rect(move.end_col * SQ_SIZE, EnPassantRow * SQ_SIZE, SQ_SIZE, SQ_SIZE)
screen.blit(IMAGES[move.piece_captured], endSquare)
if move.piece_moved != '--':
screen.blit(IMAGES[move.piece_moved], p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))
p.display.flip()
clock.tick(60)
def drawEndGameText(screen, text):
font = p.font.SysFont("Helvetica", 32, True, False)
textObject = font.render(text, 0, p.Color('Gray'))
textLocation = p.Rect(0, 0, BOARD_WIDTH, BOARD_HEIGHT).move(BOARD_WIDTH / 2 - textObject.get_width() / 2,
BOARD_HEIGHT / 2 - textObject.get_height() / 2)
screen.blit(textObject, textLocation)
textObject = font.render(text, 0, p.Color('Black'))
screen.blit(textObject, textLocation.move(2, 2))
if __name__ == "__main__":
main()