purge magic numbers from graph_search, TODO: do the same in tractor.py

This commit is contained in:
Szymon Szczubkowski 2023-04-17 16:53:54 +02:00
parent ab37f8af99
commit 08b318d1e6
3 changed files with 13 additions and 28 deletions

View File

@ -14,23 +14,22 @@ class Search:
x = state[0]
y = state[1]
angle = state[2]
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
match(angles[angle]):
match(angle):
case 'UP':
possible = [['left', x, y, 270], ['right', x, y, 90]]
if y != 0: possible.append(['move', x, y - self.cell_size, 0])
possible = [['left', x, y, 'LEFT'], ['right', x, y, 'RIGHT']]
if y != 0: possible.append(['move', x, y - self.cell_size, 'UP'])
return possible
case 'RIGHT':
possible = [['left', x, y, 0], ['right', x, y, 180]]
if x != self.cell_size*(self.cell_number-1): possible.append(['move', x + self.cell_size, y, 90])
possible = [['left', x, y, 'UP'], ['right', x, y, 'DOWN']]
if x != self.cell_size*(self.cell_number-1): possible.append(['move', x + self.cell_size, y, 'RIGHT'])
return possible
case 'DOWN':
possible = [['left', x, y, 90], ['right', x, y, 270]]
if y != self.cell_size*(self.cell_number-1): possible.append(['move', x, y + self.cell_size, 180])
possible = [['left', x, y, 'RIGHT'], ['right', x, y, 'LEFT']]
if y != self.cell_size*(self.cell_number-1): possible.append(['move', x, y + self.cell_size, 'DOWN'])
return possible
case 'LEFT':
possible = [['left', x, y, 180], ['right', x, y, 0]]
if x != 0: possible.append(['move', x - self.cell_size, y, 270])
possible = [['left', x, y, 'DOWN'], ['right', x, y, 'UP']]
if x != 0: possible.append(['move', x - self.cell_size, y, 'LEFT'])
return possible
def graphsearch(self, istate, goaltest):
@ -54,7 +53,7 @@ class Search:
# print(elem.state[0], elem.state[1], elem.state[2])
if elem.state[0] == goaltest[0] and elem.state[1] == goaltest[1]: # checks if we reached the given point
steps = []
while elem.parent != '':
while elem.parent:
steps.append([elem.action, elem.state[0], elem.state[1]]) # should return only elem.action in prod
elem = elem.parent

View File

@ -98,8 +98,10 @@ class Game:
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
print("Generated target: ",random_x, random_y)
# below line should be later moved into tractor.py
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
tractor_next_moves = graph_search_object.graphsearch(
[self.tractor.x, self.tractor.y, self.tractor.angle], [random_x, random_y])
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y])
else:
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
elif event.type == QUIT:

View File

@ -31,22 +31,6 @@ class Tractor:
def move(self, direction, cell_size, cell_number):
# if direction == 'up':
# if self.y != 0:
# self.y -= cell_size
# self.image = self.up
# if direction == 'down':
# if self.y != (cell_number-1)*cell_size:
# self.y += cell_size
# self.image = self.down
# if direction == 'left':
# if self.x != 0:
# self.x -= cell_size
# self.image = self.left
# if direction == 'right':
# if self.x != (cell_number-1)*cell_size:
# self.x += cell_size
# self.image = self.right
if direction == 'move':
if self.angle == 0 and self.y != 0:
self.y -= cell_size