![](huglife.png)
Overview
In this assignment, students are given a framework for simulating creatures in a 2D space. Students are given a default Creature class, and each Creature class must provide the following methods (in addition to other methods not listed below that are inherited by all Creatures):- void move(): Called by the simulator when a creature moves.
- void attack(Creature c): Called by the simulator when a creature attacks.
- Creature replicate(): Called by the simulator when a creature replicates.
- void stay(): Called by the simulator when a creature replicates.
- Color color(): Provides a color for the creature (usually used to indicate health)
- Action chooseAction(Map<Direction, Occupant> neighbors): Chooses from the above four actions based on the north, south, west, and east neighbor.
public void stay() { if (energy + 0.2 > 2) { energy = 2; } else { energy += 0.2; } }An example of what the system looks like when it is running correctly can be found at this youtube link. Beyond implentation of the creature classes above, there is a significant emphasis on writing unit tests for the creatures. In my version of the assignment, we used JUnit, though a more ad hoc testing approach would be reasonable as well. An example of such a test is given below. In this test, a carnivorous Clorus is created and then told that there is a Plip to the right, and the test ensures that the Clorus correctly wants to attack the Plip.
@Test public void testChooseAttack() { HashMapworldWithPlipToTheRight = new HashMap (); worldWithPlipToTheRight.put(Direction.TOP, new Empty()); worldWithPlipToTheRight.put(Direction.BOTTOM, new Impassible()); worldWithPlipToTheRight.put(Direction.LEFT, new Impassible()); worldWithPlipToTheRight.put(Direction.RIGHT, new Plip()); Clorus c = new Clorus(2); Action expected = new Action(Action.ActionType.ATTACK, Direction.RIGHT); Action actual = c.chooseAction(worldWithPlipToTheRight); assertEquals(expected, actual); }
Goals
My version of the assignment covers the following goals, in decreasing order of importance:- Unit testing (in my case, JUnit)
- Java packages
- Callbacks
- Debugging in the context of a large sysetm
- OOP
- Java Serialization (for saving the state of the simulation)
Materials
Specification for this assignment: LinkStarter files (Java): Link
Demo video: Link
Starter files (Python): Starter file for creatures and simulation client The Python version of this assignment was used during a Summer course in 2014. It's in a more raw state than the Java version, which has been battle tested over two semesters.
Metadata
Summary |
Students create and test creatures that compete for resources in a 2 dimensional grid-based world. |
Topics |
Unit testing, java packages, callbacks, debugging a large system, subtype polymorphism, serialization. |
Audience |
Late CS1, CS2 |
Difficulty |
Easy to moderate. This was used as a 2 hour lab (but runs over time).
|
Strengths |
|
Weaknesses |
|
Library Dependencies |
Requires Princeton's StdDraw.java file. |
Variants |
Students can create other creatures than those suggested, allowing observation of novel dynamics. Students can simply tweak parameters to attempt to push the system into greater ecological stability. Assignment can be adapted to focus on other features (e.g. inheritance, serialization) instead of its present focus as a testing assignment. Students can share creature definitions and world files, allowing for a contest for survival-of-the-fittest. |