From 08b318d1e679416ba01030efadabdf9b9127e799 Mon Sep 17 00:00:00 2001 From: Szymon Szczubkowski Date: Mon, 17 Apr 2023 16:53:54 +0200 Subject: [PATCH] purge magic numbers from graph_search, TODO: do the same in tractor.py --- graph_search.py | 21 ++++++++++----------- main.py | 4 +++- tractor.py | 16 ---------------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/graph_search.py b/graph_search.py index a703c010..622d3c86 100644 --- a/graph_search.py +++ b/graph_search.py @@ -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 diff --git a/main.py b/main.py index 61a3d487..3bdc21bc 100644 --- a/main.py +++ b/main.py @@ -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: diff --git a/tractor.py b/tractor.py index ca8545ee..f9c5f623 100644 --- a/tractor.py +++ b/tractor.py @@ -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