forked from marcusbuffett/command-line-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPawn.py
96 lines (75 loc) · 4.02 KB
/
Pawn.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
from Rook import Rook
from Bishop import Bishop
from Knight import Knight
from Queen import Queen
from Piece import Piece
from Coordinate import Coordinate as C
from Move import Move
WHITE = True
BLACK = False
class Pawn (Piece) :
stringRep = 'p'
value = 1
def __init__(self, board, side, position, movesMade=0) :
super(Pawn, self).__init__(board, side, position)
self.movesMade = movesMade
def getPossibleMoves(self) :
currentPosition = self.position
# Pawn moves one up
movement = C(0, 1) if self.side == WHITE else C(0, -1)
advanceOnePosition = currentPosition + movement
if self.board.isValidPos(advanceOnePosition) :
#Promotion moves
if self.board.pieceAtPosition(advanceOnePosition) is None :
col = advanceOnePosition[1]
if col == 7 or col == 0:
piecesForPromotion = [Rook(self.board, self.side, advanceOnePosition), Knight(self.board, self.side, advanceOnePosition), Bishop(self.board, self.side, advanceOnePosition), Queen(self.board, self.side, advanceOnePosition)]
for piece in piecesForPromotion :
move = Move(self, advanceOnePosition)
move.promotion = True
move.specialMovePiece = piece
yield move
else :
yield Move(self, advanceOnePosition)
#Pawn moves two up
if self.movesMade == 0 :
movement = C(0, 2) if self.side == WHITE else C(0, -2)
advanceTwoPosition = currentPosition + movement
if self.board.isValidPos(advanceTwoPosition) :
if self.board.pieceAtPosition(advanceTwoPosition) is None and self.board.pieceAtPosition(advanceOnePosition) is None:
yield Move(self, advanceTwoPosition)
#Pawn takes
movements = [C(1,1), C(-1,1)] if self.side == WHITE else [C(1,-1), C(-1,-1)]
for movement in movements :
newPosition = self.position + movement
if self.board.isValidPos(newPosition) :
pieceToTake = self.board.pieceAtPosition(newPosition)
if pieceToTake and pieceToTake.side != self.side :
col = newPosition[1]
#Promotions
if col == 7 or col == 0:
piecesForPromotion = [Rook(self.board, self.side, newPosition), Knight(self.board, self.side, newPosition), Bishop(self.board, self.side, newPosition), Queen(self.board, self.side, newPosition)]
for piece in piecesForPromotion :
move = Move(self, advanceOnePosition)
move.promotion = True
move.specialMovePiece = piece
yield move
else :
yield Move(self, newPosition, pieceToCapture = pieceToTake)
#En pessant
movements = [C(1,1), C(-1,1)] if self.side == WHITE else [C(1,-1), C(-1,-1)]
for movement in movements :
posBesidePawn = self.position + C(movement[0], 0)
if self.board.isValidPos(posBesidePawn) :
pieceBesidePawn = self.board.pieceAtPosition(posBesidePawn)
lastPieceMoved = self.board.getLastPieceMoved()
lastMoveWasAdvanceTwo = False
lastMove = self.board.getLastMove()
if lastMove :
if lastMove.newPos - lastMove.oldPos == C(0,2) or lastMove.newPos - lastMove.oldPos == C(0,-2) :
lastMoveWasAdvanceTwo = True
if pieceBesidePawn and pieceBesidePawn.stringRep == 'p' and pieceBesidePawn.side != self.side and lastPieceMoved is pieceBesidePawn and lastMoveWasAdvanceTwo:
move = Move(self, self.position + movement, pieceToCapture = pieceBesidePawn)
move.pessant = True
move.specialMovePiece = pieceBesidePawn
yield move