-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieces.py
355 lines (313 loc) · 12.2 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Daniel Garcia
# gamda_yo@csu.fullerton.edu
# Assignment 6
# Game of Chess
# piece classes
# This file contains the classes for each different
# piece, part of "model"
from chessLocals import *
import square
""" All pieces have:
Variables:
1. color - either white or black
2. pos - position tuple
3. iAm - char from locals.py to indicate the type of
piece that it is
Methods:
1. move( newpos ): change position attribute
2. validMoves( squares, pieces ):
- Parameters are the dicts declared in board class
- Return value is a list of position tuples
3. PRIVATE isOppositeSide( square ):
- Parameter is a square, it contains the necessary info
- Returns true if the pieces are on opposing teams or
square is empty.
4. squaresAttacked( squares ):
- Parameter is the squares dict from the board class
- Return value is a list of position tuples. It includes
all squares that the piece "sees" including squares
occupied by allies and squares past the enemy king.
* King provides additional info, it knows if it has moved.
* Pawn also knows if has moved; if it hasn't, valid moves
includes the second square if available
"""
## All variables contained in these classes are pickable
# so no need for __getstate__ or __setstate__
class pawn:
def __init__( self, newside, newpos ):
self.color = newside
self.pos = newpos
self.iAm = PAWN
self.hasMoved = False
def _isOppositeSide( self, square ):
return not (self.color == square.contentColor)
def move( self, newpos ):
self.pos = newpos
self.hasMoved = True
def validMoves( self, squares ):
# white moves up, black moves down
moves = []
posToCheck = self.pos
doneChecking = False
if( self.color == WHITE ):
# check top squares
###################
# top
posToCheck = squares[self.pos].neighbor(TOP)
if( posToCheck ): # posToCheck != None
if( squares[posToCheck].isEmpty() ):
moves.append(posToCheck)
else:
doneChecking = True
# second top square if it hasn't moved
if not self.hasMoved and not doneChecking:
posToCheck = squares[posToCheck].neighbor(TOP)
if( posToCheck ): # posToCheck != None
if( squares[posToCheck].isEmpty() ):
moves.append(posToCheck)
# top left
posToCheck = squares[self.pos].neighbor(TOPLEFT)
if( posToCheck ): #posToCheck != None
if ( not squares[posToCheck].isEmpty() and \
self._isOppositeSide(squares[posToCheck]) ):
moves.append(posToCheck)
# top right
posToCheck = squares[self.pos].neighbor(TOPRIGHT)
if( posToCheck ): #posToCheck != None
if ( not squares[posToCheck].isEmpty() and \
self._isOppositeSide(squares[posToCheck]) ):
moves.append(posToCheck)
else:
# check bottom squares
######################
# btm
posToCheck = squares[self.pos].neighbor(BTM)
if( posToCheck ): # posToCheck != None
if( squares[posToCheck].isEmpty() ):
moves.append(posToCheck)
else:
doneChecking = True
# second btm square if it hasn't moved
if not self.hasMoved and not doneChecking:
posToCheck = squares[posToCheck].neighbor(BTM)
if( posToCheck ): # posToCheck != None
if( squares[posToCheck].isEmpty() ):
moves.append(posToCheck)
# btm left
posToCheck = squares[self.pos].neighbor(BTMLEFT)
if( posToCheck ): #posToCheck != None
if ( not squares[posToCheck].isEmpty() and \
self._isOppositeSide(squares[posToCheck]) ):
moves.append(posToCheck)
# btm right
posToCheck = squares[self.pos].neighbor(BTMRIGHT)
if( posToCheck ): #posToCheck != None
if ( not squares[posToCheck].isEmpty() and \
self._isOppositeSide(squares[posToCheck]) ):
moves.append(posToCheck)
return moves
def squaresAttacked( self, squares ):
attackedSquares = []
# only two options, topleft and topright for white
# btmleft and btmright for black
if( self.color == WHITE ):
posToCheck = squares[self.pos].neighbor(TOPLEFT)
if not( posToCheck == None ):
attackedSquares.append(posToCheck)
posToCheck = squares[self.pos].neighbor(TOPRIGHT)
if not( posToCheck == None ):
attackedSquares.append(posToCheck)
else: # self.color == BLACK
posToCheck = squares[self.pos].neighbor(BTMLEFT)
if not( posToCheck == None ):
attackedSquares.append(posToCheck)
posToCheck = squares[self.pos].neighbor(BTMRIGHT)
if not( posToCheck == None ):
attackedSquares.append(posToCheck)
return attackedSquares
class rook:
def __init__( self, newside, newpos ):
self.color = newside
self.pos = newpos
self.iAm = ROOK
def _isOppositeSide( self, square ):
return not (self.color == square.contentColor)
def move( self, newpos ):
self.pos = newpos
def validMoves( self, squares ):
moves = []
for i in STRAIGHT_DIR:
newmoves = square.line( squares, self.pos, i , False )
if( len(newmoves) > 1 ):
if not self._isOppositeSide( squares[newmoves[-1]] ):
# remove last square becaues it is an allie
newmoves = newmoves[0:-1]
elif( len(newmoves) == 1 ):
if not self._isOppositeSide( squares[newmoves[0]] ):
# the only move is to a square containing an allie
newmoves = []
moves.extend( newmoves )
return moves
def squaresAttacked( self, squares ):
attackedSquares = []
for i in STRAIGHT_DIR:
newSqrs = square.line( squares, self.pos, i , True )
attackedSquares.extend( newSqrs )
return attackedSquares
class knight:
def __init__( self, newside, newpos ):
self.color = newside
self.pos = newpos
self.iAm = KNIGHT
def _isOppositeSide( self, square ):
return not (self.color == square.contentColor)
def move( self, newpos ):
self.pos = newpos
def validMoves( self, squares ):
moves = []
posToCheck = []
x = self.pos[0]
y = self.pos[1]
# 8 total possible moves:
## top top right
posToCheck.append((x+1,y+2))
## top top left
posToCheck.append((x-1,y+2))
## top right right
posToCheck.append((x+2,y+1))
## top left left
posToCheck.append((x-2,y+1))
## btm btm right
posToCheck.append((x+1,y-2))
## btm btm left
posToCheck.append((x-1,y-2))
## btm right right
posToCheck.append((x+2,y-1))
## btm left left
posToCheck.append((x-2,y-1))
for x,y in posToCheck:
# make sure its inside
if not( x>h or y>8 or x<a or y<1 ):
# make sure square is empty or w/enemy
if( squares[(x,y)].isEmpty() or
self._isOppositeSide(squares[(x,y)]) ):
moves.append((x,y))
return moves
def squaresAttacked( self, squares ):
attackedSqrs = []
posToCheck = []
x = self.pos[0]
y = self.pos[1]
# 8 total possible moves:
## top top right
posToCheck.append((x+1,y+2))
## top top left
posToCheck.append((x-1,y+2))
## top right right
posToCheck.append((x+2,y+1))
## top left left
posToCheck.append((x-2,y+1))
## btm btm right
posToCheck.append((x+1,y-2))
## btm btm left
posToCheck.append((x-1,y-2))
## btm right right
posToCheck.append((x+2,y-1))
## btm left left
posToCheck.append((x-2,y-1))
for x,y in posToCheck:
# make sure its inside
if not( x>h or y>8 or x<a or y<1 ):
attackedSqrs.append((x,y))
return attackedSqrs
class bishop:
def __init__( self, newside, newpos ):
self.color = newside
self.pos = newpos
self.iAm = BISHOP
def _isOppositeSide( self, square ):
return not (self.color == square.contentColor)
def move( self, newpos ):
self.pos = newpos
def validMoves( self, squares ):
moves = []
for i in DIAGONAL_DIR:
newmoves = square.line( squares, self.pos, i , False )
if( len(newmoves) > 1 ):
if not self._isOppositeSide( squares[newmoves[-1]] ):
# remove last square becaues it is an allie
newmoves = newmoves[0:-1]
elif( len(newmoves) == 1 ):
if not self._isOppositeSide( squares[newmoves[0]] ):
newmoves = []
moves.extend( newmoves )
return moves
def squaresAttacked( self, squares ):
attackedSquares = []
for i in DIAGONAL_DIR:
newSqrs = square.line( squares, self.pos, i , True )
attackedSquares.extend( newSqrs )
return attackedSquares
class queen:
def __init__( self, newside, newpos ):
self.color = newside
self.pos = newpos
self.iAm = QUEEN
def _isOppositeSide( self, square ):
return not (self.color == square.contentColor)
def move( self, newpos ):
self.pos = newpos
def validMoves( self, squares ):
moves = []
for i in DIRECTIONS:
newmoves = square.line( squares, self.pos, i , False )
if( len(newmoves) > 1 ):
if not self._isOppositeSide( squares[newmoves[-1]] ):
# remove last square becaues it is an allie
newmoves = newmoves[0:-1]
elif( len(newmoves) == 1 ):
if not self._isOppositeSide( squares[newmoves[0]] ):
newmoves = []
moves.extend( newmoves )
return moves
def squaresAttacked( self, squares ):
attackedSquares = []
for i in DIRECTIONS:
newSqrs = square.line( squares, self.pos, i , True )
attackedSquares.extend( newSqrs )
return attackedSquares
class king:
""" Additional information: king know if it has moved, useful
to determine if it can castle. """###validMoves() is used like in
#all other classes, but will include castle moves if possible."""
def __init__( self, newside, newpos ):
self.color = newside
self.pos = newpos
self.iAm = KING
#####################
self.hasMoved = False
def _isOppositeSide( self, square ):
return not (self.color == square.contentColor)
def move( self, newpos ):
self.pos = newpos
self.hasMoved = True
def validMoves( self, squares ):
moves = []
for i in DIRECTIONS:
neighborpos = squares[self.pos].neighbor(i)
if( neighborpos and self._isOppositeSide( squares[neighborpos] ) ):
# neighborpos != None and neighbor content is not ally
if( self.color == WHITE ):
if not squares[neighborpos].attackedByBlack():
moves.append( neighborpos )
else: #self.color == BLACK
if not squares[neighborpos].attackedByWhite():
moves.append( neighborpos )
return moves
def squaresAttacked( self, squares ):
attackedSqrs = []
for i in DIRECTIONS:
neighborpos = squares[self.pos].neighbor(i)
if not( neighborpos == None ):
attackedSqrs.append( neighborpos )
return attackedSqrs