Running and Debugging

Part D - critter_run.py Command Line Arguments

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.

python3 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 python3 critter_run.py in the Terminal 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 GreenSloth class, we would call critter_run.py as follows:

python3 critter_run.py --include GreenSloth

The --exclude argument is used in a similar manner, but it instead causes the simulation to load all available critters except those specified. In our running example with Sloth and GreenSloth, if we only want to test out GreenSloth, we could also call critter_run.py using:

python3 critter_run.py --exclude Sloth

This becomes more useful when you are testing more than two critters!

One or the other!

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.

python3 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.

python3 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

python3 critter_run.py --no-gui

might produce the output below. Try it yourself and see what happens with just Sloth and GreenSloth!

---------------------------------------------
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

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

will only perform the simulation for 50 steps.

Practice

Based on the explanations and examples above, run the Critter simulation with exactly eight green sloths. Include the syntax in your WARMUP.md file.

What’s Next?

During this lab, you’ll be asked to implement 6 different critter classes. Each class must have only one constructor and that constructor must accept exactly the parameter(s) described in the accompanying table. For random moves, each possible choice must be equally likely (with the exception of your DIY critters). In total, your final simulation will have the following critters: Sloth, Mouse, Tiger, Elephant, OppositeElephant, Chameleon, and your DIY critter.

Take some time to properly design your classes. Use static (non-instance) variables and imported constants.py values for colors, directions, attacks, etc. as appropriate.

Reminder

Commit and push your changes when you finish with this part of the warmup.