forked from ci-group/revolve
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.py
149 lines (132 loc) · 4.19 KB
/
grid.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class Grid:
# Current position of last drawn element
x_pos = 0
y_pos = 0
# Orientation of robot
orientation = 1
# Direction of last movement
previous_move = -1
# Coordinates and orientation of movements
movement_stack = [[0,0,1]]
# Coordinates of visited positions
visited_coordinates = []
def get_position(self):
"""Return current position on x and y axis"""
return [Grid.x_pos, Grid.y_pos]
def set_position(self, x, y):
"""Set position of x and y axis"""
Grid.x_pos = x
Grid.y_pos = y
def set_orientation(self, orientation):
"""Set new orientation on grid"""
if orientation in [0, 1, 2, 3]:
Grid.orientation = orientation
else:
return False
def calculate_orientation(self):
"""Set orientation by previous move and orientation"""
if (Grid.previous_move == -1 or
(Grid.previous_move == 1 and Grid.orientation == 1) or
(Grid.previous_move == 2 and Grid.orientation == 3) or
(Grid.previous_move == 3 and Grid.orientation == 2) or
(Grid.previous_move == 0 and Grid.orientation == 0)):
self.set_orientation(1)
elif ((Grid.previous_move == 2 and Grid.orientation == 1) or
(Grid.previous_move == 0 and Grid.orientation == 3) or
(Grid.previous_move == 1 and Grid.orientation == 2) or
(Grid.previous_move == 3 and Grid.orientation == 0)):
self.set_orientation(2)
elif ((Grid.previous_move == 0 and Grid.orientation == 1) or
(Grid.previous_move == 3 and Grid.orientation == 3) or
(Grid.previous_move == 2 and Grid.orientation == 2) or
(Grid.previous_move == 1 and Grid.orientation == 0)):
self.set_orientation(0)
elif ((Grid.previous_move == 3 and Grid.orientation == 1) or
(Grid.previous_move == 1 and Grid.orientation == 3) or
(Grid.previous_move == 0 and Grid.orientation == 2) or
(Grid.previous_move == 2 and Grid.orientation == 0)):
self.set_orientation(3)
def move_by_slot(self, slot):
"""Move in direction by slot id"""
if slot == 0:
self.move_down()
elif slot == 1:
self.move_up()
elif slot == 2:
self.move_right()
elif slot == 3:
self.move_left()
def move_right(self):
"""Set position one to the right in correct orientation"""
if Grid.orientation == 1:
Grid.x_pos += 1
elif Grid.orientation == 2:
Grid.y_pos += 1
elif Grid.orientation == 0:
Grid.x_pos -= 1
elif Grid.orientation == 3:
Grid.y_pos -= 1
Grid.previous_move = 2
def move_left(self):
"""Set position one to the left"""
if Grid.orientation == 1:
Grid.x_pos -= 1
elif Grid.orientation == 2:
Grid.y_pos -= 1
elif Grid.orientation == 0:
Grid.x_pos += 1
elif Grid.orientation == 3:
Grid.y_pos += 1
Grid.previous_move = 3
def move_up(self):
"""Set position one upwards"""
if Grid.orientation == 1:
Grid.y_pos -= 1
elif Grid.orientation == 2:
Grid.x_pos += 1
elif Grid.orientation == 0:
Grid.y_pos += 1
elif Grid.orientation == 3:
Grid.x_pos -= 1
Grid.previous_move = 1
def move_down(self):
"""Set position one downwards"""
if Grid.orientation == 1:
Grid.y_pos += 1
elif Grid.orientation == 2:
Grid.x_pos -= 1
elif Grid.orientation == 0:
Grid.y_pos -= 1
elif Grid.orientation == 3:
Grid.x_pos += 1
Grid.previous_move = 0
def move_back(self):
if len(Grid.movement_stack) > 1:
Grid.movement_stack.pop()
last_movement = Grid.movement_stack[-1]
Grid.x_pos = last_movement[0]
Grid.y_pos = last_movement[1]
Grid.orientation = last_movement[2]
def add_to_visited(self):
"""Add current position to visited coordinates list"""
self.calculate_orientation()
Grid.visited_coordinates.append([Grid.x_pos, Grid.y_pos])
Grid.movement_stack.append([Grid.x_pos, Grid.y_pos, Grid.orientation])
def calculate_grid_dimensions(self):
min_x = 0
max_x = 0
min_y = 0
max_y = 0
for coorinate in Grid.visited_coordinates:
min_x = coorinate[0] if coorinate[0] < min_x else min_x
max_x = coorinate[0] if coorinate[0] > max_x else max_x
min_y = coorinate[1] if coorinate[1] < min_y else min_y
max_y = coorinate[1] if coorinate[1] > max_y else max_y
return [min_x, max_x, min_y, max_y]
def reset_grid(self):
Grid.x_pos = 0
Grid.y_pos = 0
Grid.orientation = 1
Grid.previous_move = -1
Grid.movement_stack = [[0,0,1]]
Grid.visited_coordinates = []