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 < width; i++) for (int j = 0; j < width; j++) board[i][j] = new Cell (); int ii, jj; for (int i = 2; i < args.length; i += 2) { ii = Integer.parseInt (args[i]); jj = Integer.parseInt (args[i + 1]); board[ii][jj].setAlive (true); } } /** * Creates an empty (all cells dead) playing board of the given width. */ static Cell[][] emptyBoard (int width) { Cell[][] result = new Cell[width][width]; for (int i = 0; i < width; i++) for (int j = 0; j < width; j++) result[i][j] = new Cell (); return result; } /** * Prints out the current board. */ static void print () { for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) System.out.print ((Cell.isAlive (i, j) ? " *" : " .")); System.out.println (""); } System.out.println (""); } /** * Sets up and plays a game of Life. */ public static void main (String args[]) { Life life = new Life (args); print (); numberOfSteps = Integer.parseInt (args[1]); int numberOfNeighbors; for (int step = 0; step < numberOfSteps; step++) { // Set up an empty board and fill it with value for next step nextBoard = emptyBoard (width); for (int i = 0; i < width; i++) for (int j = 0; j < width; j++) { numberOfNeighbors = Cell.countLiveNeighbors (i, j); if (Cell.isAlive (i, j)) { if (numberOfNeighbors == 2 || numberOfNeighbors == 3) nextBoard[i][j].setAlive (true); } else // cell is dead if (numberOfNeighbors == 3) nextBoard[i][j].setAlive (true); } // Replace the current board with the new one board = nextBoard; print (); } } }