-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick.py
91 lines (77 loc) · 3.46 KB
/
brick.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
81
82
83
84
85
86
87
88
89
90
91
from typing import List
from direction import Direction
from orientation import Orientation
from pos import Pos
class Brick:
def __init__(self, pos: Pos):
self.pos = pos
def next_pos(self, direction: Direction) -> Pos:
"""
Find and return next position in the given direction.
This function does NOT check for the validity of the move.
:param direction: Direction of the move (left, right, up, down)
:return: Brick's next position/orientation in the given direction.
"""
# nextpos based on current position params.
nextpos = Pos(self.pos.x, self.pos.y, self.pos.orientation)
# Update nextpos position object params based on current orientation and the direction of next move.
# In a Pos object:
# x value always points to the left block for horizontal lying brick.
# y value always points to the upper block for vertical lying brick.
if self.pos.orientation is Orientation.STANDING:
if direction is Direction.UP:
nextpos.y -= 2
nextpos.orientation = Orientation.VERTICAL_LYING
if direction is Direction.DOWN:
nextpos.y += 1
nextpos.orientation = Orientation.VERTICAL_LYING
if direction is Direction.LEFT:
nextpos.x -= 2
nextpos.orientation = Orientation.HORIZONTAL_LYING
if direction is Direction.RIGHT:
nextpos.x += 1
nextpos.orientation = Orientation.HORIZONTAL_LYING
elif self.pos.orientation is Orientation.HORIZONTAL_LYING:
if direction is Direction.UP:
nextpos.y -= 1
if direction is Direction.DOWN:
nextpos.y += 1
if direction is Direction.LEFT:
nextpos.x -= 1
nextpos.orientation = Orientation.STANDING
if direction is Direction.RIGHT:
nextpos.x += 2
nextpos.orientation = Orientation.STANDING
elif self.pos.orientation is Orientation.VERTICAL_LYING:
if direction is Direction.UP:
nextpos.y -= 1
nextpos.orientation = Orientation.STANDING
if direction is Direction.DOWN:
nextpos.y += 2
nextpos.orientation = Orientation.STANDING
if direction is Direction.LEFT:
nextpos.x -= 1
if direction is Direction.RIGHT:
nextpos.x += 1
return nextpos
def move(self, next_pos: Pos):
"""
Move the brick to the given position.
:param next_pos: Position object.
"""
self.pos = next_pos
def get_blocks_occupied(self) -> List[List[int]]:
"""
Find the blocks occupied by the brick.
:return: A list containing 1 or more elements,
where each list element contains a block's x,y coordinates.
"""
# occupies 1 block in standing position
if self.pos.orientation is Orientation.STANDING:
return [[self.pos.x, self.pos.y]]
# occupies 2 blocks along y axis in vertical pos.
if self.pos.orientation is Orientation.VERTICAL_LYING:
return [[self.pos.x, self.pos.y], [self.pos.x, self.pos.y + 1]]
# occupies 2 blocks along x axis in horizontal pos.
if self.pos.orientation is Orientation.HORIZONTAL_LYING:
return [[self.pos.x, self.pos.y], [self.pos.x + 1, self.pos.y]]