-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieces.py
157 lines (126 loc) · 4.09 KB
/
pieces.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
import pygame
from itertools import product
pygame.init()
class Piece(object):
def __init__(self, team, row, column):
self.team = team
self.row = row
self.col = column
def filename(self):
if self.team > 0:
return './Images/Sprites/W'
else:
return './Images/Sprites/B'
def bishop_moves(self, state):
moves = []
for i in list(product(range(-1, 2, 2), repeat=2)):
x = self.col
y = self.row
while True:
y += i[0]
x += i[1]
if 0 <= y <= 7 and 0 <= x <= 7:
a = state[y][x]
if not a or self.team != a.team:
moves.append(str(y) + str(x))
if a and self.team != a.team:
break
else:
break
else:
break
return moves
def rook_moves(self, state):
moves = []
for i in list(product(range(-1, 2), repeat=2)):
if abs(sum(i)) == 1:
x = self.col
y = self.row
while True:
y += i[0]
x += i[1]
if 0 <= y <= 7 and 0 <= x <= 7:
a = state[y][x]
if not a or self.team != a.team:
moves.append(str(y) + str(x))
if a and self.team != a.team:
break
else:
break
else:
break
return moves
class Pawn(Piece):
id = 'P'
def moves(self, state):
moves = []
x = self.col
y = self.row
z = self.team
if x != 0:
a = state[y - z][x - 1]
if a and z != a.team:
moves.append(str(y - z) + str(x - 1))
if x != 7:
b = state[y - z][x + 1]
if b and z != b.team:
moves.append(str(y - z) + str(x + 1))
if not state[y - z][x]:
moves.append(str(y - z) + str(x))
if (y + z) % 7 == 0 and not state[y - (2 * z)][x]:
moves.append(str(y - (2 * z)) + str(x))
return moves
class Knight(Piece):
id = 'N'
def moves(self, state):
moves = []
for i in list(product(range(-2, 3), repeat=2)):
if abs(i[0]) + abs(i[1]) == 3:
x = self.col + i[0]
y = self.row + i[1]
if 0 <= x <= 7 and 0 <= y <= 7:
a = state[y][x]
if not a or self.team != a.team:
moves.append(str(y) + str(x))
return moves
class Bishop(Piece):
id = 'B'
def moves(self, state):
return super().bishop_moves(state)
class Rook(Piece):
id = 'R'
moved = False
def moves(self, state):
return super().rook_moves(state)
class Queen(Piece):
id = 'Q'
def moves(self, state):
bishop_moves = super().bishop_moves(state)
rook_moves = super().rook_moves(state)
return [*bishop_moves, *rook_moves]
class King(Piece):
id = 'K'
moved = False
immune = True
def moves(self, state):
moves = []
king_moves = list(product(range(-1, 2), repeat=2))
king_moves.remove((0, 0))
for i in king_moves:
x = self.col + i[0]
y = self.row + i[1]
if 0 <= x <= 7 and 0 <= y <= 7:
a = state[y][x]
if not a or self.team != a.team:
moves.append(str(y) + str(x))
if not self.moved:
b = state[self.row]
if not (b[1] or b[2] or b[3]):
lr = b[0]
if lr and lr.id == 'R' and not lr.moved:
moves.append(str(self.row) + '2')
if not (b[5] or b[6]):
rr = b[7]
if rr and rr.id == 'R' and not rr.moved:
moves.append(str(self.row) + '6')
return moves