This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
238 lines (201 loc) · 8.04 KB
/
game.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
import pygame
import copy
from player import Player
from board import Board
from human import Human
from new_ai import Ai
from button import Button
from help import Help
from pause import Pause
from play import Play
from clock import Clock
# couleur des pièces
N = "n"
B = "b"
pygame.font.init()
font1 = pygame.font.SysFont("Comic Sans MS", 20) # police pour l'écriture
font2 = pygame.font.SysFont("Sans-Serif", 15)
font3 = pygame.font.SysFont("Comic Sans MS", 10)
font4 = pygame.font.SysFont("JetBrains Mono Regular", 20)
class Game:
def __init__(
self, screen: pygame.Surface, data: dict, settings: dict, display: dict
):
"""
une classe de jeu d'échecs
Parameters
----------
screen : pygame.Surface
la surface de jeu
data : dict
le dictionnaire des couleurs
settings : dict
les réglages de l'ia
display : dict
les réglages des graphiques
"""
self.pause = False # pour mettre le jeu en pause
self.new_start = 0.0 # le temps pour redémarer les chronos après la pause
self.running = True # le jeu tourne
self.clock = Clock(-1, 0) # pour avoir la durée de la partie
self.screen = screen
self.data = data
self.settings = settings
self.display = display
self.board = Board()
self.playerB = Human(B, self) # joueur avec les pièces blanches
self.playerN = Ai(N, self) # joueur avec les pièces noires
self.cur_player = self.playerB
self.score = self.board.get_score(self.board.board)
self.all_scores = [self.score]
self.all_moves = [] # str
self.suggested = [(-11, -11), (-11, -11)]
self.all_buttons = pygame.sprite.Group()
self.all_buttons.add(Help(self))
self.all_buttons.add(Pause(self))
def next_player(self):
"""
le joueur suivant
"""
# plus aucun coup n'est suggéré
self.suggested = [(-11, -11), (-11, -11)]
# actualisation du score avant l'actualisation de l'état d'échec
self.score = self.board.get_score(self.board.board)
# recherche d'une meilleure fonction d'évaluation
if self.playerB.name in ("aib", "ain") or self.playerN.name in ("aib", "ain"):
ev = (self.playerB, self.playerN)[self.playerN.name in ("aib", "ain")]
try:
ev.engine.set_position(self.all_moves)
self.score = ev.engine.get_evaluation()["value"] / 256
except:
pass
self.all_scores.append(self.score)
# actualisation de tous les coups
self.all_moves.append(self.get_move() + self.board.last_move_addon)
self.board.last_move_addon = ""
# actualisation de l'état d'échec
self.board.get_check(self.playerB, self.playerN)
self.cur_player = (self.playerB, self.playerN)[self.cur_player == self.playerB]
self.cur_player.is_mate(self) # test du mat
if self.cur_player.checkmate:
self.running = False # le joueur ne peut plus jouer
def get_move(self) -> str:
move = self.board.last_move
indexes = ["a", "b", "c", "d", "e", "f", "g", "h"]
y1 = str(8 - move[0][0])
y2 = str(8 - move[1][0])
x1 = indexes[move[0][1]]
x2 = indexes[move[1][1]]
return x1 + y1 + x2 + y2
def display_score(self):
"""
affichage du graphique du score
"""
all_scores = self.all_scores
n = len(all_scores)
if n == 1:
all_scores.append(all_scores[0])
n += 1
screen = self.screen
# le coefficient de zoom
zoom = self.display["zoom_coef"]
# espacement
space = 250 // n
# parcours des scores
for i in range(n - 1):
y1 = 300 - all_scores[0] * zoom
y2 = 300 - all_scores[1] * zoom
if y1 * y2 < 0: # on passe par 0
m = 1 / (y2 - y1)
b = m * (-i * space)
# résolution de mx+b=0
x = -b / m
self.draw_polygon([(525 + i * space, y1), (525 + x, 300)])
self.draw_polygon([(525 + x, 300), (525 + (i + 1) * space, y2)])
else: # on ne passe pas par 0
self.draw_polygon([(525 + i * space, y1), (525 + (i + 1) * space, y2)])
def draw_polygon(self, l: list({tuple({int})})):
# les points
x1 = l[0][0]
y1 = l[0][1]
x2 = l[1][0]
y2 = l[1][1]
l0 = l + [(x2, 300), (x1, 300)]
# toutes les couleurs
c1 = self.data["score_graph_if_positiv_color"]
c2 = self.data["score_graph_if_positiv_color_faded"]
c3 = self.data["score_graph_if_negativ_color"]
c4 = self.data["score_graph_if_negativ_color_faded"]
c = (c1, c3)[y1 <= 0 and y2 <= 0]
cf = (c2, c4)[y2 <= 0 and y2 <= 0]
pygame.draw.polygon(self.screen, cf, l0)
pygame.draw.line(self.screen, c, l[0], l[1])
def display_state(self):
"""
affichage de l'état de la partie
"""
screen = self.screen
data = self.data
add = 150
for i in (1, 2):
textsurface = font1.render(f"joueur {i}", True, data["player_text_color"])
screen.blit(textsurface, (510 + add * (i - 1), 1))
player = (self.playerB, self.playerN)[i == 2]
name = ("HUMAIN", "I.A.")[player.name in ("aib", "ain")]
textsurface = font2.render(name, True, data["player_text_color_faded"])
screen.blit(textsurface, (520 + add * (i - 1), 25))
if self.cur_player.name in ("humanb", "aib"): # tour du joueur b
pygame.draw.lines(
screen,
data["active_player_indicator_color"],
True,
[(501, 11), (511, 15), (501, 19)],
)
if self.cur_player.name in ("humann", "ain"): # tour du joueur n
pygame.draw.lines(
screen,
data["active_player_indicator_color"],
True,
[(651, 11), (661, 15), (651, 19)],
)
# afficher le score
score = round(self.score, 2)
color = (data["score_if_positiv_color"], data["score_if_negativ_color"])[
score < 0
]
score += 0.0 # on évite l'affichage de -0.0
textsurface = font3.render(str(score), True, color)
screen.blit(textsurface, (570, 20))
if not self.running: # afficher l'échec-et-mat
if self.playerB.checkmate:
textsurface = font2.render(
"checkmate!", True, data["checkmate_indicator_color"]
)
screen.blit(textsurface, (520, 35))
if self.playerN.checkmate:
textsurface = font2.render(
"checkmate!", True, data["checkmate_indicator_color"]
)
screen.blit(textsurface, (670, 35))
else:
# afficher l'état d'échec des rois
if self.playerB.check == True:
textsurface = font2.render("check!", True, data["check_indictor_color"])
screen.blit(textsurface, (520, 35))
if self.playerN.check == True:
textsurface = font2.render("check!", True, data["check_indictor_color"])
screen.blit(textsurface, (670, 35))
# afficher la durée de la partie
h, m, s = self.clock.get_time()
textsurface = font4.render(
str(h) + ":" + str(m) + ":" + str(s), True, data["game_duration_color"]
)
screen.blit(textsurface, (500, 480))
def switch_button(self):
for button in self.all_buttons:
if button.action == 3: # bouton pause
self.all_buttons.remove(button)
self.all_buttons.add(Play(self))
if button.action == 4: # bouton play
self.all_buttons.remove(button)
self.all_buttons.add(Pause(self))