-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpawn.py
More file actions
64 lines (46 loc) · 1.86 KB
/
Copy pathpawn.py
File metadata and controls
64 lines (46 loc) · 1.86 KB
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
from piece import ChessPiece
class Pawn(ChessPiece):
def __init__(self, color, row, col):
super().__init__(color, row, col)
self.hasMoved = False
def move(self, row, col, board):
# board[row= row][col= col]
if self.is_valid_moves(row, col, board) == False:
return False
self.hasMoved = True
self.position = (row, col)
if isinstance(board[row][col], ChessPiece):
board[row][col].capture()
return True
def is_valid_moves(self, row, col, board):
step_size = 2 if self.hasMoved == False else 1
step_direction = step_size if self.color == "black" else (step_size * -1)
where_to = board[row][col]
print(self.hasMoved, step_size, step_direction)
if isinstance(where_to, ChessPiece):
if where_to.color == self.color:
return False
row_dir = -1 if self.position[0] > row else 1
if not self.position[0] + row_dir == row:
print(
"Pawn can only move 1 step diagonally at a time when capturing an enemy piece"
)
return False
else:
if not self.position[1] == col:
print("Pawn can only move diagonally when capturing an enemy piece")
return False
if self.color == "black":
if not self.position[0] + step_direction >= row:
print("Pawn can only move 1 step forward at a time ")
return False
else:
if not self.position[0] + step_direction <= row:
print("Pawn can only move 1 step forward at a time ")
return False
return True
def capture(self):
self.wasCaptured = True
print("Captured")
def draw(self):
return "wP" if self.color == "white" else "bP"