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
Goals for today:
To work on the critters assignment, you must first download the classes that are used by the simulator. To begin:
Stone.java
and CritterMain.java
from the (unzipped) Critters folder.
Stone.java
and then compile and run CritterMain.java
. You should see a world that
has 25 "Stone" critters. You can click on
"Go", although nothing will happen with just Stone
critters in the world.
MiniMain.java
, which is a great resource for debugging odd Critter behavior! MiniMain
lets you create instances of your Critters and call their methods!
public class LabTA extends Critter { }
CritterMain
. You should see 25 new
critters that are displayed as black question marks.
LabTA
?)
As we just saw, a default LabTA
is black, represented by a ?, and doesn't move over time. This is because LabTA
inherits from Critter
, and Critter.java
tells us that we, by default:
// make my color black in Critter world public Color getColor() { return Color.BLACK; } // don't move when getMove() is called public Direction getMove() { return Direction.CENTER; } // represent me as a ? in Critter world public String toString() { return "?"; }
LabTA
class.
public Direction getMove() { return Direction.NORTH; } public Color getColor() { return Color.MAGENTA; } public String toString() { return ":-)"; }
import java.awt.*;
LabTA
to look like now? How do you think a LabTA
will move in the Critter world?LabTA
, and run CritterMain
again (click the "Go" button). Is the look/behavior of the LabTA
what you were expecting?
NORTH
-wards). If you don't, try to figure out why not/ask a TA for help!
Our LabTA critters currently have very simple behaviors that don't change. They are always magenta, displayed as :-), moving NORTH
, attacking using POUNCE
, and eating when finding food.
Now we're going to alter the LabTA
to change over time. Specifically, we want to alternate colors between magenta and white, switching every time the LabTA
moves (getMove()
is called), with the initial color (before any moves are made) being magenta.
Developing this kind of behavior is a central part of the critters homework, so reasoning through how to implement this behavior is really important. Think about what kind of information you need to change behavior over time, and recall from the last lab how to encode information into an Object.
Hint: Remember that in defining critters, you get to decide what fields to introduce!
Another hint: Think about what kind of field is suitable. What type of variable captures that we'll either want to be magenta or white?
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 a LabTA
should be displaying itself as magenta or as white. You can then switch
the boolean
value each time the LabTA
makes a move.
isWhite
to keep track of
whether the LabTA should be white, or not (in which case it should be magenta). Remember that fields should be
declared to be private
. Also recall that the default value of a boolean is false.
getMove
method to flip this boolean value each
time getMove()
is called.
getColor
method using the isWhite
boolean to change colors as expected.
LabTA
class and
running CritterMain
.
import java.awt.*; public class LabTA extends Critter { // default value = false private boolean isWhite; public Direction getMove() { isWhite = !isWhite; return Direction.NORTH; } public Color getColor() { if (isWhite) { return Color.WHITE; } else { return Color.MAGENTA; } } // methods implemented earlier ommitted }
import java.awt.*; public class LabTA extends Critter { // default value = 0 private int moves; public Direction getMove() { moves++; return Direction.NORTH; } public Color getColor() { if (moves % 2 == 1) { return Color.WHITE; } else { return Color.MAGENTA; } } // methods implemented earlier ommitted } |
There are many ways of storing information about the state of the Critter. On the last slide, we used a boolean ( Some tasks will require knowing more information about the Critter's history/state. Using an integer to solve this problem is not the best style. However, you might run into other Critters who will need something like an integer move counter to remember how many moves its made. |
Now we're going to work on the moves that the LabTA
critters
make.
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; } }
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.
CritterMain
and observing
the LabTA
critters.
Now we're going to include a bit of randomness. The current version of
the LabTA
class always moves NORTH
. We are going to change it so that it instead randomly chooses between going EAST
and WEST
.
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. Remember to import java.util.*;
to use Random
.
rand
of type Random
. Do not initialize the field where it is declared.
LabTA
s. Now add a constructor that takes no parameters, and in the constructor, initialize
the Random
object.
getMove
method so that it uses
the Random
object to randomly pick between going WEST
and going EAST
.
LabTA
, and running CritterMain
again.
import java.awt.*; import java.util.*; public class LabTA extends Critter { private boolean isWhite; private Random rand; public LabTA() { rand = new Random(); isWhite = false; // this line is not necessary, boolean default value is false } public Direction getMove() { isWhite = !isWhite; boolean goEast = rand.nextBoolean(); if (goEast) { return Direction.EAST; } else { return Direction.WEST; } } // methods implemented earlier ommitted }
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!