Running and Debugging

You will be interacting with client code (i.e., code that is not your own) throughout this lab. Therefore, it’s important to get comfortable working with the existing code base before you start writing your own Critter subclasses. The Critter simulation is controlled by the critter_run.py file. This program is a bit fancier than the starter code you’ve experienced in the past. critter_run.py can accept a range of arguments from the command line. The arguments allow you to change different aspects of the simulation—aspects that might help you debug your code. Let’s explore critter_run.py now.

As with many command line tools, you can ask critter_run.py for help to learn more about what arguments it can accept. We do this using the --help flags when calling critter_run.py. Note that all of the flags we discuss below use two hyphens (–) to indicate the argument name.

python critter_run.py --help

What we’ll get back is a printout of the various options and arguments available to us.

usage: critter_run.py [-h] [--no-gui] [--width] [--height] [--iters] [-n] [-i  | -e ]

Runs the Critter simulation.

optional arguments:
  -h, --help         show this help message and exit
  --no-gui           run simulation without the GUI and print results directly to the
                     console.
  --width            width of the game board.
  --height           height of the game board.
  --iters            number of simulation iterations to perform (only used in no-gui
                     mode).
  -n , --ncritters   number of each critter to add to the simulation.
  -i , --include     if specified, the simulation is run only for the provided
                     critters. Critters should be specified by their class name. This
                     flag only accepts one class name; however, you can use the flag
                     multiple times to include different critters. Cannot be used with
                     the --exclude argument.
  -e , --exclude     if specified, the simulation is run for all available critters
                     except those provided. Critters should be specified by their class
                     name. This flag only accepts one class name; however, you can use
                     the flag multiple times to exclude different critters. Cannot be
                     used with the --include argument.

The first thing we should note is that we can always call python critter_run.py in the Shell to start up the Critter simulation with the default parameters. Perhaps the two most useful arguments are --include and --exclude. These two flags let you control which critters are used in the simulation. By default, critter_run.py will populate the simulation with all available Critter subclasses. Using the --include argument changes the behavior of the simulation to only load the specified critters. For example, if we want to test out only our Tiger and Sloth classes, we would call critter_run.py as follows:

python critter_run.py --include Tiger --include Sloth

The --exclude argument is used in a similar manner, but it instead causes the simulation to load all available critters except those specified. Let’s assume that we’ve defined Sloth, Mouse, Tiger, and Elephant. If we only want to test out Sloth, Tiger, and Elephant, we would call critter_run.py using:

python critter_run.py --exclude Mouse

It’s important to note that you cannot use the --include and --exclude arguments at the same time.

Now that we know how to control which critters are in the simulation, we should also learn how to change the number of critters of each type which get populated. By default, the simulation will always at 25 of each critter. We can change this number with the --ncritters argument. For example, the following call will run a simulation with only 2 of each critter species.

python critter_run.py --ncritters 2

This argument can be very useful for debugging because it simplifies how many critters you have to keep track of during the simulation.

Other Useful Arguments

If you are running the simulation on a small screen, you might want to change the width and height of the game board from the default values of 38 and 35 to something smaller. You can change these parameters with the --width and --height arguments. For example, you can get a 30 x 25 game board using the call below.

python critter_run.py --width 30 --height 25

Keep in mind that reducing the game board size means that your critters have fewer places to move. We recommend sticking with dimensions above 20.

Lastly, there are two useful arguments for running the simulation without having to deal with the graphical user interface (GUI): --no-gui and --iters. The --no-gui flag tells critter_run.py to execute the simulation without showing the GUI. The simulation is run for 1000 iterations and the results are printed on the screen. For example, the call

python critter_run.py --no-gui

might produce the output below.

---------------------------------------------
Critter               Wins  Alive   Total
---------------------------------------------
Tiger                   47      0      47
Sloth                   12     10      22
Mouse                   22      0      22
Elephant                21      0      21
Chameleon               15      5      20
OppositeElephant        18      0      18

You can change the number of iterations using the --iters argument. For example, the call

python critter_run.py --no-gui --iters 50

will only perform the simulation for 50 steps.