-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpieces.py
233 lines (203 loc) · 6.54 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
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
'''
Contains all of the different pieces in the game
as well as a base 'Piece' class
'''
class Piece:
'''
Superclass for piece type.
'''
# pylint: disable=too-many-instance-attributes
def __init__(self):
'''
Initializes the piece object
'''
self.up_allowed = False
self.left_allowed = False
self.right_allowed = False
self.down_allowed = False
self.diag_left = False
self.diag_right = False
self.move_set = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
self.name = None
self.color = None
self.value = None
def get_name(self):
'''
Returns the name of the piece
'''
return self.name
def set_name(self, name):
'''
Gets the name of this piece (e.g. "Rook", "Pawn")
'''
self.name = name
def get_color(self):
'''
Returns the color of the piece
'''
return self.color
def set_color(self, color=0):
'''
Sets the color of the piece (0 for white and 1 for black)
'''
self.color = color
def set_moves_horiz(self, upallowed=False, leftallowed=False,
rightallowed=False, downallowed=False):
''' Sets where a piece is able to move relative to itself'''
self.up_allowed = upallowed
self.left_allowed = leftallowed
self.right_allowed = rightallowed
self.down_allowed = downallowed
def set_moves_diag(self, diagleft=False, diagright=False):
''' Sets where a piece is able to move relative to itself '''
self.diag_left = diagleft
self.diag_right = diagright
def set_moves(self, moveset=None):
'''
Gets the list of squares a piece is allowed to go to relative to itself
'''
if moveset is None:
self.move_set = []
def get_allowed_moves(self):
'''
Returns a list of the moves a particular piece is allowed to make
'''
return [self.up_allowed, self.left_allowed, self.right_allowed,
self.down_allowed, self.diag_left, self.diag_right,
self.move_set]
def set_value(self, value):
'''
Sets the centipawn value of a piece (1 represents 100 centipawns)
'''
self.value = value
def get_value(self):
'''
Returns the centipawn value of a given piece
(1 represents 100 centipawns)
'''
return self.value
class Rook(Piece):
''' Rook Piece '''
def __init__(self, color):
'''
Creates a rook object of a given color with the allowed
moveset and value 5
'''
super(Rook, self).__init__()
self.set_name("Rook")
self.set_color(color)
self.up_allowed = True
self.set_moves_horiz(leftallowed=True, rightallowed=True, upallowed=True, downallowed=True)
self.set_value(5)
def __str__(self):
'''
Returns the String representation of the Rook
'''
return f"{self.get_name()}({self.get_color()})"
class Queen(Piece):
'''
Queen Piece
'''
def __init__(self, color):
'''
Creates a Queen object of a given color with the allowed
moveset and value 9
'''
super(Queen, self).__init__()
self.set_name("Queen")
self.set_color(color)
self.up_allowed = True
self.set_moves_horiz(upallowed=True, leftallowed=True, rightallowed=True, downallowed=True)
self.set_moves_diag(diagleft=True, diagright=True)
self.set_value(9)
def __str__(self):
'''
Returns a string representation of the queen of a given color
'''
return f"{self.get_name()}({self.get_color()})"
class Pawn(Piece):
''' Pawn Piece '''
def __init__(self, color):
'''
Creates a Queen object of a given color with the allowed
moveset and value 1
'''
super(Pawn, self).__init__()
self.set_name("Pawn")
self.set_color(color)
self.up_allowed = True
if color == 0:
self.set_moves(moveset=[[-1, 3, -1], [2, 1, 2], [-1, 0, -1]])
else:
self.set_moves(moveset=[[-1, 0, -1], [2, 1, 2], [-1, 3, -1]])
self.set_value(1)
self.has_moved = False
def __str__(self):
'''
Returns a string representation of the pawn of a given color
'''
return f"{self.get_name()}({self.get_color()})"
class Knight(Piece):
''' Knight Piece '''
def __init__(self, color):
'''
Creates a Knight object of a given color with the allowed moveset
and value 3
'''
super(Knight, self).__init__()
self.knight_moves = [[-1, 1, -1, 1, -1],
[1, -1, -1, -1, 1],
[-1, -1, 0, -1, -1],
[1, -1, -1, -1, 1],
[-1, 1, -1, 1, -1]]
self.set_name("Knight")
self.set_color(color)
self.up_allowed = True
self.set_moves(moveset=self.knight_moves)
self.set_value(3)
def __str__(self):
'''
Returns a string representation of a knight of a given color
'''
return f"{self.get_name()}({self.get_color()})"
class King(Piece):
''' King Piece '''
def __init__(self, color):
'''
Creates a King object of a given color with the allowed moveset
and value 0
'''
super(King, self).__init__()
self.king_moves = [[1, 1, 1],
[1, 0, 1],
[1, 1, 1]]
self.set_name("King")
self.set_color(color)
self.up_allowed = True
self.set_moves(moveset=self.king_moves)
self.set_value(0)
def __str__(self):
'''
Returns a string representation of a King of a given color
'''
return f"{self.get_name()}({self.get_color()})"
class Bishop(Piece):
''' Bishop Piece '''
def __init__(self, color):
'''
Creates a Bishop object of a given color with the allowed moveset
and value 4
'''
super(Bishop, self).__init__()
self.set_name("Bishop")
self.set_color(color)
self.up_allowed = True
self.set_moves_diag(diagright=True, diagleft=True)
self.set_value(3)
def __str__(self):
'''
Returns a string representation of a King of a given color
'''
return f"{self.get_name()}({self.get_color()})"