-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbishop.py
More file actions
95 lines (80 loc) · 3.28 KB
/
bishop.py
File metadata and controls
95 lines (80 loc) · 3.28 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
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 chess_rule
from board import Board
from piece import Piece
class Bishop(Piece):
def __init__(self, pos_X: int, pos_Y: int, side_: bool):
"""sets initial values by calling the constructor of Piece"""
super().__init__(pos_X, pos_Y, side_)
def can_reach(self, pos_X: int, pos_Y: int, B: Board) -> bool:
"""
checks if this bishop can move to coordinates pos_X, pos_Y
on board B according to rule [Rule1] and [Rule3] (see section Intro)
Hint: use is_piece_at
"""
# out of range
if pos_X > B[0] or pos_Y > B[0]:
return False
if not self.is_diagonal(pos_X, pos_Y):
return False
direction = self.get_direction(pos_X, pos_Y)
if not direction:
return False
for step in range(1, abs(self.pos_y - pos_Y) + 1):
move_x = self.pos_x + (step * direction[0])
move_y = self.pos_y + (step * direction[1])
if chess_rule.is_out_board(move_x, move_y, B):
break
if chess_rule.is_piece_at(move_x, move_y, B):
encountering_piece = chess_rule.piece_at(move_x, move_y, B)
if encountering_piece.side != self.side:
if encountering_piece.pos_x == pos_X and encountering_piece.pos_y == pos_Y:
return True
return False
return True
def is_diagonal(self, x: int, y: int):
return abs(self.pos_x - x) == abs(self.pos_y - y)
def get_direction(self, pos_X: int, pos_Y: int):
if self.pos_x < pos_X and self.pos_y < pos_Y:
x = 1
y = 1
elif self.pos_x > pos_X and self.pos_y < pos_Y:
x = -1
y = 1
elif self.pos_x < pos_X and self.pos_y > pos_Y:
x = 1
y = -1
elif self.pos_x > pos_X and self.pos_y > pos_Y:
x = -1
y = -1
else:
return False
return x, y
def can_move_to(self, pos_X: int, pos_Y: int, B: Board) -> bool:
"""
checks if this bishop can move to coordinates pos_X, pos_Y
on board B according to all chess rules
Hints:
- firstly, check [Rule1] and [Rule3] using can_reach
- secondly, check if result of move is capture using is_piece_at
- if yes, find the piece captured using piece_at
- thirdly, construct new board resulting from move
- finally, to check [Rule4], use is_check on new board
"""
if not self.can_reach(pos_X, pos_Y, B):
return False
new_board = self.build_new_board(pos_X, pos_Y, B)
if chess_rule.is_check(self.side, new_board):
return False
return True
def move_to(self, pos_X: int, pos_Y: int, B: Board) -> Board:
"""
returns new board resulting from move of this rook to coordinates pos_X, pos_Y on board B
assumes this move is valid according to chess rules
"""
new_board = self.build_new_board(pos_X, pos_Y, B)
return new_board
def build_new_board(self, pos_X: int, pos_Y: int, B: Board):
new_list = self.remove_piece_from_board(pos_X, pos_Y, B)
new_piece = Bishop(pos_X, pos_Y, self.side)
new_list.append(new_piece)
return B[0], new_list