-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
192 lines (162 loc) · 6.5 KB
/
classes.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
import pygame as pg
class Tile(pg.sprite.Sprite):
tilesize = 180
def __init__(self, x, y):
super().__init__()
self.x = x
self.y = y
self.color = (255, 255, 255) if (x + y) % 2 == 0 else (51, 102, 0)
self.rect = pg.Rect(x * self.tilesize, y * self.tilesize, self.tilesize, self.tilesize)
self.image = pg.Surface((self.tilesize, self.tilesize))
self.image.fill(self.color)
class Piece(pg.sprite.Sprite):
def __init__(self, x, y, color, type):
super().__init__()
self.x = x
self.y = y
self.color = color
self.image = pg.image.load(f"images/{color}/{color}_{type}.png")
self.rect = pg.Rect(x * Tile.tilesize, y * Tile.tilesize, Tile.tilesize, Tile.tilesize)
self.movable_tiles = []
self.moved = False
def kill(self,live_pieces,dead_pieces):
dead_pieces.add(self)
live_pieces.remove(self)
def outline(self,screen):
pg.draw.rect(screen, (255, 0, 0), self.rect, 5)
class King(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "king")
def kill(self):
raise AttributeError("Kings cannot be killed")
def legal_move(self, chess_grid):
lst = []
for i in range(self.x - 1, self.x + 2):
for j in range(self.y - 1, self.y + 2):
if 0 <= i <= 7 and 0 <= j <= 7 and (i, j) != (self.x, self.y):
if chess_grid[i][j] and chess_grid[i][j].color != self.color:
lst.append([i, j])
elif not chess_grid[i][j]:
lst.append([i, j])
self.movable_tiles = lst
self.movable_tiles = lst
def castle(self,target,chess_grid):
pass
class Rook(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "rook")
def castle(self,target,chess_grid):
pass
def legal_move(self,chess_grid):
lst = []
for i in range(self.x+1,8):
if chess_grid[i][self.y]:
if chess_grid[i][self.y].color != self.color:
lst.append([i,self.y])
break
else:
lst.append([i,self.y])
for i in range(self.x-1,-1,-1):
if chess_grid[i][self.y]:
if chess_grid[i][self.y].color != self.color:
lst.append([i, self.y])
break
else:
lst.append([i,self.y])
for j in range(self.y+1,8):
if chess_grid[self.x][j]:
if chess_grid[self.x][j].color != self.color:
lst.append([self.x,j])
break
else:
lst.append([self.x,j])
for j in range(self.y-1,-1,-1):
if chess_grid[self.x][j]:
if chess_grid[self.x][j].color != self.color:
lst.append([self.x,j])
break
else:
lst.append([self.x,j])
self.movable_tiles = lst
class Bishop(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "bishop")
def legal_move(self, chess_grid):
lst = []
directions = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
for dx, dy in directions:
i, j = self.x + dx, self.y + dy
while 0 <= i <= 7 and 0 <= j <= 7:
if chess_grid[i][j]:
if chess_grid[i][j].color != self.color:
lst.append([i, j])
break
else:
lst.append([i, j])
i += dx
j += dy
self.movable_tiles = lst
class Queen(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "queen")
def legal_move(self,chess_grid):
lst = []
b = Bishop(self.x,self.y,self.color)
r = Rook(self.x,self.y,self.color)
b.legal_move(chess_grid)
r.legal_move(chess_grid)
self.movable_tiles = b.movable_tiles + r.movable_tiles
class Knight(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "knight")
def legal_move(self,chess_grid):
lst = []
for i in range(-2,3):
for j in range(-2,3):
if abs(i) + abs(j) == 3:
if 0 <= self.x+i <= 7 and 0 <= self.y+j <= 7:
if chess_grid[self.x+i][self.y+j] and chess_grid[self.x+i][self.y+j].color != self.color:
lst.append([self.x+i,self.y+j])
lst.append([self.x+i,self.y+j])
self.movable_tiles = lst
class Pawn(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "pawn")
def legal_move(self,chess_grid):
lst = []
try:
if not self.moved:
if self.color == 'white':
if not chess_grid[self.x][self.y-1]:
lst.append([self.x,self.y-1])
if not chess_grid[self.x][self.y-2]:
lst.append([self.x,self.y-2])
else:
if not chess_grid[self.x][self.y+1]:
lst.append([self.x,self.y+1])
if not chess_grid[self.x][self.y+2]:
lst.append([self.x,self.y+2])
self.moved = True
else:
if self.color == 'white':
if not chess_grid[self.x][self.y-1]:
lst.append([self.x,self.y-1])
else:
if not chess_grid[self.x][self.y+1]:
lst.append([self.x,self.y+1])
if self.color == 'white':
if chess_grid[self.x+1][self.y-1] and chess_grid[self.x+1][self.y-1].color != self.color:
lst.append([self.x+1,self.y-1])
if chess_grid[self.x-1][self.y-1] and chess_grid[self.x-1][self.y-1].color != self.color:
lst.append([self.x-1,self.y-1])
else:
if chess_grid[self.x+1][self.y+1] and chess_grid[self.x+1][self.y+1].color != self.color:
lst.append([self.x+1,self.y+1])
if chess_grid[self.x-1][self.y+1] and chess_grid[self.x-1][self.y+1].color != self.color:
lst.append([self.x-1,self.y+1])
except KeyError:
pass
self.movable_tiles = lst
def promote(self,live_pieces,piece):
live_pieces.remove(self)
live_pieces.add(piece.capitalize()(self,self.x,self.y,self.color))