-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
301 lines (256 loc) · 10.4 KB
/
board.py
File metadata and controls
301 lines (256 loc) · 10.4 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
# Interface Gráfica do Tabuleiro de Xadrez (GUI)
# Permite escolher entre os seguintes modos: player (brancas) vs bot e bot vs bot
import chess
import chess.engine
import pygame
import time
from strategies import RandomMove, Pyrarucu
# Definições de dimensões, cores e fonte
pygame.init()
pygame.display.set_caption("Chess Board")
width, height = 640, 640
squareSize = width // 8
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
FONT = pygame.font.Font(None, 36)
PLAYER_BOT = 'p-bot'
BOT_BOT = 'bot-bot'
SYMBOLS = ['K', 'Q', 'R', 'B', 'N', 'P', 'k', 'q', 'r', 'b', 'n', 'p']
whitePromotionOptions = ['Q', 'R', 'B', 'N']
blackPromotionOptions = ['q', 'r', 'b', 'n']
class ChessGUI:
def __init__(self, screen: pygame.display):
self.board = chess.Board()
self.screen = screen
self.selected_square = None # Quadrado inicial da peça selecionada
self.image_dict = {
symbol: pygame.image.load(f"images/{symbol}.png") for symbol in SYMBOLS
}
self.mode_selection_screen()
mode = None
while True:
if mode != None:
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if 200 <= x <= 440 and 100 <= y <= 150:
mode = PLAYER_BOT
elif 200 <= x <= 440 and 160 <= y <= 210:
mode = BOT_BOT
if mode == PLAYER_BOT:
self.playerVSbot()
elif mode == BOT_BOT:
self.botVSbot()
def draw_board(self) -> None:
colors = [WHITE, GRAY]
for row in range(8):
for col in range(8):
color = colors[(row + col) % 2]
# Desenha um retângulo/tabuleiro
pygame.draw.rect(
self.screen,
color,
pygame.Rect(
col * squareSize,
row * squareSize,
squareSize,
squareSize
)
)
def draw_pieces(self, board: chess.Board) -> None:
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece:
piece_image = self.image_dict[piece.symbol()]
row = 7 - (square // 8)
col = square % 8
# Sobrepõe a imagem da peça sobre o tabuleiro
self.screen.blit(
piece_image,
pygame.Rect(
col * (squareSize) + 8,
row * (squareSize) + 10,
squareSize,
squareSize
)
)
def mode_selection_screen(self) -> None:
pygame.draw.rect(self.screen, GRAY, (200, 100, 240, 50))
pygame.draw.rect(self.screen, GRAY, (200, 160, 240, 50))
text_botPlayer = FONT.render("Player vs Bot", True, WHITE)
text_doubleBot = FONT.render("Bot vs Bot", True, WHITE)
self.screen.blit(text_botPlayer, (237, 110))
self.screen.blit(text_doubleBot, (255, 170))
# Atualiza a tela
pygame.display.flip()
def promotion_event(self) -> None:
pygame.draw.rect(
screen,
(255, 255, 60),
pygame.Rect(
width // 4,
height // 4,
width // 2,
height // 8
)
)
if self.board.turn == chess.WHITE:
final = whitePromotionOptions
else:
final = blackPromotionOptions
# Insere na tela as peças de promoção corretas
for i, piece in enumerate(final):
piece_image = self.image_dict[final[i]]
screen.blit(
piece_image,
pygame.Rect(
(width // 4) + i * (squareSize + 5),
height // 4,
squareSize,
squareSize
)
)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
# Verificar qual peça foi clicada
if height // 4 < y < (height // 4) + squareSize:
for i, piece in enumerate(final):
if (width // 4) + i * (squareSize + 5) < x < (width // 4) + (i + 1) * (squareSize + 5):
return piece
# Define o controle das ações e movimentos ao clicar com o mouse
def handle_mouse_click(self, pos: tuple):
row = pos[1] // squareSize
col = pos[0] // squareSize
clicked_square = chess.square(col, 7 - row)
# Nenhuma peça foi selecionada
if self.selected_square is None:
if self.board.piece_at(clicked_square) and self.board.piece_at(clicked_square).color == (self.board.turn):
# Seleciona a peça
self.selected_square = clicked_square
pygame.draw.rect(
self.screen,
pygame.Color(255, 255, 40),
pygame.Rect(
col * squareSize,
row * squareSize,
squareSize,
squareSize
)
)
# A mesma casa foi selecionada
elif self.selected_square == clicked_square:
self.draw_board()
self.selected_square = None
# Uma peça foi selecionada
else:
move = chess.Move(self.selected_square, clicked_square)
# Verifica se é um caso de promoção
if self.board.piece_at(self.selected_square).piece_type == chess.PAWN:
if (
(self.board.turn == chess.WHITE and chess.square_rank(clicked_square) == 7) or
(self.board.turn == chess.BLACK and chess.square_rank(clicked_square) == 0)
):
promotionPiece = promotion_event()
if (promotionPiece == 'q') or (promotionPiece == 'Q'):
move = chess.Move(self.selected_square, clicked_square, promotion=chess.QUEEN)
elif (promotionPiece == 'r') or (promotionPiece == 'R'):
move = chess.Move(self.selected_square, clicked_square, promotion=chess.ROOK)
elif (promotionPiece == 'b') or (promotionPiece == 'B'):
move = chess.Move(self.selected_square, clicked_square, promotion=chess.BISHOP)
elif (promotionPiece == 'n') or (promotionPiece == 'N'):
move = chess.Move(self.selected_square, clicked_square, promotion=chess.KNIGHT)
if move in self.board.legal_moves:
self.board.push(move)
self.draw_board()
self.selected_square = None
self.draw_pieces(self.board)
def verify_check(self) -> None:
if self.board.is_check():
# Obter a posição do rei que está em xeque
if self.board.turn:
king_square = self.board.king(chess.WHITE)
else:
king_square = self.board.king(chess.BLACK)
# Destaca a posição do rei com uma cor vermelha
row = 7 - (king_square // 8)
col = king_square % 8
pygame.draw.rect(
self.screen,
pygame.Color(255, 0, 0),
pygame.Rect(
col * squareSize,
row * squareSize,
squareSize,
squareSize
)
)
self.draw_pieces(self.board)
def has_ended(self) -> bool:
if self.board.is_checkmate():
if self.board.turn == chess.WHITE:
print("Black Wins!")
else:
print("White Wins!")
return True
elif (
self.board.is_insufficient_material() or
self.board.is_stalemate() or
self.board.can_claim_threefold_repetition() or
self.board.can_claim_fifty_moves()
):
print("Draw!")
return True
return False
def playerVSbot(self, engine = Pyrarucu()) -> None:
running = True
self.draw_board()
self.draw_pieces(self.board)
while running:
# Verificar se algum rei está em xeque
self.verify_check()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif self.board.turn == chess.WHITE and event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
self.handle_mouse_click(pos)
elif self.board.turn == chess.BLACK:
result = engine.search(self.board)
self.board.push(result.move)
self.draw_board()
self.draw_pieces(self.board)
# Verificar xeque-mate ou empate
running = not self.has_ended()
def botVSbot(self, engine1 = Pyrarucu(), engine2 = Pyrarucu()) -> None:
running = True
self.draw_board()
self.draw_pieces(self.board)
while running:
# Verificar se algum rei está em xeque
self.verify_check()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif self.board.turn == chess.WHITE:
result = engine1.search(self.board)
self.board.push(result.move)
time.sleep(0.5)
elif self.board.turn == chess.BLACK:
result = engine2.search(self.board)
self.board.push(result.move)
time.sleep(0.5)
self.draw_board()
self.draw_pieces(self.board)
# Verificar xeque-mate ou empate
running = not self.has_ended()