com.holub.life
Class Neighborhood

java.lang.Object
  extended by com.holub.life.Neighborhood
All Implemented Interfaces:
Cell

public final class Neighborhood
extends Object
implements Cell

A group of Cell objects. Cells are grouped into neighborhoods to make board updates more efficient. When a neighborhood is quiescent (none of the Cells it contains are active), then it ignores any requests to update itself.

History

11-29-04 Alexy Marinichev fixed the disapearing-glider problem by clearing the active edges in transistion() rather then figureNextState(). The original call is commented out and the new line is marked with "(1)"


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.holub.life.Cell
Cell.Memento
 
Field Summary
 
Fields inherited from interface com.holub.life.Cell
DUMMY, LOAD, STORE
 
Constructor Summary
Neighborhood(int gridSize, Cell prototype)
          Create a new Neigborhood containing gridSize-by-gridSize clones of the prototype.
 
Method Summary
 void clear()
          Set the cell and all subcells into a "dead" state.
 Cell create()
          The "clone" method used to create copies of the current neighborhood.
 Storable createMemento()
          This method is used by container of the outermost cell.
 Cell edge(int row, int column)
          Return the edge cell in the indicated row and column.
 boolean figureNextState(Cell north, Cell south, Cell east, Cell west, Cell northeast, Cell northwest, Cell southeast, Cell southwest)
          Figures the next state of the current neighborhood and the contained neigborhoods (or cells).
 boolean isAlive()
          Return true if this cell or any subcells are alive.
 Direction isDisruptiveTo()
          Shows the direction of the cells along the edge of the block that will change state in the next transition.
 void redraw(Graphics g, Rectangle here, boolean drawAll)
          Redraw the current neighborhood only if necessary (something changed in the last transition).
 boolean transfer(Storable memento, Point corner, boolean load)
          Cause subcells to add an annotation to the indicated memento if they happen to be alive.
 boolean transition()
          Transition the neighborhood to the previously-computed state.
 void userClicked(Point here, Rectangle surface)
          Notification of a mouse click.
 int widthInCells()
          Return the specified width plus the current cell's width
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Neighborhood

public Neighborhood(int gridSize,
                    Cell prototype)
Create a new Neigborhood containing gridSize-by-gridSize clones of the prototype. The Protype is deliberately not put into the grid, so you can reuse it if you like.

Method Detail

create

public Cell create()
The "clone" method used to create copies of the current neighborhood. This method is called from the containing neighborhood's constructor. (The current neighborhood is passed into the containing-neighborhood constructor as the "prototype" argument.

Specified by:
create in interface Cell

isDisruptiveTo

public Direction isDisruptiveTo()
Shows the direction of the cells along the edge of the block that will change state in the next transition. For example, if the upper-left corner has changed, then the current Cell is disruptive in the NORTH, WEST, and NORTHWEST directions. If this is the case, the neigboring cells may need to be updated, even if they were previously stable.

Specified by:
isDisruptiveTo in interface Cell
Returns:
A Direction object that indicates the edge or edges on which a change has occured.

figureNextState

public boolean figureNextState(Cell north,
                               Cell south,
                               Cell east,
                               Cell west,
                               Cell northeast,
                               Cell northwest,
                               Cell southeast,
                               Cell southwest)
Figures the next state of the current neighborhood and the contained neigborhoods (or cells). Does not transition to the next state, however. Note that the neighboring cells are passed in as arguments rather than being stored internally---an example of the Flyweight pattern.

Specified by:
figureNextState in interface Cell
Parameters:
north - The neighbor to our north
south - The neighbor to our south
east - The neighbor to our east
west - The neighbor to our west
northeast - The neighbor to our northeast
northwest - The neighbor to our northwest
southeast - The neighbor to our southeast
southwest - The neighbor to our southwest
Returns:
true if this neighborhood (i.e. any of it's cells) will change state in the next transition.
See Also:
transition()

transition

public boolean transition()
Transition the neighborhood to the previously-computed state.

Specified by:
transition in interface Cell
Returns:
true if the transition actually changed anything.
See Also:
figureNextState(com.holub.life.Cell, com.holub.life.Cell, com.holub.life.Cell, com.holub.life.Cell, com.holub.life.Cell, com.holub.life.Cell, com.holub.life.Cell, com.holub.life.Cell)

redraw

public void redraw(Graphics g,
                   Rectangle here,
                   boolean drawAll)
Redraw the current neighborhood only if necessary (something changed in the last transition).

Specified by:
redraw in interface Cell
Parameters:
g - Draw onto this graphics.
here - Bounding rectangle for current Neighborhood.
drawAll - force a redraw, even if nothing has changed.
See Also:
transition()

edge

public Cell edge(int row,
                 int column)
Return the edge cell in the indicated row and column.

Specified by:
edge in interface Cell
Parameters:
row - The requested row. Must be on the edge of the block.
column - The requested column. Must be on the edge of the block.
Returns:
true if the the state changed.

userClicked

public void userClicked(Point here,
                        Rectangle surface)
Notification of a mouse click. The point is relative to the upper-left corner of the surface.

Specified by:
userClicked in interface Cell
Parameters:
here - The position of the click relative to the bounding rectangle of the current Cell.

isAlive

public boolean isAlive()
Description copied from interface: Cell
Return true if this cell or any subcells are alive.

Specified by:
isAlive in interface Cell

widthInCells

public int widthInCells()
Description copied from interface: Cell
Return the specified width plus the current cell's width

Specified by:
widthInCells in interface Cell

clear

public void clear()
Description copied from interface: Cell
Set the cell and all subcells into a "dead" state.

Specified by:
clear in interface Cell

transfer

public boolean transfer(Storable memento,
                        Point corner,
                        boolean load)
Cause subcells to add an annotation to the indicated memento if they happen to be alive.

Specified by:
transfer in interface Cell
Returns:
true if this cell was modified by the transfer.

createMemento

public Storable createMemento()
Description copied from interface: Cell
This method is used by container of the outermost cell. It is not used internally. It need be implemented only by whatever class defines the outermost cell in the universe. Other cell implementions should throw an UnsupportedOperationException when this method is called.

Specified by:
createMemento in interface Cell