from helpers import add, distance, get_occupant from random import random, choice class Man_Chaser: """Represents a man_chaser, coming after the man. Modelled after the beast in the Beast game""" character = "H" color = "yellow" probability_of_moving = 0.03 probability_of_random_move = 0.2 deadly = True snack=False mine = False chaser = True def __init__(self, position): self.position = position def play_turn(self, game): if self.should_move(): possible_moves = [] for position in self.get_adjacent_positions(): if game.is_empty(position) and game.on_board(position): possible_moves.append(position) if possible_moves: if self.should_move_randomly(): self.position = choice(possible_moves) else: self.position = self.choose_best_move(possible_moves, game) #chaser catching the man score penalty is covered in man.py unlike the beast game def get_adjacent_positions(self): """Returns a list of all adjacent positions, including diagonals """ positions = [] for i in [-1, 0, 1]: for j in [-1, 0, 1]: if i or j: positions.append(add(self.position, (i, j))) return positions def should_move(self): return random() < self.probability_of_moving def should_move_randomly(self): return random() < self.probability_of_random_move def choose_best_move(self, possible_moves, game): man = game.get_agent_by_name("man") move_distances = [[distance(man.position, move), move] for move in possible_moves] shortest_distance, best_move = sorted(move_distances)[0] return best_move def die(self, game): """Removes the chaser if the man runs into him, man death is described in man.py""" game.remove_agent(self)