-
Notifications
You must be signed in to change notification settings - Fork 1
/
preview.py
68 lines (61 loc) · 2.18 KB
/
preview.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
import piece
class Preview(piece.Piece):
"""
Class that represents a preview piece.
"""
def __init__(self, lowerSide):
self.origin = Preview
self.lowerSide = lowerSide
self.canPromote = True
self.ID = id(Preview)
def getCaptureRepr(self):
"""
:return: representation after being captured
"""
if self.lowerSide:
return "p"
else:
return "P"
def __repr__(self):
if self.lowerSide is None:
return "__"
if self.lowerSide:
return " p"
else:
return " P"
def generatePossibleMoves(self, board, start):
"""
:param board: the chess board
:param start: the starting position
:return: all valid moves of drive starting from index position.
"""
i, j = start.getX(), start.getY()
if self.lowerSide:
if 1 <= i <= 4 and 0 <= j <= 4 \
and (not board[i-1][j].getPiece() or board[i-1][j].getPiece().isLower() != start.getPiece().isLower()):
return [board[i-1][j]]
else:
if 0 <= i <= 3 and 0 <= j <= 4 \
and (not board[i+1][j].getPiece() or board[i+1][j].getPiece().isLower() != start.getPiece().isLower()):
return [board[i+1][j]]
return []
def canMove(self, player, board, start, end, changeCapture = True):
"""
:param player: the current player
:param board: the chess board
:param start: the starting position
:param end: the ending position
:param changeCapture: boolean indicating whether the captureList needs to be changed
:return: whether the player could move from start to end in the given board
"""
if not self.checkMoveBasics(start, end):
return False
allMoves = self.generatePossibleMoves(board, start)
if end not in allMoves:
return False
if end.getPiece():
if changeCapture:
player.setCapture(end.getPiece().origin(player.lowerSide))
end.setPiece(start.getPiece())
start.setPiece(None)
return True