From 1d9e45c6313c73e0a2d79824573d96e3c0d1d9aa Mon Sep 17 00:00:00 2001 From: Jakub Danilewicz Date: Sat, 17 Apr 2021 18:03:16 +0200 Subject: [PATCH] Movemen has been moved to agent, functions are all there ready to be used. Also new functions in project_constants in Direction Class --- agent.py | 31 +++++++++++++++++++++++++---- json_generator.py | 4 +++- main.py | 22 +++----------------- project_constants.py | 7 +++++++ resources/minefields/secondmap.json | 3 ++- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/agent.py b/agent.py index d598e8e..0e2b576 100644 --- a/agent.py +++ b/agent.py @@ -13,13 +13,35 @@ class Agent: data = json.load(json_data) self.row, self.column = data['agent_starting_position'].split(",") self.position = [int(self.row), int(self.column)] - + #self.direction = const.Direction() + self.direction = 0 + + + def rotate_left (self): + self.direction = self.direction.previous() + def rotate_right(self): + self.direction = self.direction.next() + + def go(self): + if (self.direction == const.Direction.RIGHT ): + temp = go_right() + self.position[1] = temp[1] + elif (self.direction == const.Direction.LEFT ): + temp = go_left() + self.position[1] = temp[1] + elif (self.direction == const.Direction.UP): + temp = go_up() + self.position[0] = temp[0] + elif (self.direction == const.Direction.DOWN): + temp = temp = go_down() + self.position[0] = temp[0] + def go_right(self): - + return self.position[0], self.position[1] + 1 def go_left(self): - + return self.position[0], self.position[1] - 1 def go_up(self): @@ -28,4 +50,5 @@ class Agent: def go_down(self): - return self.position[0] + 1, self.position[1] \ No newline at end of file + return self.position[0] + 1, self.position[1] + \ No newline at end of file diff --git a/json_generator.py b/json_generator.py index 5779a8e..6c30485 100644 --- a/json_generator.py +++ b/json_generator.py @@ -18,9 +18,11 @@ def _get_random_attribute_values(self, attr_type, grid_dimensions): class JsonGenerator: grid = dict() - def __init__(self, agent_starting_position): + def __init__(self, agent_starting_position, agent_direction): self.agent_starting_position = agent_starting_position starting_row, starting_column = agent_starting_position + self.agent_direction = agent_direction + starting_direction = agent_direction self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column) # sets agent's starting position diff --git a/main.py b/main.py index a1febf5..ea89bde 100644 --- a/main.py +++ b/main.py @@ -64,26 +64,10 @@ def main(): running = False # Assigning all input from keyboard as variables into an array - keys = pygame.key.get_pressed() + - # Depending on what key we press, the agent will move in that direction - # DISCRETION : The only keys that are available are arrow keys - # DISCRETION : is_valid_move is a new brand function that now plays a critical role in movement of our Agent (It is NOT just the "check up" function anymore) - if keys[pygame.K_RIGHT]: - target_row, target_column = minefield.agent.go_right() - minefield.is_valid_move(target_row, target_column) - - elif keys[pygame.K_LEFT]: - target_row, target_column = minefield.agent.go_left() - minefield.is_valid_move(target_row, target_column) - - elif keys[pygame.K_UP]: - target_row, target_column = minefield.agent.go_up() - minefield.is_valid_move(target_row, target_column) - - elif keys[pygame.K_DOWN]: - target_row, target_column = minefield.agent.go_down() - minefield.is_valid_move(target_row, target_column) + + if __name__ == "__main__": diff --git a/project_constants.py b/project_constants.py index b9d80d1..ff2c173 100644 --- a/project_constants.py +++ b/project_constants.py @@ -41,6 +41,13 @@ class Direction(Enum): RIGHT = 1 DOWN = 2 LEFT = 3 + + def next(self): + v = (self.value + 1 ) % 4 + return Direction(v) + def previous (self): + v = (self.value - 1) % 4 + return Direction(v) class Action(Enum): diff --git a/resources/minefields/secondmap.json b/resources/minefields/secondmap.json index 28a19bb..5ed3b02 100644 --- a/resources/minefields/secondmap.json +++ b/resources/minefields/secondmap.json @@ -495,5 +495,6 @@ "color": "RED", "mine": null }, - "agent_starting_position": "0,0" + "agent_starting_position": "0,0" + } \ No newline at end of file