Projekt_Sztuczna_Inteligencja/agent.py

53 lines
1.7 KiB
Python

import project_constants as const
import json
# Class of our agent, initialization of it
# movement functions (those defined by the 'go_' prefix are not meant to actually move our agent, they just return some
# values that are later used by another function called 'is_valid_move' (which is defined in Minefield));
class Agent:
def __init__(self, json_path):
with open(json_path) as json_data:
data = json.load(json_data)
self.row, self.column = data["agents_initial_state"]["position"].split(",")
self.position = [int(self.row), int(self.column)]
# self.direction = const.Direction()
self.direction = const.Direction(data["agents_initial_state"]["direction"])
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 = self.go_right()
self.position[1] = temp[1]
elif self.direction == const.Direction.LEFT:
temp = self.go_left()
self.position[1] = temp[1]
elif self.direction == const.Direction.UP:
temp = self.go_up()
self.position[0] = temp[0]
elif self.direction == const.Direction.DOWN:
temp = self.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):
return self.position[0] - 1, self.position[1]
def go_down(self):
return self.position[0] + 1, self.position[1]