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
- If at time t a gubble is alone in a cell and has not obstruction to its front, it goes forward one step.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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 withrows
rows andcols
columns of cells, each of which is visually displayed as a square whose width and height areside
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 pointul
when the grid is displayed in an animation.
Instance Methods
public void addGubble (Gubble g);
Adds the gubbleg
to the initial configuration of the cellular automaton represented by this gubbleworld. Complains if there is already a wall at the same position asg
.
public boolean isGubbleAt (Point p);
Returnstrue
if there is a gubble in the cell with addressp
in the current configuration; otherwise returnsfalse
. As inBuggleWorld
, 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 pointp
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);
Returnstrue
if there is a wall in the cell with addressp
in the current configuration; otherwise returnsfalse
. As inBuggleWorld
, 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 theGubble
contract. The upper left corner of the grid and the size of the grid cells are determined by parameters passed to theGubbleWorld
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.