Lab 1
Lab 1
Introduction
In this lab assignment, you will familiarise yourself with the Pac-Man World. Over the next few
assignments, you will implement your Pac-Man agent to find paths through its maze world in order
to reach a particular location and collect food efficiently. You will also build general search
algorithms and apply them to Pac-Man scenarios.
The code for the assignment consists of several Python files, some of which you will need to read
and understand to complete assignments and some of which you can ignore. The Pac-Man code
was developed by John DeNero and Dan Klein at UC Berkeley for the class CS188. The code is
written in Python 3.x, which you require an interpreter for to run the lab assignment.
1
Extract the files into a directory/folder on your computer. A folder called “search” will be created,
and you will find several dozen files. To ensure that you have a working version of the files, run
the following command:
python pacman.py
You should see a game screen pop up (see Figure 1). This is a basic Pac-Man game. In the game,
you control the movements of Pac-Man using arrow keys on your keyboard. Go ahead and try it.
The Pac-Man world is laid out as corridors (with shiny blue walls) where Pac-Man can move
about. Little white pellets are sometimes littered throughout the corridors. This is food for Pac-
Man (larger pellets are power food or capsules, try and figure out what those are for). In the world
shown in Figure 1, PacMan has adversaries: colored ghosts that eat Pac-Man when it runs into
them. Ghosts move about without eating any food. When Pac-Man is eaten, it dies and the game
ends. The screen will disappear.
Go ahead and try it. It is a simple maze with one corridor. Here is one you will use more often:
2
There are several other environments defined: mediumMaze, bigMaze, openSearch, etc. You
can also vary the scale of the screen by using the –zoom option as shown below:
All of these are single agent environments, the agent being Pac-Man. In these environments, Pan-
Man always starts at the top right corner and, at the bottom left corner is a single food pellet (see
picture above). The game ends when Pac-Man eats very last pellet (there can be pellets anywhere
in its world).
Figure 3: The Pac-Man Grid. Pac-Man is at position (5, 5). Food pellet is at (1, 1)
Actions
3
Pac-Man can only carry out the following actions:
Below, you will see how these are specified to be carried out.
Skim through the parts worth reading section of the code. Focus first on the following classes:
Agent, Directions, and Configuration.
Agent
The Agent class is very simple. It is the class you will subclass to create your Pac-Man agent. For
example, here is a very simple, and dumb, agent:
from game import Agent
from game import Directions
class DumbAgent(Agent):
"An agent that goes East until it can't"
def getAction(self, state):
"The agent always goes East"
return Directions.EAST
The way it is set up, when you specify to the game (see below) that the Pac-Man will be controlled
by an instance of a DumbAgent, the action returned by the getAction() method will be carried
out at each time step. Important things to note in the above code are:
• You should create a new file called, Agents.py, in the same directory/folder as the rest of
the code base. Enter the code above exactly as shown. Be sure to save the file.
• Every subclass of Agent (like DumbAgent) is required to implement a getAction()
method. This is the method called in each time step of the game and as mentioned above,
it should return a valid action for Pac-Man to carry out.
• Notice that we are importing the classes Agent and Directions from game.py.
4
• The getAction() method is supplied a parameter: state, which it can use to find out about
the current game state (more on this below). For now, we are ignoring it.
• Study the class Directions (defined in game.py).
The command above is specifying to run the Pac-Man game using the tinyMaze environment and
the agent is controlled by the DumbAgent. What happens?
In the Pac-Man game, if the path to the grid is blocked and Pac-Man tries to go into it, the game
crashes with an “Illegal action” exception. This is OK. After all, it is a dumb agent. We’ll fix that
next. Try the same agent in the mediumMaze. Same result, right? Good!
As in Step 4, save this version of your program in Agents.py and run it on tinyMaze, as well as
mediumMaze. Observe the behavior. Try out some of the other methods defined in GameState
to get an idea of what information is available to your agent.
5
Exercise 1: Create a new class called, RandomAgent (in the Agents.py file), which based on the
current options and pick a random action to carry out. Run your agent in the tinyMaze
environment as well as mediumMaze environment. Observe the agent’s behavior. Does it get to
the food? Always? Without crashing? Etc.
Exercise 2: Create a small environment of your own. Make sure it has walls and corridors, as well
as some food. Save it as myLayout.lay in the layouts directory.
Also, try your agent out in the openSearch environment (files are already provided in the layouts
directory). Run your agent several times and record, on average, what score you get.
Exercise 3: Create a new class called BetterRandomAgent (in the file Agent.py) so that it never
chooses ‘Stop’ as its action. Run the agent in openSearch and myLayout environments and
observe how it does.
It is important to realize that the game has several different agents (Pac-Man and the ghosts). Each
agent in the game has a unique index; Pac-Man is always index 0, with ghosts starting at index 1.
• His position
• The position of all the ghosts
• The locations of the walls
• The positions of the capsules
6
• The positions of each food pellet
• The total number of food pellets still available
• Whether it has won or lost the game
• His current score in the game
In addition, Pac-Man can also determine given the action it chooses what the next state of the
environment will be, by using the method generatePacmanSuccessor(). It is clear from the
methods available here that Pac-Man's environment is fully observable. Pac-Man's environment is
also static because until it decides what to do and takes an action, the ghosts do not move.
Exercise 4: In the file Agents.py create a new agent called ReflexAgent. This agent should look
at the possible legal actions, and if one of these actions would cause a food pellet to be eaten, it
should choose that action. If none of the immediate actions lead to food, it should choose randomly
from the possibilities (excluding 'Stop'). Test your agent in both the openSearch and myLayout
layouts.
What to submit:
Your submission should include the following: