class DemoAgent(): def __init__(self): # Initialise globals self.robot = GRobot("demoAgent", colour="yellow") worldPath = "./../Maps/MazeExtra.map" # this must be the same as that used in RobotGridWorld.pyw (and any other agents operating together) # import world newworld = pickle.load(open(worldPath, 'rb')) self.mapsize = len(newworld) - 2 self.world = [[None] * (self.mapsize + 3) for i in range(self.mapsize + 3)] # World map # take out the buffer walls for i in range(self.mapsize): for j in range(self.mapsize): self.world[i][j] = newworld[i + 1][j + 1] # Erase hazards from memory # TODO: We will need to modify this to remove random rewards as well for i in range(0, self.mapsize): for j in range(0, self.mapsize): if self.world[i][j] == "Hazard": self.world[i][j] = None def run(self): try: while True: char = screen.getch() if char == 'q' or char == 'Q': break elif char == curses.KEY_RIGHT: result = self.robot.right() elif char == curses.KEY_LEFT: result = self.robot.left() elif char == curses.KEY_UP: result = self.robot.forward() result = self.robot.look() # pull out values from world based on result coords cells = [] for cell in result: x = cell[1] y = cell[2] cells.append(self.cellVal(x, y)) # print out values from look and from world (note they are the same) screen.erase() screen.addstr(0, 0, str(result)) screen.addstr(1, 0, str(cells)) finally: # shut down cleanly curses.nocbreak() screen.keypad(0) curses.echo() curses.endwin() exit() def cellVal(self, x, y): if x < 0 or x >= self.mapsize: return ("Wall", x, y) elif y < 0 or y >= self.mapsize: return ("Wall", x, y) else: return (self.world[x][y], x, y)
class HumanAgent(): def __init__(self): # Initialise globals self.robot = GRobot("HumanAgent", colour="yellow") self.heading = 90 #0=forward, 90 = right, 180 = down, 270 = left self.path = [] worldPath = "./../Maps/MazeExtra.map" # this must be the same as that used in RobotGridWorld.pyw (and any other agents operating together) # import world newworld = pickle.load(open(worldPath, 'rb')) self.mapsize = len(newworld) - 2 self.world = [[None] * (self.mapsize + 3) for i in range(self.mapsize + 3)] # World map # take out the buffer walls for i in range(self.mapsize): for j in range(self.mapsize): self.world[i][j] = newworld[i + 1][j + 1] # Erase hazards from memory # TODO: We will need to modify this to remove random rewards as well for i in range(0, self.mapsize): for j in range(0, self.mapsize): if self.world[i][j] == "Hazard": self.world[i][j] = None def run(self): self.plan() self.move() def plan(self): #path plan with a* G = Graph(self.mapsize, self.world) start = G.get_vertex(1 + self.mapsize * 0) print("Start: ", start.get_xy(self.mapsize)) goal = G.get_vertex(1 + self.mapsize * 15) #goal pos for MazeExtra.map print("Goal: ", goal.get_xy(self.mapsize)) t = a_star(G, start, goal) self.path = reversed(t) def move(self): for coord in self.path: (i, j) = coord (x, y) = (i, j) direction = (x - self.robot.posx, y - self.robot.posy) print((x, y), (self.robot.posx, self.robot.posy)) if direction == (1, 0): #right if self.heading == 0: self.robot.forward() else: for i in range(int(self.heading / 90)): self.robot.right() self.robot.forward() self.heading = 0 self.robot.posx += 1 elif direction == (0, 1): #up if self.heading == 90: self.robot.forward() elif self.heading > 90: for i in range(int((self.heading - 90) / 90)): self.robot.right() self.robot.forward() self.heading = 90 else: #facing right self.robot.left() self.robot.forward() self.heading = 90 self.robot.posy += 1 elif direction == (-1, 0): #left if self.heading == 180: self.robot.forward() elif self.heading < 180: for i in range(int((180 - self.heading) / 90)): self.robot.left() self.robot.forward() self.heading = 180 else: # facing down = 270 self.robot.right() self.robot.forward() self.heading = 180 self.robot.posx -= 1 elif direction == (0, -1): #down if self.heading == 270: self.robot.forward() else: for i in range(int((270 - self.heading) / 90)): self.robot.left() self.robot.forward() self.heading = 270 self.robot.posy -= 1