-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacmanAgents.py
76 lines (63 loc) · 2.89 KB
/
pacmanAgents.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# pacmanAgents.py
# ---------------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
from pacman import Directions
from game import Agent
import random
import game
import util
class LeftTurnAgent(game.Agent):
"An agent that turns left at every opportunity"
def getAction(self, state):
"""getAction(state) - Make a decsion based on the current game state
state - pacman.GameState instance
returns a valid action direction: North, East, South, West or
a Stop action when no legal actions are possible
"""
# List of directions the agent can choose from
legal = state.getLegalPacmanActions()
# Get the agent's state from the game state and find agent heading
agentState = state.getPacmanState()
heading = agentState.getDirection()
if heading == Directions.STOP:
# Pacman is stopped, assume North (true at beginning of game)
heading = Directions.NORTH
# Turn left if possible
left = Directions.LEFT[heading] # What is left based on current heading
if left in legal:
action = left
else:
# No left turn
if heading in legal:
action = heading # continue in current direction
elif Directions.RIGHT[heading] in legal:
action = Directions.RIGHT[heading] # Turn right
elif Directions.REVERSE[heading] in legal:
action = Directions.REVERSE[heading] # Turn around
else:
action = Directions.STOP # Can't move!
return action
class GreedyAgent(Agent):
def __init__(self, evalFn="scoreEvaluation"):
self.evaluationFunction = util.lookup(evalFn, globals())
assert self.evaluationFunction != None
def getAction(self, state):
# Generate candidate actions
legal = state.getLegalPacmanActions()
if Directions.STOP in legal: legal.remove(Directions.STOP)
successors = [(state.generateSuccessor(0, action), action) for action in legal]
scored = [(self.evaluationFunction(state), action) for state, action in successors]
bestScore = max(scored)[0]
bestActions = [pair[1] for pair in scored if pair[0] == bestScore]
return random.choice(bestActions)
def scoreEvaluation(state):
return state.getScore()