CS 231: Assignment #4


This week we'll start putting resources on our landscape and requiring entities to use resources in order to move about. Our entities need broccoli to survive, so each tile in the landscape will have both an entity and some stuff.

Documentation for Java 1.5 is located at: Java 1.5 SE API.

Documentation for Java 1.6 is located at: Java 1.6 SE API


Copy your Entity.java and Landscape.java from last week to a new working directory. Download a revised version of LandscapeDisplay.java.

We're going to re-organize things a bit this week because we need to add complexity to our simulation. The overall organization is going to be as shown below.

   | (has a)
   | (has a)
grid of Tile
         | (has a)
  Entity    Stuff
  | (is a)


  1. interface Agent - create an Agent interface that specifies the following methods.
      int Row();
      int Col();
      int Type();
      void SetType( int t );
      void SetPosition( int r, int c );
      String toString();
      int Stock( int cans );
      boolean Eat();
      int Cans();
      public void Draw( Graphics g, int x0, int y0, int dx, int dy, Color c );
  2. class Stuff - create a class to represent stuff that can grow on the Landscape. The stuff class should have the following methods.
    • public Stuff() - default constructor.
    • public Stuff( String t, int q ) - constructor that takes in a string (the stuff's type) and a quantity (the amount of stuff).
    • public String Name() - returns the name of the stuff.
    • public int Quantity() - returns the amount of stuff.
    • public void Grow() - increments the amount of stuff by 1 unit. You are free to decide how to represent a unit. There should be a maximum quantity of 255 units, and the amount of stuff should never go over that quantity.
    • public int Harvest() - harvests up to 10 units of stuff. If there is more than 10 units of stuff, reduce the amount of stuff by 10 and return 10. If there is less than 10 units of stuff, return whatever is left, leaving zero units of stuff.
  3. class BroccoliFiend - extends the Entity class and implements the Agent class. It needs to store how many cans of broccoli it has. In addition, implement the following methods.
    • public BroccoliFiend() - should call super() and set the number of cans to 1.
    • public BroccoliFiend(int row, int col, int type, int cans) - should call super with row, col, and type, then set the number of cans as specified.
    • public boolean Eat() - A BroccoliFiend eats 8 cans of broccoli at each meal. If the BroccoliFiend has 8 or more cans, reduce its number of cans by 8 and return true. If the BroccoliFiend has fewer than 8 cans, it starves, so return false.
    • public int Stock(int cans) - add the specified number of cans to the BroccoliFiend's store.
    • public int Cans() - return the number of cans the BroccoliFiend has left.
  4. class Tile - create a class Tile that has an Agent and Stuff. The class needs the standard accessors and constructors. The ones below are used this week.
    • public void reset() - set the agent to null and make a new Stuff object with the type "broccoli" and an initial quantity between 0 and 255.
    • public Tile() - call reset.
    • public Stuff Stuff() - return the Stuff object.
    • public Agent Entity() - return the entity object.
    • public void SetEntity(Agent e) - set the Tile's entity to e.
  5. class Landscape - the main structure in the Landscape should now be a grid of Tile objects. Each Tile will have an Agent object and a Stuff object. You'll need to modify the following methods. Overall, you need to replace all Entity objects with Agent objects.
    • public Landscape(int nRows, int nCols) - create a 2D array of Tile objects and allocate a Tile object for each location (i, j).
    • public void reset() - goes through the grid and calls the reset method of each Tile.
    • public void Clear() - goes through the grid of Tile objects and uses the SetEntity method to set each Tile's entity to null.
    • public Agent GetEntity() - modified to use the GetEntity() function to access the Agent object of the tile.
    • public Agent PutEntity(int r, int c, Agent e) - modified to use the Entity() and SetEntity() methods of the Tile object.
    • public Agent RemoveEntity(int r, int c ) - modified to use Entity and SetEntity() methods of the Tile object.
    • public Tile GetTile( int r, int c ) - return the Tile object at location (r, c). The method should do bounds checking and return null if r or c is out of bounds.
  6. class BroccoliSim - we're going to ditch the old Simulation and start over. The BroccoliSim will need to hold both a Landscape and a LandscapeDisplay. The class needs the following methods. You can use the BroccoliSim.java skeleton file, if you wish. It has an implementation of the Iterate function.
    • public BroccoliSim( int rows, int cols, int gridsize ) - create a new Landscape and LandscapeDispaly using the given arguments.
    • public void Initialize( double percentFilled ) - calls the Landscape reset method, then populates the landscape with entities. There should be a percentFilled chance of a BroccoliFiend being created on each grid location.
    • public void Update(int pause) throws InterruptedExecption - calls the update method of the LandscapeDisplay and then sleeps for the given time (ms).
    • public void Iterate() - executes one iteration of the simulation. Use the following algorithm.
      For each row
        For each column
         Get the tile t
         Get the agent e
         if e is null
           tell the Stuff in the tile to Grow
           stick the agent in an array
      While the array size is not zero
        remove a random agent from the array
        check its neighborhood to see what square has the most stuff
        if it's not the Agent's current location
          if the square is unoccupied
            Remove the agent from its current location
            If the agent can Eat
              Put the agent in its new square
    • public static void main( String args[]) throws InterruptedException - the main function should create a BroccoliSim object, call Initialize, then loop for some number of iterations (like 500), called Iterate and Update within the loop. That's it.
  7. Run the BroccoliSim main function. Have each iteration print out the number of entities left. What happens as the food runs out? Experiment with different conditions.


  1. Experiment with different resource usage patterns.
  2. Try modifying the usage patterns based on whether other entities are nearby.
  3. Try adding in rules for creating new entities. As is, entities only die.
  4. Combine the social simulation with the broccoli simulation so that both sets of rules are in play.
  5. Use command line arguments to control some of the simulation parameters (number of rows and columns, for example).


Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in lab.

Your writeup should have a simple format.

Once you have written up your assignment, give the page the label:


You can give any page a label when you're editing it using the label field at the bottom of the page.

Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, attach it to an email and send it to the prof.