From b94c1bf5ad7d334cd6af3a2967ed4158344e5ac0 Mon Sep 17 00:00:00 2001 From: michalnowak18 Date: Sun, 3 Apr 2022 18:35:24 +0200 Subject: [PATCH] added class constants.py with class Constants and bfs.py with class Node --- src/constants.py | 11 +++++++++ src/utils/bfs.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/constants.py create mode 100644 src/utils/bfs.py diff --git a/src/constants.py b/src/constants.py new file mode 100644 index 0000000..7f7be47 --- /dev/null +++ b/src/constants.py @@ -0,0 +1,11 @@ +class Constants: + """ Class to represent all constants for the app """ + + def __init__(self): + self.move = "move" + self.up = "up" + self.down = "down" + self.right = "right" + self.left = "left" + self.rotate_right = "rotate_right" + self.rotate_left = "rotate_left" diff --git a/src/utils/bfs.py b/src/utils/bfs.py new file mode 100644 index 0000000..87e4ec3 --- /dev/null +++ b/src/utils/bfs.py @@ -0,0 +1,64 @@ +from constants import Constants as c + +class Node: + def __init__(self, current_x, current_y, agent_direction, action=None, parent=None): + self.current_x = current_x + self.current_y = current_y + self.action = action + self.parent = parent + self.agent_direction = agent_direction + + def successor(self): + x = self.current_x + y = self.current_y + neighbours = [] + + if x < 9: # right neighbour + if self.agent_direction == c.right: + actions = c.move + elif self.agent_direction == c.left: + actions = [c.rotate_right, c.rotate_right, c.move] + elif self.agent_direction == c.up: + actions = [c.rotate_right, c.move] + elif self.agent_direction == c.down: + actions = [c.rotate_left, c.move] + + neighbours.append((actions, (x + 1, y), c.right)) + + if x > 0: # left neighbour + if self.agent_direction == c.right: + actions = [c.rotate_left, c.rotate_left, c.move] + elif self.agent_direction == c.left: + actions = c.move + elif self.agent_direction == c.up: + actions = [c.rotate_left, c.move] + elif self.agent_direction == c.down: + actions = [c.rotate_right, c.move] + + neighbours.append((actions, (x - 1, y), c.left)) + + if y > 0: # upper neighbour + if self.agent_direction == c.right: + actions = [c.rotate_left, c.move] + elif self.agent_direction == c.left: + actions = [c.rotate_right, c.move] + elif self.agent_direction == c.up: + actions = c.move + elif self.agent_direction == c.down: + actions = [c.rotate_left, c.rotate_left, c.move] + + neighbours.append((actions, (x, y - 1), c.up)) + + if y < 9: # down neighbour + if self.agent_direction == c.right: + actions = [c.rotate_right, c.move] + elif self.agent_direction == c.left: + actions = [c.rotate_left, c.move] + elif self.agent_direction == c.up: + actions = [c.rotate_left, c.rotate_left, c.move] + elif self.agent_direction == c.down: + actions = c.move + + neighbours.append((actions, (x, y + 1), c.down)) + + return neighbours