CPSC 111 2005/6 Winter Term 2, Assignment 3: Beasts
Contents: Details | Deliverables
Details
Out: Thu Mar 23 2006
Due: Fri Apr 7 2006, 5:00pm, both hard copy and electronic
handin
Value: 4% of final grade (subject to instructor discretion)
Objectives:
- to practice using arrays
- to practice creating classes that implement interfaces
- to practice using interfaces
- to read, understand, and extend an existing code base
- to add functionality to a larger program in manageable pieces
- to test your additions
Contents:
Description
Beasts are graphical objects that move around an onscreen window,
showing their mood by their color. Your job is to control their
behavior. Beasts are controlled by their brains, which set their
direction of motion and mood. The behavior of a beast can also be
influenced by the members of its pack.
Your mission is to control the beasts by implementing three
interfaces:
- The IBeastBrain interface requires you to create classes that
control how Beasts behave by choosing a mood and direction. A BeastMood is
described by intensity and grooviness. Intensity ranges from 0 to
100, and grooviness ranges from -100 (sad) to +100 (happy). A
direction is specified by an angle in degrees or radians.
For instance, a behavior could be to move in the direction of a sine
wave, with intensity depending on whether the Beast was moving
upwards or downwards. Another behavior would be moving perpendicular
to the pack leader, with high grooviness if you're on the left half
of the screen and low grooviness if you're on the right.
- The IPackFinder interface requires you to create classes that
return a single leader Beast and an array of Beasts that are members
of the pack. The behavior of a BeastBrain may depend on some
computation involving its leader and/or pack.
For instance, a pack could be all of the groovy beasts, or all the
nearby beasts, or all the beasts travelling in a certain direction
that are not groovy.
- The IBeastProvider interface requires you to make a class that
provides an array of Beast objects.
You are provided with example classes that implement these interfaces.
The DumbBrain class is a very simple IBeastBrain that moves in a
straight line, with no mood changes. The RandomWalkingBrain is a
slightly smarter IBeastBrain that needs to access information about
the Beast's state, as stored in the BeastInfo class. It chooses its
direction randomly, and its intensity gets lower as it ages. The
AllBeastsPack is a very simple IPackFinder that returns all created
Beasts as being pack members. The DumbSetup class is a simple
IBeastProvider class that returns an array of newly created Beasts to
the BeastSimulationProgram driver class in its setup method, so that
they can be added to the scene.
The requirements of this assignment are to create many more classes
that implement these interfaces. Specifically:
- IBeastBrain:
- SpinnyBrain directs the beast to spin around in circles, and
leaves the mood unchanged. The pack is ignored.
- FollowerBrain directs the beast towards the pack leader, and
its mood is the same grooviness and half the intensity of its
leader.
- GroupieBrain directs the beast toward the pack
leader, and its mood becomes more groovy the closer it gets,
reaching maximum grooviness when it is 100 pixels or closer to
the leader. Its intensity decreases with the number of other
Beasts in its pack, reaching 0 when there are 5 or more others.
- FlockBrain directs the beast towards the center of
its pack. Its mood becomes groovier when it is close to other
pack members, with an intensity that depends on the number of
Beasts in the pack. If there are 10 or more pack members, it
reaches maximum intensity. If there are no other Beasts in its
pack, it wanders aimlessly by picking a random direction and its
mood is neutral (intensity and grooviness both set to the middle
of the possible range).
- YourBrain is up to you! Design something interesting
and fun, and implement it. Make sure to include comments
explaining the desired behavior of your brain, so that we can
understand whether you've succeeded. Feel free to post English
descriptions of the behaviors of YourBrain to WebCT, but do
not post either code or English descriptions of how to
implement the design.
- IPackFinder:
- GlumPackFinder returns the pack of all the Beasts with
grooviness below 0. The most ungroovy Beast in the pack is the leader.
- NearbyIntensePackFinder returns the pack of all Beasts
within 200 pixels. The leader is the most intense Beast in the
pack.
- CongaPackFinder returns the pack of all beasts
provided by the IBeastProvider. However, it returns a different
pack leader for each beast: it returns the next beast in the
array (wrapping around to the 0th beast for the leader of the
last beast). If the beast passed to getLeader is not in the
array, getLeader should return null.
- YourPackFinder is up to you! Design something interesting
and fun, and implement it. Make sure to include comments
explaining the desired behavior of your pack, so that we can
understand whether you've succeeded. Feel free to post English
descriptions of the behaviors of YourPackFinder to WebCT, but do
not post either code or English descriptions of how to
implement the design.
- IBeastProvider:
- You will need to create setup classes to test your
BeastBrains and PackFinders. To call these different setup
classes, you will need to change the setup method of
BeastSimulationProgram. You must submit appropriate IBeastProviders
to test all of the code that you write. Feel free to post your
IBeastProviders on WebCT, to share ideas on what's fun to make.
- BeastManager is an example of a complex IBeastProvider that
keeps track of all the Beasts so that they can be drawn by the
main driver class, BeastSimulationProgram. Note that you do not need to
change this class at all, and your IBeastProviders are likely
to be much simpler than this one.
Beasts can, and often do, move out of the visible area of the window.
If those descriptions above sound vague, it's because they are! For
instance, how tight should the SpinnyBeast's circles be? It's up to
you. As long as your beasts look right in the way they behave, and
your documentation shows how you've fulfilled the specification,
you'll get full marks. So make sure you comment your code
carefully to justify your choices.
Be careful of what assumptions you make when writing beast brains and
pack finders. For example, remember that you may be dealing with an
empty pack containing no beasts, or that there might be no leader.
Your code should be robust and do something reasonable in these cases,
not crash! Again, document your design choices.
As usual, some bonus points are available for going above and beyond
the call of duty, by implementing more or particularly impressive
brains and packs.
Beasts Design
We have provided a framework for a working Beast package. You are
required to create all the new classes listed above, implementing
the three interfaces for brains, packs, and setup. The only code
that you will need to change in the existing framework is the setup
method in the BeastSimulationProgram, marked with "TODO".
Here's a diagram of the three interfaces, with placeholders like
"YOURBrains" for the classes that you need to write:
The full framework is:
All code that you add must be clear, easily readable, and documented
with JavaDoc. Note that it is unlikely that the comments provided for
an interface's methods are sufficient documentation for the methods of
a class that implements it, so don't just copy them!
Any significant or potentially confusing parts of methods should be
documented; code that is reused in different parts of a class should
be abstracted into a method; and methods that become overly long or
complex should be broken down into simpler methods.
Remember to explain in your comments all design decisions you made!
Beasts Download/API
Download the framework: Beasts.zip
Includes these source files (no need to download separately):
BeastSimulationProgram.java,
AllBeastsPack.java,
Beast.java,
BeastInfo.java,
BeastManager.java,
BeastMood.java,
Direction.java,
DumbBrain.java,
DumbSetup.java,
IBeastBrain.java,
IBeastProvider.java,
IPackFinder.java,
RandomWalkingBrain.java
API documentation for Beast package
:
Optional Information (not needed for this assignment): If you're
curious about how we implemented the graphical display, feel free to
browse the documentation for the ACM
packages. If you'd like to read about the design rationale for
that set of packages, see the discussion from the ACM Java Task
Force.
Setting Up Your Project
Setting up in Eclipse: Start by downloading the file
Beasts.zip to
your computer. Next, open Eclipse, and make a new Project named
Beasts. Under the File
menu
select Import...
. Select Archive file
as
the import source and browse to the Beasts.zip
file that you just downloaded. For the Into folder
,
choose Beasts and hit Finish
.
Now, your project contains all the necessary code, but the
acm.jar file is not
on your classpath. Right-click on the
Beasts
project and select
Properties
. In the menu on the left, select Java
Build Path
. Select the Libraries
tab on the
right. Click Add Jars...
. You should see
Beasts
in the dialog box that pops up.
When you double-click on it, you should see the jar file
acm.jar
. Select it and hit OK
.
You should now be ready to solve the assignment in Eclipse.
Setting up on the command line: Start by downloading
the file Beasts.zip to
whatever directory on your computer you'd like to work from. Next,
unzip the file to that directory. (The Home Suite Home CD and the lab
computers have FilZip for unzipping files; you may have another
application.)
To compile from the directory where you installed your code, use:
javac -classpath .;acm.jar *.java
To run from the directory where you installed your code, use:
java -classpath .;acm.jar BeastSimulatorProgram
Note the dot at the start of the classpath arguments! That's crucial
so Java knows to look in the current directory for classes and files.
On some computers, you may need to use :
s rather than
;
s in the commands above.
Reflection Questions
Be sure to answer these reflection questions after you
finish the assignment. There are no right answers to these questions, and we
don't expect more than a couple of sentences for each
question. However, we will grade on your effort to answer
throughtfully.
- Describe YourBrain and YourPackFinder. What nifty things can they do?
- Now that you know about inheritance, how could you have used inheritance
in this project to make your program easier to write or more powerful.
- If Java didn't have polymorphism at all, how would you have done this project?
Are interfaces and inheritance worth the complexity they add to the language?
- OPTIONAL: What was especially interesting or
frustrating about this part of the assignment? What could we have done
to make it better for you?
Note: You should include your answers to the
reflection questions in a separate text file from
your code named README.txt. Please do not submit your questions
in any format other than plain text. (Note that neither Microsoft Word
nor PDF is plain text! Whatever editor you use to write your source
code should be usable to write a plain text README.txt file.)
You should answer the reflection questions for all parts of an
assignment in the same README.txt file.
Deliverables
Last modified: 2006/04/03