-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elephant.py
78 lines (66 loc) · 2.96 KB
/
Elephant.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
from MasterPiece import MasterPiece
class Elephant(MasterPiece):
"""
Represents an elephant piece. Inherits from MasterPiece. Will be instantiated by Game class and contained
in the Board class.
"""
def __init__(self, color: str, name: str, type: str, 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]) == 2 and abs(next_loc[1] - cur_loc[1]) == 3:
return True
# vertical move, then diagonal
elif abs(next_loc[0] - cur_loc[0]) == 3 and abs(next_loc[1] - cur_loc[1]) == 2:
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]) == 2:
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])
# check spot for occupation
if board.get_piece(cur_loc) is not None:
return True
# check diagonal spots
if cur_loc[0] < next_loc[0] and cur_loc[1] < next_loc[1]:
cur_loc = (cur_loc[0] + 1, cur_loc[1] + 1)
elif cur_loc[0] < next_loc[0] and cur_loc[1] > next_loc[1]:
cur_loc = (cur_loc[0] + 1, cur_loc[1] - 1)
elif cur_loc[0] > next_loc[0] and cur_loc[1] > next_loc[1]:
cur_loc = (cur_loc[0] - 1, cur_loc[1] - 1)
else:
cur_loc = (cur_loc[0] - 1, cur_loc[1] + 1)
# check spot for occupation
if board.get_piece(cur_loc) is not None:
return True
return False