University of Washington, CSE 142

Lab 9: Critters Practice

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Basic lab instructions

Today's lab

Goals for today:

Exercise : Critters Setup

To work on the critters assignment, you must first download the classes that are used by the simulator. There are eight different standard classes that you will need access to.

Exercise : LabTA Version 1

Exercise : LabTA Version 2

Exercise : LabTA Version 3

Our LabTA critters have very simple behaviors that don't change. They always hop forward and they always display as magenta emoticons.

For this exercise, we want to alter the behavior of the LabTA critter so that it changes color over time. In particular, we want them to alternate between the colors magenta and white. Initially they should be magenta, but after they make a first move, they should change their color to white. Then after another move they should change back to magenta. Then after another move they should change back to white.

Developing this kind of behavior is a central part of the critters homework. Think about how you can implement this. In particular, think about what kind of state information you could add to the LabTA critter that would allow it to do this. Remember that in defining critters, you get to decide what fields to introduce. A field allows an object to remember information about its state.

The next slide tells you how to do it, but it would be good to try to come up with your own ideas first.

Exercise : LabTA Version 3

To allow your LabTA critters to change colors on alternate moves, they need to keep track of where they are in the cycle. For example, you could introduce a boolean field that keeps track of whether it should be displaying itself as magenta or as white. You can then switch the boolean value each time that getMove is called.

We can solve this in an even more general way by introducing a move counter:

  • Add a field to your class called count to keep track of how many moves the critter has made. Remember that fields should be declared to be private.
  • Modify the getMove method to increment this counter each time it is called.
  • Use the counter to rewrite the getColor method to return magenta when the move counter is even and to return white when it is odd.
  • Test your new version by recompiling the LabTA class and running CritterMain.

Exercise : LabTA Version 4

Now we're going to work on the moves that the LabTA critters make.

  • Open the FlyTrap class to see how it is written. Notice its getMove method:
        public Action getMove(CritterInfo info) {
            if (info.getFront() == Neighbor.OTHER) {
                return Action.INFECT;
            } else {
                return Action.LEFT;
            }
        }
    
  • Copy and paste this code to replace the current version of getMove in the LabTA class. Then modify it so that it always infects when it can and otherwise it hops if it can and otherwise it turns left. This involves adding a new branch in the if/else. Remember that the constant Neighbor.EMPTY can be used to see if a position is empty and that you move forward with the Action.HOP move.
  • Test your new version by running CritterMain and observing the LabTA critters.

Exercise : LabTA Version 5

Now we're going to include a bit of randomness. The current version of the LabTA class always turns left when it can't infect or hop. We are going to change it so that it instead randomly chooses between turning left and turning right.

To solve this problem, we will need access to a Random object. We don't want to construct one every time we call getMove, so we will instead store it as a field.

Take your time to complete each of the following steps because the practice will be very valuable. Ask a TA or your neighbor for help if you don't know how to accomplish any of the tasks:

  • Add a field called r of type Random.
  • Introduce a constructor for the class that initializes the Random object.
  • Modify the getMove method so that it uses the Random object to randomly pick between turning left and turning right if it can't infect or hop.
  • Test your new version by running CritterMain and observing the LabTA critters.

Exercise : Cat Class

Define a Critter class called Cat:

  • It should always infect if an enemy is in front of it. Otherwise it should hop if there is a critter of another species either to its left, its right, or behind it. Otherwise it should turn right.
  • Its color should alternate between green and white on successive moves.
  • It should be displayed as a "C".

To test the Cat class behavior, include 30 cats and watch what happens (turning on the debug feature will be particularly helpful)

Exercise : Dog Class

Define a Critter class called Dog:

  • It should always infect if an enemy is in front of it. Otherwise it should turn left five times and then it should hop once.
  • It should be colored pink.
  • It should display how many left turns it has made since it last tried to hop (initially 0, then 1, then 2, ..., eventually becoming 5, then going back to 0, 1, 2, ...).

To test the Dog class behavior, include 30 dogs and watch what happens (turning on the debug feature will be particularly helpful).

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!