-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement.py
95 lines (80 loc) · 3.55 KB
/
movement.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
import pieces
class Move:
def __init__(self, piece, target, board):
self.piece = piece
self.target = target
self.board = board
self.testBoard = board.clone()
self.executable = True
if not self.piece or not self.target:
self.executable = False
if not self.isValid():
self.executable = False
def __str__(self):
return f'{self.piece.getLetter()}: {self.piece.getSquare()} --> {self.target}'
def isValid(self):
if self.piece.isValid(self.target, self.board):
targetPiece = self.testBoard.getPieceAt(self.target)
if targetPiece:
targetPiece.setTaken()
testPiece = self.piece.clone()
self.testBoard.replacePiece(self.testBoard.getPieceAt(self.piece.getSquare()), testPiece)
self.checkSpecialMoves(testPiece, self.testBoard)
testPiece.setSquare(self.target)
testPiece.setMoved()
if not self.testBoard.isInCheck(testPiece.getColour()):
return True
return False
def checkSpecialMoves(self, piece, board):
self.checkCastling(piece, board)
self.checkEnPassant(piece, board)
self.checkPromotion(piece, board)
def checkCastling(self, piece, board):
if isinstance(piece, pieces.King):
x, y = pieces.distance(self.target, piece.getSquare())
if y == 0:
if x == 2:
rookSquare = (piece.getSquare()[0] + 3, piece.getSquare()[1])
targetSquare = (piece.getSquare()[0] + 1, piece.getSquare()[1])
rook = board.getPieceAt(rookSquare)
if rook:
rook.setSquare(targetSquare)
rook.setMoved()
elif x == -2:
rookSquare = (piece.getSquare()[0] - 4, piece.getSquare()[1])
targetSquare = (piece.getSquare()[0] - 1, piece.getSquare()[1])
rook = board.getPieceAt(rookSquare)
if rook:
rook.setSquare(targetSquare)
rook.setMoved()
def checkEnPassant(self, piece, board):
if isinstance(piece, pieces.Pawn):
x, y = pieces.distance(self.target, piece.getSquare())
if abs(y) == 2:
piece.setEnPassant()
else:
piece.removeEnPassant()
adjacentSquare = (piece.getSquare()[0] + x, piece.getSquare()[1])
adjacentPiece = board.getPieceAt(adjacentSquare)
if abs(x) == abs(y) == 1 and not board.getPieceAt(self.target) and adjacentPiece:
adjacentPiece.setTaken()
def checkPromotion(self, piece, board):
if isinstance(piece, pieces.Pawn):
x, y = self.target
if y == 0 or y == 7:
targetPiece = board.getPieceAt(self.target)
if targetPiece:
targetPiece.setTaken()
oldPiece = piece
piece = pieces.Queen(self.target, oldPiece.getColour())
piece.setMoved()
board.replacePiece(oldPiece, piece)
def execute(self):
if self.executable:
self.checkSpecialMoves(self.piece, self.board)
targetPiece = self.board.getPieceAt(self.target)
if targetPiece and targetPiece.getColour() != self.piece.getColour():
targetPiece.setTaken()
self.piece.setSquare(self.target)
self.piece.setMoved()
self.executable = False