forked from marcusbuffett/command-line-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.py
50 lines (41 loc) · 1.55 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
from Coordinate import Coordinate as C
from Move import Move
WHITE = True
BLACK = False
X = 0
Y = 1
class Piece:
def __init__(self, board, side, position, movesMade=0):
self.board = board
self.side = side
self.position = position
movesMade = 0
def __str__(self):
sideString = 'White' if self.side == WHITE else 'Black'
return 'Type : ' + type(self).__name__ + \
' - Position : ' + str(self.position) + \
" - Side : " + sideString + \
' -- Value : ' + str(self.value)
def movesInDirectionFromPos(self, pos, direction, side):
for dis in range(1, 8):
movement = C(dis * direction[X], dis * direction[Y])
newPos = pos + movement
if self.board.isValidPos(newPos):
pieceAtNewPos = self.board.pieceAtPosition(newPos)
if pieceAtNewPos is None:
yield Move(self, newPos)
elif pieceAtNewPos is not None:
if pieceAtNewPos.side != side:
yield Move(self, newPos, pieceToCapture=pieceAtNewPos)
return
def __eq__(self, other):
if self.board == other.board and \
self.side == other.side and \
self.position == other.position and \
self.__class__ == other.__class__:
return True
return False
def copy(self):
cpy = self.__class__(self.board, self.side, self.position,
movesMade=self.movesMade)
return cpy