CS111 PS10 Task 2 GubbleWorld Contract

An instance of the GubbleWorld class represents a two dimensional cellular automaton. Each configuration of the automaton is characterized by the contents of its cells. Each cell may contain either a wall or zero or more gubbles, each of which is characterized by its direction and color.

GubbleWorld is a subclass of Sprite, so an instance of GubbleWorld is intended to be included in an animation. At each invocation of updateState(), a new configuration is determined from the old configuration by the following rules. In all of the rules, the term obstruction refers to either (1) a wall or (2) another gubble. When obstructions are mention as being to the left, front, or right of the gubble, this means the immediate left, immediate front, or immediate right.


GubbleWorld Rules

  1. If at time t a gubble is alone in a cell and has not obstruction to its front, it goes forward one step.
  2. If at time t a gubble has obstructions to its front and right but not to its left, then at time t+1 it will turn left and go forward one step.
  3. If at time t a gubble has obstructions to its left and front but not to its right, then at time t+1 it will turn right and go forward one step.
  4. If at time t a gubble has an obstruction to its front but not to its left or right, then at time t+1 it splits into two gubbles of the same color, one of which turns left and goes forward one step, and the other of which turns right and goes forward one step.
  5. If at time t a gubble has obstructions to its left, front, and right, then it dies and disappears from the grid at time t+1.
  6. If at time t two or more gubbles are in the same cell, then they eat each other (gasp!) and disappear by time t+1.
  7. Walls never change or move: if at time t a cell contains a wall, then it contains a wall at time t+1; if at time t a cell contains no wall, then it contains no wall at time t+1.

Constructor Methods

public GubbleWorld (Point ul, int rows, int cols, int side);
Creates and returns a new GubbleWorld with rows rows and cols columns of cells, each of which is visually displayed as a square whose width and height are side pixels. (These dimensions include the top and left grid lines on the cell but not the bottom and right grid lines.) The upper left corner of the grid should be at point ul when the grid is displayed in an animation.

Instance Methods

public void addGubble (Gubble g);
Adds the gubble g to the initial configuration of the cellular automaton represented by this gubbleworld. Complains if there is already a wall at the same position as g.

public boolean isGubbleAt (Point p);
Returns true if there is a gubble in the cell with address p in the current configuration; otherwise returns false. As in BuggleWorld, grid cells are indexed starting at 1 (not 0), and the y axis increases upward (not downward).

public void addWall (Point p);
Adds a wall at point p to the initial configuration of the cellular automaton represented by this gubbleworld. Complains if there is a already a gubble at that position.

public boolean isWallAt (Point p);
Returns true if there is a wall in the cell with address p in the current configuration; otherwise returns false. As in BuggleWorld, grid cells are indexed starting at 1 (not 0), and the y axis increases upward (not downward).

public void drawState (Graphics g);
Draws the current configuration of this GubbleWorld. Cells are drawn as green squares that are separated with blue grid lines; walls are drawn as black squares; and gubbles are drawn as described in the Gubble contract. The upper left corner of the grid and the size of the grid cells are determined by parameters passed to the GubbleWorld constructor.

public void updateState ();
Updates the current configuration of the cellular automaton represented by this GubbleWorld to be the next configuration as determined by the GubbleWorld Rules given at the beginning of this contract.

public void resetState ();
Changes the configuration of the cellular automaton represented by this GubbleWorld back to its initial configuration.