Movemen has been moved to agent, functions are all there ready to be used. Also new functions in project_constants in Direction Class
This commit is contained in:
parent
1efe763937
commit
1d9e45c631
23
agent.py
23
agent.py
@ -13,6 +13,28 @@ class Agent:
|
|||||||
data = json.load(json_data)
|
data = json.load(json_data)
|
||||||
self.row, self.column = data['agent_starting_position'].split(",")
|
self.row, self.column = data['agent_starting_position'].split(",")
|
||||||
self.position = [int(self.row), int(self.column)]
|
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):
|
def go_right(self):
|
||||||
|
|
||||||
@ -29,3 +51,4 @@ class Agent:
|
|||||||
def go_down(self):
|
def go_down(self):
|
||||||
|
|
||||||
return self.position[0] + 1, self.position[1]
|
return self.position[0] + 1, self.position[1]
|
||||||
|
|
@ -18,9 +18,11 @@ def _get_random_attribute_values(self, attr_type, grid_dimensions):
|
|||||||
class JsonGenerator:
|
class JsonGenerator:
|
||||||
grid = dict()
|
grid = dict()
|
||||||
|
|
||||||
def __init__(self, agent_starting_position):
|
def __init__(self, agent_starting_position, agent_direction):
|
||||||
self.agent_starting_position = agent_starting_position
|
self.agent_starting_position = agent_starting_position
|
||||||
starting_row, starting_column = 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)
|
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
|
||||||
|
|
||||||
# sets agent's starting position
|
# sets agent's starting position
|
||||||
|
16
main.py
16
main.py
@ -64,26 +64,10 @@ def main():
|
|||||||
running = False
|
running = False
|
||||||
|
|
||||||
# Assigning all input from keyboard as variables into an array
|
# 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__":
|
if __name__ == "__main__":
|
||||||
|
@ -42,6 +42,13 @@ class Direction(Enum):
|
|||||||
DOWN = 2
|
DOWN = 2
|
||||||
LEFT = 3
|
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):
|
class Action(Enum):
|
||||||
ROTATE_LEFT = 0
|
ROTATE_LEFT = 0
|
||||||
|
@ -495,5 +495,6 @@
|
|||||||
"color": "RED",
|
"color": "RED",
|
||||||
"mine": null
|
"mine": null
|
||||||
},
|
},
|
||||||
"agent_starting_position": "0,0"
|
"agent_starting_position": "0,0"
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user