-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piece.py
80 lines (65 loc) · 1.75 KB
/
Piece.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
# File: Piece.py
# By: Christopher Lin
# Date: 2/10/20
# piece superclass
from graphics import *
#Piece class
class Piece():
#constructor
def __init__(self, coord, board, color, imgStr):
#self.board is board object
self.coord, self.board, self.color = coord, board, color
self.prevCoord = None
#moves piece into 2D array
self.board.movePiece(self)
self.imgStr = imgStr
self.isEaten = False
self.image = Image(Point(-100,-100),"pieces/"+self.imgStr)
self.undrawn = False
self.reset = coord
self.hasMoved = False
#getters
def getCoord(self):
return self.coord
def getHasMoved(self):
return self.hasMoved
def getPrevCoord(self):
return self.prevCoord
def getColor(self):
return self.color
def getImageObj(self):
return self.image
def getIsEaten(self):
return self.isEaten
def getImageStr(self):
return self.imgStr
#sets boolean and returns obj
def kill(self):
self.isEaten = True
return self
#moves piece
def move(self, coord):
self.prevCoord = self.coord
self.coord = coord
#moves piece in 2d array
self.board.movePiece(self)
self.hasMoved = True
#gets possible moves (king check included)
def getPossibleMoves(self):
#gets own color's king coords
kingCoords = self.board.getKingCoord(self.color)
#each piece has its own getPossibleMovesNoCheck
possibleMoves = self.getPossibleMovesNoCheck()
legalMoves = []
for move in possibleMoves:
#checks if move puts king in check
if not self.board.check(kingCoords,move,self):
legalMoves.append(move)
return legalMoves
#raises exception if subclass doesnt have method
def getPossibleMovesNoCheck(self): raise Exception("Undefined")
#undraws image obj
def undraw(self):
if not self.undrawn:
self.image.undraw()
self.undrawn = True