-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
477 lines (390 loc) · 15 KB
/
main.py
File metadata and controls
477 lines (390 loc) · 15 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
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
469
470
471
472
473
474
475
476
477
import math
import sys
from typing import Optional
import chess
import pygame
# ----------------------------
# Configuration
# ----------------------------
LARGEUR = 720
LONGUEUR = 760
TAILLE_TABLEAU = 720
TAILLE_SQ = TAILLE_TABLEAU // 8
FPS = 60
CARRE_BLANC = (240, 217, 181)
CARRE_NOIR = (181, 136, 99)
SOULIGNER = (246, 246, 105)
MOOVE_CONSEILLE = (106, 166, 74)
TEXT_COULEUR = (25, 25, 25)
FOND_COULEUR = (235, 235, 235)
CHECK_COULEUR = (220, 80, 80)
NIVEAU_BOT = 3 # 2 = plus rapide, 3 = plus fort
# Unicode des pièces
PIECES = {
"P": "♙",
"N": "♘",
"B": "♗",
"R": "♖",
"Q": "♕",
"K": "♔",
"p": "♟",
"n": "♞",
"b": "♝",
"r": "♜",
"q": "♛",
"k": "♚",}
# Valeurs matérielles
VALEUR_PIECES = {
chess.PAWN: 1,
chess.KNIGHT: 3,
chess.BISHOP: 3,
chess.ROOK: 5,
chess.QUEEN: 9,
chess.KING: 0,}
# Tables positionnelles simples
PAWN_TABLE = [
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5, -10, 0, 0, -10, -5, 5,
5, 10, 10, -20, -20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0,]
KNIGHT_TABLE = [
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 5, 5, 0, -20, -40,
-30, 5, 10, 15, 15, 10, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 10, 15, 15, 10, 0, -30,
-40, -20, 0, 0, 0, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50,]
BISHOP_TABLE = [
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20,]
ROOK_TABLE = [
0, 0, 5, 10, 10, 5, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0,]
QUEEN_TABLE = [
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 5, 0, 0, 0, 0, -10,
-10, 5, 5, 5, 5, 5, 0, -10,
0, 0, 5, 5, 5, 5, 0, -5,
-5, 0, 5, 5, 5, 5, 0, -5,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20,]
KING_TABLE_MID = [
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-20, -30, -30, -40, -40, -30, -30, -20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20,]
def index_miroir(index: int) -> int:
# Miroir vertical pour les pièces noires.
rang = index // 8
file_ = index % 8
return (7 - rang) * 8 + file_
def piece_carre_valeur(piece: chess.Piece, carre: chess.Square) -> int:
idx = carre
if piece.color == chess.BLACK:
idx = index_miroir(carre)
if piece.piece_type == chess.PAWN:
return PAWN_TABLE[idx]
if piece.piece_type == chess.KNIGHT:
return KNIGHT_TABLE[idx]
if piece.piece_type == chess.BISHOP:
return BISHOP_TABLE[idx]
if piece.piece_type == chess.ROOK:
return ROOK_TABLE[idx]
if piece.piece_type == chess.QUEEN:
return QUEEN_TABLE[idx]
if piece.piece_type == chess.KING:
return KING_TABLE_MID[idx]
return 0
def evaluation_tableau(tab: chess.Board) -> int:
# Évaluation depuis le point de vue des noirs. Score positif = avantage noir.
if tab.is_checkmate():
if tab.turn == chess.WHITE:
# Blanc doit jouer mais est mat -> noir a gagné
return 100000
else:
# Noir doit jouer mais est mat -> blanc a gagné
return -100000
if (tab.is_stalemate() or tab.is_insufficient_material() or tab.can_claim_threefold_repetition() or tab.can_claim_fifty_moves()):
return 0
score = 0
for carre, piece in tab.piece_map().items():
carre = VALEUR_PIECES[piece.piece_type] + piece_carre_valeur(piece, carre)
if piece.color == chess.BLACK:
score += carre
else:
score -= carre
# Petite prime à la mobilité
mobilite = len(list(tab.legal_moves))
if tab.turn == chess.BLACK:
score += mobilite * 2
else:
score -= mobilite * 2
return score
def ordre_mouvement(tab: chess.Board) -> list[chess.Move]:
# Trie simple : captures et promotions d'abord.
def moove_score(move: chess.Move) -> int:
score = 0
if tab.is_capture(move):
victime = tab.piece_at(move.to_square)
attaquant = tab.piece_at(move.from_square)
if victime and attaquant:
score += 10 * VALEUR_PIECES[victime.piece_type] - VALEUR_PIECES[attaquant.piece_type]
else:
score += 100
if move.promotion:
score += 800
if tab.gives_check(move):
score += 50
return score
moves = list(tab.legal_moves)
moves.sort(key=moove_score, reverse=True)
return moves
def minimax(board: chess.Board, depth: int, alpha: int, beta: int, maximizing: bool) -> int:
if depth == 0 or board.is_game_over():
return evaluation_tableau(board)
moves = ordre_mouvement(board)
if maximizing:
max_eval = -math.inf
for move in moves:
board.push(move)
eval_ = minimax(board, depth - 1, alpha, beta, False)
board.pop()
max_eval = max(max_eval, eval_)
alpha = max(alpha, eval_)
if beta <= alpha:
break
return int(max_eval)
else:
min_eval = math.inf
for move in moves:
board.push(move)
eval_ = minimax(board, depth - 1, alpha, beta, True)
board.pop()
min_eval = min(min_eval, eval_)
beta = min(beta, eval_)
if beta <= alpha:
break
return int(min_eval)
def otenir_meilleur_coup(board: chess.Board, depth: int) -> Optional[chess.Move]:
# Le bot joue les noirs.
if board.is_game_over():
return None
best_move = None
best_eval = -math.inf
for move in ordre_mouvement(board):
board.push(move)
eval_ = minimax(board, depth - 1, -math.inf, math.inf, False)
board.pop()
if eval_ > best_eval:
best_eval = eval_
best_move = move
return best_move
def square_to_screen(square: chess.Square) -> tuple[int, int]:
file_ = chess.square_file(square)
rank = chess.square_rank(square)
x = file_ * TAILLE_SQ
y = (7 - rank) * TAILLE_SQ
return x, y
def screen_to_square(pos: tuple[int, int]) -> Optional[chess.Square]:
x, y = pos
if x < 0 or x >= TAILLE_TABLEAU or y < 0 or y >= TAILLE_TABLEAU:
return None
file_ = x // TAILLE_SQ
rank = 7 - (y // TAILLE_SQ)
return chess.square(file_, rank)
def interface(
affichage: pygame.Surface,
tableau: chess.Board,
piece_font: pygame.font.Font,
ui_font: pygame.font.Font,
selection_carre: Optional[chess.Square],
coup_legal: list[chess.Square],
status_text: str,
) -> None:
affichage.fill(FOND_COULEUR)
# Cases
for rank in range(8):
for file_ in range(8):
x = file_ * TAILLE_SQ
y = rank * TAILLE_SQ
color = CARRE_BLANC if (rank + file_) % 2 == 0 else CARRE_NOIR
pygame.draw.rect(affichage, color, (x, y, TAILLE_SQ, TAILLE_SQ))
# Surligner roi en échec
if tableau.is_check():
king_square = tableau.king(tableau.turn)
if king_square is not None:
x, y = square_to_screen(king_square)
pygame.draw.rect(affichage, CHECK_COULEUR, (x, y, TAILLE_SQ, TAILLE_SQ))
# Case sélectionnée
if selection_carre is not None:
x, y = square_to_screen(selection_carre)
pygame.draw.rect(affichage, SOULIGNER, (x, y, TAILLE_SQ, TAILLE_SQ))
# Cibles légales
for sq in coup_legal:
x, y = square_to_screen(sq)
center = (x + TAILLE_SQ // 2, y + TAILLE_SQ // 2)
pygame.draw.circle(affichage, MOOVE_CONSEILLE, center, 12)
# Pièces
for square, piece in tableau.piece_map().items():
x, y = square_to_screen(square)
symbol = PIECES[piece.symbol()]
texte = piece_font.render(symbol, True, TEXT_COULEUR)
text_rect = texte.get_rect(center=(x + TAILLE_SQ // 2, y + TAILLE_SQ // 2 + 4))
affichage.blit(texte, text_rect)
# Coordonnées
coordonnee_police = pygame.font.SysFont("Arial", 18)
for file_ in range(8):
lettre = chr(ord("a") + file_)
texte = coordonnee_police.render(lettre, True, (60, 60, 60))
affichage.blit(texte, (file_ * TAILLE_SQ + 4, TAILLE_TABLEAU - 22))
for rank in range(8):
nombre = str(8 - rank)
texte = coordonnee_police.render(nombre, True, (60, 60, 60))
affichage.blit(texte, (4, rank * TAILLE_SQ + 4))
# Bandeau UI
pygame.draw.rect(affichage, (245, 245, 245), (0, TAILLE_TABLEAU, LARGEUR, LONGUEUR - TAILLE_TABLEAU))
info = ui_font.render(status_text, True, (30, 30, 30))
affichage.blit(info, (16, TAILLE_TABLEAU + 16))
aide_texte = "R: restart | Z: undo last full move | Bot: noirs"
aide_surface = ui_font.render(aide_texte, True, (80, 80, 80))
affichage.blit(aide_surface, (16, TAILLE_TABLEAU + 48))
pygame.display.flip()
def obtenir_coup_legal(board: chess.Board, from_square: chess.Square) -> list[chess.Square]:
return [move.to_square for move in board.legal_moves if move.from_square == from_square]
def trouver(board: chess.Board, from_square: chess.Square, to_square: chess.Square, promotion_piece: int = chess.QUEEN,) -> Optional[chess.Move]:
# Trouve un coup légal correspondant au déplacement. Gère la promotion en dame par défaut.
candidate = chess.Move(from_square, to_square)
if candidate in board.legal_moves:
return candidate
# Promotion
promo_candidate = chess.Move(from_square, to_square, promotion=promotion_piece)
if promo_candidate in board.legal_moves:
return promo_candidate
return None
def main() -> None:
pygame.init()
pygame.display.set_caption("Python Chess AI - Joue contre l'assistant de AmZzPYJS")
affichage = pygame.display.set_mode((LARGEUR, LONGUEUR))
temps = pygame.time.Clock()
piece_police = pygame.font.SysFont("DejaVu Sans", 64)
ui_police = pygame.font.SysFont("Arial", 24)
tab = chess.Board()
case_selectionne: Optional[chess.Square] = None
coup_legal: list[chess.Square] = []
jouer = True
bot_reflechi = False
texte_statut = "À toi de jouer !"
while jouer:
temps.tick(FPS)
# Tour du bot
if tab.turn == chess.BLACK and not tab.is_game_over() and not bot_reflechi:
bot_reflechi = True
texte_statut = "L'assistant de AmZzPYJS réfléchit..."
interface(affichage, tab, piece_police, ui_police, case_selectionne, coup_legal, texte_statut)
meilleur_coup = otenir_meilleur_coup(tab, NIVEAU_BOT)
if meilleur_coup is not None:
tab.push(meilleur_coup)
bot_reflechi = False
if tab.is_checkmate():
texte_statut = "Échec et mat. L'assistant de AmZzPYJS a gagné."
elif tab.is_stalemate():
texte_statut = "Pat."
elif tab.is_check():
texte_statut = "Échec contre les blancs."
else:
texte_statut = "À toi de jouer !"
for event in pygame.event.get():
if event.type == pygame.QUIT:
jouer = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
tab = chess.Board()
case_selectionne = None
coup_legal = []
texte_statut = "Nouvelle partie. À toi de jouer !"
elif event.key == pygame.K_z:
# Annule le dernier coup blanc + noir si possible
if len(tab.move_stack) >= 2:
tab.pop()
tab.pop()
case_selectionne = None
coup_legal = []
texte_statut = "Dernier tour annulé."
elif len(tab.move_stack) == 1:
tab.pop()
case_selectionne = None
coup_legal = []
texte_statut = "Dernier coup annulé."
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if tab.turn != chess.WHITE or tab.is_game_over():
continue
square = screen_to_square(event.pos)
if square is None:
continue
piece = tab.piece_at(square)
# Aucune pièce sélectionnée : on en choisit une blanche
if case_selectionne is None:
if piece is not None and piece.color == chess.WHITE:
case_selectionne = square
coup_legal = obtenir_coup_legal(tab, square)
else:
# Clique sur une autre pièce blanche : on change la sélection
if piece is not None and piece.color == chess.WHITE:
case_selectionne = square
coup_legal = obtenir_coup_legal(tab, square)
else:
move = trouver(tab, case_selectionne, square)
if move is not None:
tab.push(move)
case_selectionne = None
coup_legal = []
if tab.is_checkmate():
texte_statut = "Échec et mat. Tu as gagné."
elif tab.is_stalemate():
texte_statut = "Pat."
elif tab.is_check():
texte_statut = "Échec contre les noirs."
else:
texte_statut = "Coup joué. Le bot va répondre."
else:
# Désélection si coup illégal
case_selectionne = None
coup_legal = []
# Fin de partie
if tab.is_game_over():
outcome = tab.outcome()
if outcome is not None:
if outcome.winner is True:
texte_statut = "Partie terminée : les blancs gagnent."
elif outcome.winner is False:
texte_statut = "Partie terminée : les noirs gagnent."
else:
texte_statut = "Partie terminée : nulle."
interface(affichage, tab, piece_police, ui_police, case_selectionne, coup_legal, texte_statut)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()