This is version 0. It is the version I wrote originally to run as an application.
| Syntax of call | java Life size nsteps x1 y1 x2 y2 ... xn yn |
| Example call | java Life 8 10 3 4 4 5 5 3 5 4 5 5 |
/**
* A Cell is one element of a two-dimensional array in John Conway's
* "Game of Life." It is either "alive" or "dead."
* @author Dave Matuszek
*/
class Cell
{
boolean alive = false;
/**
* Constructs a Cell in a given state (alive or dead).
*/
Cell (boolean alive)
{
this.alive = alive;
}
/**
* Constructs a Cell in a "dead" state.
*/
Cell ()
{
this (false);
}
/**
* Sets this cell to be alive or dead.
*/
void setAlive (boolean alive)
{
this.alive = alive;
}
/**
* Returns the state (alive or dead) of this cell.
*/
boolean getAlive ()
{
return alive;
}
/**
* Returns the state (alive or dead) of this cell, given its
* coordinates in the array.
*/
static boolean isAlive (int i, int j)
{
return Life.board[i][j].getAlive ();
}
/**
* Count the number of living cells adjacent to this one.
* Cells outside the array bounds are considered "dead."
*/
static int countLiveNeighbors (int i, int j)
{
int limit = Life.width - 1;
int count = 0;
for (int ii = i - 1; ii <= i + 1; ii++) for (int jj="j" 1; jj <="j" + 1; jj++) { if (ii="=" i && jj="=" j) continue; if (ii < 0 || ii> limit) continue;
if (jj <0 || jj> limit) continue;
if (isAlive (ii, jj)) count++;
}
return count;
}
}
/**
* Life is the application class for John Conway's "Game of Life."
* @author Dave Matuszek
*/
public class Life
{
/** The size of the (square) playing board. */
static int width;
/** The playing board. */
static Cell[][] board;
/** A scratch area to use while computing the next board. */
static Cell[][] nextBoard;
/** Number of steps to run the game. */
static int numberOfSteps;
/**
* A constructor to set up the playing board. All cells are
* initially set to "dead," then the cells indicated on the command
* line are set to "alive".
*/
Life (String args[])
{
width = Integer.parseInt (args[0]);
board = new Cell[width][width];
for (int i = 0; i