-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovesGenerator.py
More file actions
214 lines (202 loc) · 11.6 KB
/
MovesGenerator.py
File metadata and controls
214 lines (202 loc) · 11.6 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
def in_borders(pos):
""" Sprawdza czy podana pozycja jest na szachownicy
Zwraca wartość prawda gdy jest, wartość fałsz gdy nie ma"""
if pos[0] < 0 or pos[0] > 7 or pos[1] < 0 or pos[1] > 7:
return False
return True
class MovesGenerator:
def __init__(self, gameplay):
self.gameplay = gameplay
def generate_valid_moves(self):
""" Generuje i zwraca wszystkie ruchy zgodne z zasadami, niekonieczne poprawne w danej sytuacji
(poprawność sprawdza sobie gameplay)"""
valid_moves = []
self.generate_queen_moves(valid_moves)
self.generate_king_moves(valid_moves)
self.generate_bishop_moves(valid_moves)
self.generate_rock_moves(valid_moves)
self.generate_castling(valid_moves)
self.generate_knight_moves(valid_moves)
self.generate_pawn_attacks(valid_moves)
self.generate_pawn_moves(valid_moves)
return valid_moves
def generate_castling(self, valid_moves):
""" Dodaje do podanej listy wszystkie możliwe roszady do wykonania """
if self.gameplay.active_color == "w":
if self.gameplay.white_short_castling:
valid_moves.append(((7, 4), (7, 6)))
if self.gameplay.white_long_castling:
valid_moves.append(((7, 4), (7, 2)))
if self.gameplay.active_color == "b":
if self.gameplay.black_short_castling:
valid_moves.append(((0, 4), (0, 6)))
if self.gameplay.black_long_castling:
valid_moves.append(((0, 4), (0, 2)))
def generate_pawn_attacks(self, valid_moves):
""" Generuje wszystkie ataki pionów i dodaje do listy """
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "P" or color != self.gameplay.active_color:
continue
if color == "w":
if (pos[0] - 1, pos[1] + 1) in self.gameplay.pieces.keys() and \
self.gameplay.pieces.get((pos[0] - 1, pos[1] + 1))[4] == "b":
valid_moves.append(((pos[0], pos[1]), (pos[0] - 1, pos[1] + 1)))
if (pos[0] - 1, pos[1] - 1) in self.gameplay.pieces.keys() \
and self.gameplay.pieces.get((pos[0] - 1, pos[1] - 1))[4] == "b":
valid_moves.append(((pos[0], pos[1]), (pos[0] - 1, pos[1] - 1)))
# bicie w przelocie
last_black_move, figure = self.gameplay.get_last_move()
if last_black_move is None:
continue
start_pos = last_black_move[0]
end_pos = last_black_move[1]
diff = (end_pos[0] - start_pos[0], end_pos[1] - start_pos[1])
if diff == (2, 0) and figure == "P":
if (end_pos[0], end_pos[1] - 1) in self.gameplay.pieces.keys():
fig_type = self.gameplay.pieces[end_pos[0], end_pos[1] - 1][5]
fig_color = self.gameplay.pieces[end_pos[0], end_pos[1] - 1][4]
if fig_type == "P" and fig_color == self.gameplay.active_color:
valid_moves.append(((end_pos[0], end_pos[1] - 1), (end_pos[0] - 1, end_pos[1])))
if (end_pos[0], end_pos[1] + 1) in self.gameplay.pieces.keys():
fig_type = self.gameplay.pieces[end_pos[0], end_pos[1] + 1][5]
fig_color = self.gameplay.pieces[end_pos[0], end_pos[1] + 1][4]
if fig_type == "P" and fig_color == self.gameplay.active_color:
valid_moves.append(((end_pos[0], end_pos[1] + 1), (end_pos[0] - 1, end_pos[1])))
else:
if (pos[0] + 1, pos[1] + 1) in self.gameplay.pieces.keys() \
and self.gameplay.pieces.get((pos[0] + 1, pos[1] + 1))[4] == "w":
valid_moves.append(((pos[0], pos[1]), (pos[0] + 1, pos[1] + 1)))
if (pos[0] + 1, pos[1] - 1) in self.gameplay.pieces.keys() \
and self.gameplay.pieces.get((pos[0] + 1, pos[1] - 1))[4] == "w":
valid_moves.append(((pos[0], pos[1]), (pos[0] + 1, pos[1] - 1)))
# bicie w przelocie
last_white_move, figure = self.gameplay.get_last_move()
if last_white_move is None:
continue
start_pos = last_white_move[0]
end_pos = last_white_move[1]
diff = (end_pos[0] - start_pos[0], end_pos[1] - start_pos[1])
if diff == (-2, 0) and figure == "P":
if (end_pos[0], end_pos[1] - 1) in self.gameplay.pieces.keys():
fig_type = self.gameplay.pieces[end_pos[0], end_pos[1] - 1][5]
fig_color = self.gameplay.pieces[end_pos[0], end_pos[1] - 1][4]
if fig_type == "P" and fig_color == self.gameplay.active_color:
valid_moves.append(((end_pos[0], end_pos[1] - 1), (end_pos[0] + 1, end_pos[1])))
if (end_pos[0], end_pos[1] + 1) in self.gameplay.pieces.keys():
fig_type = self.gameplay.pieces[end_pos[0], end_pos[1] + 1][5]
fig_color = self.gameplay.pieces[end_pos[0], end_pos[1] + 1][4]
if fig_type == "P" and fig_color == self.gameplay.active_color:
valid_moves.append(((end_pos[0], end_pos[1] + 1), (end_pos[0] + 1, end_pos[1])))
def generate_pawn_moves(self, valid_moves):
""" Generowanie ruchów piona naprzód i dodanie do listy """
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "P" or color != self.gameplay.active_color:
continue
if color == "w":
if (pos[0] - 1, pos[1]) not in self.gameplay.pieces.keys():
valid_moves.append(((pos[0], pos[1]), (pos[0] - 1, pos[1])))
if pos[0] == 6 and (pos[0] - 2, pos[1]) not in self.gameplay.pieces.keys() and (pos[0] - 1, pos[1]) not\
in self.gameplay.pieces.keys():
valid_moves.append(((pos[0], pos[1]), (pos[0] - 2, pos[1])))
else:
if (pos[0] + 1, pos[1]) not in self.gameplay.pieces.keys():
valid_moves.append(((pos[0], pos[1]), (pos[0] + 1, pos[1])))
if pos[0] == 1 and (pos[0] + 2, pos[1]) not in self.gameplay.pieces.keys() and (pos[0] + 1, pos[1]) not\
in self.gameplay.pieces.keys():
valid_moves.append(((pos[0], pos[1]), (pos[0] + 2, pos[1])))
def generate_knight_moves(self, valid_moves):
""" Generowanie wszystkich ruchów konia i dodanie do listy """
possible_vecs = [(-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)]
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "N" or color != self.gameplay.active_color:
continue
for vector in possible_vecs:
new_pos = (pos[0] + vector[0], pos[1] + vector[1])
if new_pos[0] < 0 or new_pos[0] > 7 or new_pos[1] < 0 or new_pos[1] > 7:
continue
if new_pos in self.gameplay.pieces.keys() and \
self.gameplay.pieces.get(new_pos)[4] == self.gameplay.active_color:
continue
valid_moves.append((pos, new_pos))
def generate_bishop_moves(self, valid_moves):
""" Generowanie wszystkich ruchów gońca i dopisanie do listy """
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "B" or color != self.gameplay.active_color:
continue
for x in range(-1, 2, 2):
for y in range(-1, 2, 2):
vector = (x, y)
jump = 1
possible_pos = (jump * vector[0] + pos[0], jump * vector[1] + pos[1])
while in_borders(possible_pos) and possible_pos not in self.gameplay.pieces.keys():
valid_moves.append((pos, possible_pos))
jump += 1
possible_pos = (jump * vector[0] + pos[0], jump * vector[1] + pos[1])
if possible_pos in self.gameplay.pieces.keys():
if self.gameplay.pieces.get(possible_pos)[4] == self.gameplay.active_color:
pass
else:
valid_moves.append((pos, possible_pos))
def generate_rock_moves(self, valid_moves):
""" Generowanie wszystkich ruchów wieży i dopisanie do listy """
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "R" or color != self.gameplay.active_color:
continue
for x, y in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
vector = (x, y)
jump = 1
possible_pos = (jump * vector[0] + pos[0], jump * vector[1] + pos[1])
while in_borders(possible_pos) and possible_pos not in self.gameplay.pieces.keys():
valid_moves.append((pos, possible_pos))
jump += 1
possible_pos = (jump * vector[0] + pos[0], jump * vector[1] + pos[1])
if possible_pos in self.gameplay.pieces.keys():
if self.gameplay.pieces.get(possible_pos)[4] == self.gameplay.active_color:
pass
else:
valid_moves.append((pos, possible_pos))
def generate_queen_moves(self, valid_moves):
""" Generowanie wszystkich ruchów hetmana i dopisanie do listy """
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "Q" or color != self.gameplay.active_color:
continue
for x, y in [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]:
vector = (x, y)
jump = 1
possible_pos = (jump * vector[0] + pos[0], jump * vector[1] + pos[1])
while in_borders(possible_pos) and possible_pos not in self.gameplay.pieces.keys():
valid_moves.append((pos, possible_pos))
jump += 1
possible_pos = (jump * vector[0] + pos[0], jump * vector[1] + pos[1])
if possible_pos in self.gameplay.pieces.keys():
if self.gameplay.pieces.get(possible_pos)[4] == self.gameplay.active_color:
pass
else:
valid_moves.append((pos, possible_pos))
def generate_king_moves(self, valid_moves):
""" Generowanie wszystkich ruchów króla i dopisanie do listy """
for pos, img in self.gameplay.pieces.items():
color = img[4]
fig = img[5]
if fig != "K" or color != self.gameplay.active_color:
continue
for x, y in [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]:
vector = (x, y)
possible_pos = (vector[0] + pos[0], vector[1] + pos[1])
if in_borders(possible_pos):
if possible_pos in self.gameplay.pieces.keys() \
and self.gameplay.pieces.get(possible_pos)[4] == self.gameplay.active_color:
continue
valid_moves.append((pos, possible_pos))