-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrook.py
More file actions
46 lines (33 loc) · 1.42 KB
/
Copy pathrook.py
File metadata and controls
46 lines (33 loc) · 1.42 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
from piece import ChessPiece
class Rook(ChessPiece):
def __init__(self, color, row, col):
super().__init__(color, row, col)
def is_valid_move(self, row, col, board):
if self.position[0] != row and self.position[1] != col:
print("Rook can only move vertically and horizontally")
return False
if self.position[0] == row:
step = 1 if col > self.position[1] else -1
for c in range(self.position[1] + step, col, step):
if isinstance(board[row][c], ChessPiece):
print("There's an obstruction on the path")
return False
else:
step = 1 if row > self.position[0] else -1
for r in range(self.position[0] + step, row, step):
if isinstance(board[r][col], ChessPiece):
print("There's an obstruction on the path")
return False
return True
def move(self, row, col, board):
if not self.is_valid_move(row, col, board):
return False
if isinstance(board[row][col], ChessPiece):
if board[row][col].color == self.color:
print("Can't move into a piece of the same color")
return False
board[row][col].capture()
self.position = (row, col)
return True
def draw(self):
return "wR" if self.color == "white" else "bR"