Game of Life 1.5

org.bitstorm.gameoflife
Class GameOfLifeGrid

java.lang.Object
  extended byorg.bitstorm.gameoflife.GameOfLifeGrid
All Implemented Interfaces:
CellGrid

public class GameOfLifeGrid
extends java.lang.Object
implements CellGrid

Contains the cellgrid, the current shape and the Game Of Life algorithm that changes it.

Author:
Edwin Martin

Field Summary
private  int cellCols
           
private  int cellRows
           
private  java.util.Hashtable currentShape
          Contains the current, living shape.
private  int generations
           
private  Cell[][] grid
          Every cell on the grid is a Cell object.
private  java.util.Hashtable nextShape
           
private static Shape[] shapes
           
 
Constructor Summary
GameOfLifeGrid(int cellCols, int cellRows)
          Contructs a GameOfLifeGrid.
 
Method Summary
 void addNeighbour(int col, int row)
          Adds a new neighbour to a cell.
 void clear()
          Clears grid.
 boolean getCell(int col, int row)
          Get value of cell.
 java.awt.Dimension getDimension()
          Get dimension of grid.
 java.util.Enumeration getEnum()
          Get enumeration of Cell's
 int getGenerations()
          Get number of generations.
 void next()
          Create next generation of shape.
 void resize(int cellColsNew, int cellRowsNew)
          Resize grid.
 void setCell(int col, int row, boolean c)
          Set value of cell.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cellRows

private int cellRows

cellCols

private int cellCols

generations

private int generations

shapes

private static Shape[] shapes

currentShape

private java.util.Hashtable currentShape
Contains the current, living shape. It's implemented as a hashtable. Tests showed this is 70% faster than Vector.


nextShape

private java.util.Hashtable nextShape

grid

private Cell[][] grid
Every cell on the grid is a Cell object. This object can become quite large.

Constructor Detail

GameOfLifeGrid

public GameOfLifeGrid(int cellCols,
                      int cellRows)
Contructs a GameOfLifeGrid.

Parameters:
cellCols - number of columns
cellRows - number of rows
Method Detail

clear

public void clear()
Clears grid.

Specified by:
clear in interface CellGrid

next

public void next()
Create next generation of shape.


addNeighbour

public void addNeighbour(int col,
                         int row)
Adds a new neighbour to a cell.

Parameters:
col - Cell-column
row - Cell-row

getEnum

public java.util.Enumeration getEnum()
Get enumeration of Cell's

Specified by:
getEnum in interface CellGrid
Returns:
Enumerator over Cell.
See Also:
CellGrid.getEnum()

getCell

public boolean getCell(int col,
                       int row)
Get value of cell.

Specified by:
getCell in interface CellGrid
Parameters:
col - x-coordinate of cell
row - y-coordinate of cell
Returns:
value of cell

setCell

public void setCell(int col,
                    int row,
                    boolean c)
Set value of cell.

Specified by:
setCell in interface CellGrid
Parameters:
col - x-coordinate of cell
row - y-coordinate of cell
c - value of cell

getGenerations

public int getGenerations()
Get number of generations.

Returns:
number of generations

getDimension

public java.awt.Dimension getDimension()
Get dimension of grid.

Specified by:
getDimension in interface CellGrid
Returns:
dimension of grid

resize

public void resize(int cellColsNew,
                   int cellRowsNew)
Resize grid. Reuse existing cells.

Specified by:
resize in interface CellGrid
Parameters:
cellColsNew - new number of columns.
cellRowsNew - new number of rows.
See Also:
CellGrid.resize(int, int)

Game of Life 1.5

This API descibes the Game of Life program, written by Edwin Martin. The source code is available online too.

Copyright 1996-2004 Edwin Martin <edwin@bitstorm.org>.