COMP 110 - Lab Session 14

Need Help? Add your name to the Lab 14 Help Queue! If you have a short question, you may also ask it in the CampusWire lab-sessions chatroom.

1 Overview

In this lab session you will write a couple of Critters classes to give you practice for PSA #9. Critters was originally developed in Java at the University of Washington by Stuart Reges and Marty Stepp, but USD is the first to do it in Python!

2 Important Note for macOS Users Only

Unfortunately, there is an issue in the macOS version of python you installed at the beginning of the semester. This results in some weird visual errors when running the Critters simulator.

Luckily, there is an easy fix, which you should do now (even if you aren’t the driver for this lab). Simply download and run this new Python installer. After it has installed, make sure you close VS Code and reopen it, otherwise it may not recognize that a new version of Python was installed.

3 Initial Setup

To get started, we will need to use VS Code to clone a repository with some starter code. You and your partner should do these steps on one of the classroom laptops (i.e. use pair programming).

  1. Open the Command Palette in VS Code and use the “Git: Clone” command.

  2. When prompted, enter the following for the repository URL: https://github.com/usd-cs/comp110-fa20-lab14.git

  3. Click the “Open Repository” button in the pop-up. In this repository you will find but a single Python source code file.

  4. Locate the “lab14_critters.py” file, and open it in VS Code now.

4 Critters

The starter code contains several classes that implement a graphical simulation of a 2D world with many animals moving around in it. You will write a set of classes that define the behavior of those animals. Different kinds of animals move and behave in different ways. As you write each class, you are defining those unique behaviors for each animal.

The critter world is divided into cells with integer coordinates. The world is 60 cells wide and 50 cells tall. Like images, the upper-left cell has coordinates (0,0). The figure below shows what the world looks like.

You can start the simulator now by running the lab14_critters.py file in VS Code. Do so now.

Q1: What types of critters are present in the Critters world? Note that this is different for the lab than it will be the for PSA/image shown above.

At the bottom of the Critters window you will find some “control” button.

The “Start” and “Stop” buttons work as you might expect: the start and stop the simulation. There is also a turn speed control at the bottom that you can use to control how fast each turn goes. Feel free to play around with it for a bit: it’ll be pretty boring as 2 of the 3 critters aren’t implemented yet. When you are done, make sure you stop the simulation.

Q2: Click on the “Tick” button several times. What does this button do?

4.1 Critter Behavior

Every critter has a fixed set of methods that it will use to define its specific behavior. Those methods are:

Note that a critter will go to sleep for a few turns after eating some number of times. This is indicated by a “ZZZ” appearing over the critter. Similarly, Critters will mate if they enter the same location as a Critter of the same type. This is indicated by a "<3" appearing over the critter. In both of these cases the critter cannot move and will always lose if another Critter initiates a fight with it.

4.2 Example Critter: Cow

The starter code contains an implementation of the Cow class we looked at in lecture, with a few tweaks. Find the Cow code in the lab14_critters.py file and answer the following question.

Q3: Describe the following behaviors of the Cow:

The starter code has placeholders for two other classes: Sloth and ScaredCat. We are going to work on those now.

4.3 Task #1: Implement the Sloth Class

The following describes the expected behavior of the Sloth class.

The Sloth constructor accepts a parameter representing the speed of the Sloth For example, a speed of 3 means that the sloth will move east once every 3 turns. The simulator will pick a random number between 3 and 5 for this speed.

After you are done implementing all of the methods, test it out by running the simulator again. You’ll probably want to use the “tick” button to see that each sloth is moving only once every speed turns.

IMPORTANT: Once you have finished and tested this class, get the instructor or lab assistant’s attention to demonstrate.

4.4 Task #2: Implement the ScaredCat Class

The following describes the expected behavior of the ScaredCat class.

Once again, run the simulator to test that your ScaredCat is working as expected. As before, you will likely want to use the “tick” button so you can focus on the behavior of one specific cat to make sure it’s working as expected.

IMPORTANT: Once you have finished and tested this class, get the instructor or lab assistant’s attention to demonstrate.

5 Submission

Before leaving lab, one of you should submit the following two files on Blackboard (in the “Labs” folder). Don’t worry if you didn’t get fully done with the lab: submit whatever answers you have and work on finishing up the lab later.

  1. Your answers to all the lab questions They must be submitted in PDF format.
  2. Your modified lab14_critters.py file. If you have trouble locating the lab14_critters.py file when submitting, you can right click on the file name in VS Code and select the “Reveal in Finder” (macOS) or “Reveal in File Explorer” (Windows 10).

The other person in your group won’t submit any files; instead they should submit a comment saying with whom they worked (so I can give them credit).

6 PSA Work Time / Q&A

For the rest of this lab period, you should start working on PSA #9 with your partner.