-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghost_agent.py
38 lines (30 loc) · 1.14 KB
/
ghost_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import random
from constants import *
from maze_problem import *
from pathfinder import *
from queue import Queue
from pprint import pprint
class GhostAgent:
def __init__(self, location):
self.loc = location
def choose_action(self, maze, player_location):
"""
Returns an action from a set {U, D, L, R} given perception (maze)
10% of the time returns a random choice
90% of the time moves closer to Pacman
Parameters:
maze (array): array of strings representation of the maze
Returns:
str: direction to move
"""
will_chase = random.uniform(0, 1) > Constants.get_ghost_epsilon()
# Chases Pacman by plotting the next best move to close the distance using A* search
# if the given roll was greater than the ghost's epsilon chance
if will_chase:
mp = MazeProblem(maze)
pac_loc = mp.get_player_loc()
best_path = pathfind(mp, self.loc, pac_loc)
return best_path[1][0]
# Otherwise, move in a random direction
else:
return random.sample(Constants.MOVES, 1)[0]