-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.py
80 lines (67 loc) · 2.39 KB
/
wall.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
79
80
import utils
from snake_types import Loc
class Wall:
def __init__(self, location: Loc, direction: str, length: int = 3) -> None:
"""
creates new wall
"""
# location is the middle, move
self.middle: Loc = location
self.dir: str = direction
# if the length is even the middle will be the
# upper one, for example [(),(),(middle),()]
self.length = length
def change_dir(self):
"""
changes the direction of the wall
"""
self.dir = utils.opposite_direction(self.dir)
def move_wall(self, times: int = 1) -> None:
"""move the wall to the current direction"""
for _ in range(times):
self.__move_wall_one()
def __move_wall_one(self) -> None:
"""move the wall one square"""
self.middle = utils.update_location(self.middle, self.dir)
def get_wall_locations(self) -> list[Loc]:
"""return the wall locations"""
locations = [self.middle]
self.__from_middle(locations)
self.__before_middle(locations)
return locations
def __from_middle(self, locations: list):
"""
adds a part to the wall on one side
"""
half_length = self.length // 2
even = self.length % 2
from_middle = half_length-1+even
loc = self.middle
for _ in range(from_middle):
loc = utils.update_location(loc, self.dir)
locations.insert(0, loc)
def __before_middle(self, locations: list):
"""
adds a part to the wall on the other side of what was added at 'from_middle'
"""""
half_length = self.length // 2
loc = self.middle
dir = utils.opposite_direction(self.dir)
for _ in range(half_length):
loc = utils.update_location(loc, dir)
locations.append(loc)
def is_wall(self, loc: Loc) -> bool:
"""
checks if a given location is wall
returns True or False accordingly
"""
locations = self.get_wall_locations()
return loc in locations
def get_head(self) -> Loc:
"""
return the head of the wall,(based on his direction)
"""
locations = self.get_wall_locations()
if len(locations) == 0:
return None
return locations[0]