import random from dataclasses import dataclass from maze import Cell @dataclass class Choice: score: int cell: Cell def __add__(self, other): self.score += other def __sub__(self, other): self.score -= other class Agent: def __init__(self, buffer, grid_size): self.location = Cell(0, 0) self.last_visited = Cell(0, 0) self.buffer = buffer self.grid_size = grid_size self.first_step = True self.auto_finish = 1 def reset(self): self.location = Cell(0, 0) self.first_step = True self.last_visited = Cell(0, 0) def get_x_position(self): return (self.location.x * self.grid_size) + self.buffer + self.grid_size / 1.9 def get_y_position(self): return (self.location.y * self.grid_size) + self.buffer + self.grid_size / 1.9 def move(self, maze): if self.first_step: self.first_step = False return # Random Walk # self.location = random.choice(maze.maze[self.location]) # Direct to end # _, self.location = min(sorted([(maze.distance_map[x], x) for x in maze.maze[self.location]])) # no backstep. # self.last_visited = self.location # Go to end when close. if maze.distance_map[self.location] <= self.auto_finish: _, self.location = min(sorted([(maze.distance_map[x], x) for x in maze.maze[self.location]])) else: choices = maze.maze[self.location].copy() try: choices.remove(self.last_visited) except ValueError: pass if not choices: self.last_visited = self.location self.location = maze.maze[self.last_visited][0] else: if smart_choices := set(choices).difference(maze.dead_ends): self.last_visited = self.location self.location = random.choice(list(smart_choices)) else: self.last_visited = self.location self.location = random.choice(choices)