Lab 15: Bear-Fish Simulation, Inheritance Version

Purpose: to practice more with basic class design and implementation and work with inheritance.

In this part of the lab, you’ll work with basic class design and implementation to modify the bear-fish simulation.

Step 1: Setup

To get started with this lab exercise do the following things.

  1. Download the starter code.

  2. Extract the ZIP file to a folder named bear_fish (or similar) in your class OneDrive (or similar).

  3. Read the README.txt file. This is a common way of describing a program that spans over several files.

  4. Run the bearfishsimulation.py file and see that it works.

Step 2: Speed Up

The code runs really slowly! This is because screen refreshes are performed after every modification is made by a turtle. By disabling screen refreshing until all the work is done and then painting the screen. we will eliminate the delays that result from the screen furiously trying to update the screen from every turtle change. We can accomplish this by turning "tracing" off and then turning it back on after all the current updates are complete.

Do the following:

Step 3: Extract constants

Notice that the main simulation depends on several different constants, defined as local variables at the top of the mainSimulation() method.

Do this...

Step 4: Refactor to use inheritance

Open up bear.py and fish.py. Notice how much code is identical, or nearly identical, between the two classes! We can use inheritance to clean up this code.

Celebrate the removal of redundant code! But be careful not to overdo it: some of the methods are different, because, well, Bears and Fish are different.

Step 5: Adding some functionality

Let’s keep track of how many bears and fish there are in the world. Here’s one strategy:

Step 6: Tune the simulation

Once you have all this working, try changing the size of the world to be 25x25. Then, try changing the number of steps the world simulates to be 5000 or 10000.

What I see is that all the fish always die out, but the bears live on -- even after the fish are gone. Why is this? Can you fix it?

Do this…

You might want to add print statements to see every time a bear is born and when a bear dies.

A much more realistic scenario is that when a Bear is starting to starve, it does not breed. To implement this, we need to define a new constant -- VERY_HUNGRY_TICKS. Then, in tryToBreed(), we need to check if the bear’s starveTicks > VERY_HUNGRY_TICKS. If so, return immediately -- no need to check if the bear can breed.

Step 7: Add a Plant Class

You may encounter an error because turtles don’t know about Plant.gif. So:

Now, let’s make fish eat too:

Step 8 (optional): Additional clean-up

Can you find:

Submit your lab

Create a new ZIP file with your completed project code and upload it to Moodle.

If you don’t know how to do make a ZIP file, here are a few references: https://www.wikihow.com/Make-a-Zip-File, https://www.digitaltrends.com/computing/how-to-zip-a-file-in-windows-10/

We will grade this exercise according to the following criteria:

16 points total