-
Notifications
You must be signed in to change notification settings - Fork 0
/
Horse.py
64 lines (55 loc) · 2.44 KB
/
Horse.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
from MasterPiece import MasterPiece
class Horse(MasterPiece):
"""
Represents a horse piece. Inherits from MasterPiece. Will be instantiated in the Game class and contained
in the Board class.
"""
def __init__(self, color: str, name: str, type, location: tuple):
"""
Uses MasterPiece method to initialize the piece.
"""
super().__init__(color, name, type, location)
def valid_move(self, next_loc: tuple) -> bool:
"""
Returns whether or not the piece can move from the current location to the next location. Only
handles whether the space can be moved to by movement rules of the piece. Does not handle
whether the space is occupied, etc.
param next_loc: space to move to
return: True if space is within reach, else False
"""
cur_loc = self._location
# horizontal move, then diagonal
if abs(next_loc[0] - cur_loc[0]) == 1 and abs(next_loc[1] - cur_loc[1]) == 2:
return True
# vertical move, then diagonal
elif abs(next_loc[0] - cur_loc[0]) == 2 and abs(next_loc[1] - cur_loc[1]) == 1:
return True
return False
def is_blocked(self, next_loc: tuple, board) -> bool:
"""
Determines whether a piece is blocked from moving to it's desired location. Does not take in to account if
the space is a valid move for the piece.
param next_loc: desired move-to location of the piece
param board: the current game board
return: True if piece is blocked, else False
"""
if board.get_piece(next_loc) is not None and board.get_piece(next_loc).get_color() == self._color:
return True
cur_loc = self._location
# check horizontal then diagonal
if abs(next_loc[0] - cur_loc[0]) == 1:
if cur_loc[1] < next_loc[1]:
cur_loc = (cur_loc[0], cur_loc[1] + 1)
else:
cur_loc = (cur_loc[0], cur_loc[1] - 1)
# check vertical then diagonal
else:
if cur_loc[0] < next_loc[0]:
cur_loc = (cur_loc[0] + 1, cur_loc[1])
else:
cur_loc = (cur_loc[0] - 1, cur_loc[1])
# do not need to check the destination since it was checked above
# only need to check the orthogonal move
if board.get_piece(cur_loc) is not None:
return True
return False