/* * LifeGrid.java - Java class for the simulation grid in the jlife applet. * Utilizes the LifeCell class. * * Author : Jeff Mercer * Written: 7/23/99 * Updated: 8/10/99 * JDK Ver: 1.1.8 * * Purpose: A relatively complex class for the grid that makes up the universe * in a Life simulation. This provides the essential methods for * managing the grid contents and the simulation itself. The LifeCell * class is used for the contents of the simulation grid. * * Copyright 1999 by Jeff C. Mercer. All rights reserved. * This program may be freely distributed and used for non-profit purposes. * For commercial or business usage, please contact the author. */ /************************************ * Import necessary external classes. ************************************/ import java.awt.*; import LifeCell; public class LifeGrid { /********************************** * Data member for LifeGrid class. **********************************/ final int DEAD=0; // Constant for dead state value. final int ALIVE=1; // Constant for living state value. final int MIN_GRIDSIZE=4; // Minimum size for the grid. final int MAX_GRIDSIZE=512; // Maximum size for the grid. final int DEFAULT_GRIDSIZE=64; // Default size for the grid. private long popCount; // Population counter. private long genCount; // Generation counter. private int gridSize; // Size of grid (for both dimensions). private LifeCell gridCell[][]; // Two-dimensional array of cells. /*********************************** * Constructors for LifeGrid class. ***********************************/ // For when no parameter given. public LifeGrid() { // Generic indexer variables. int x, y; // Set size of grid to class default. gridSize=DEFAULT_GRIDSIZE; // Create space for two-dimensional array (grid) of cell objects. This // just reserves the space, the objects do not exist yet! gridCell=new LifeCell[gridSize][gridSize]; // Go through grid array and create each element's object. for (x=0; xMAX_GRIDSIZE) gridSize=DEFAULT_GRIDSIZE; // If not, set grid size to class default. else gridSize=sz; // Otherwise, set grid size to given value. // Create space for two-dimensional array (grid) of cell objects. This // just reserves the space, the objects do not exist yet! gridCell=new LifeCell[gridSize][gridSize]; // Step through grid array... for (x=0; xgridSize-1) // ...wrap around to other side. cx=0; // If the Y-axis co-ordinate is too low... if (cy<0) // ...wrap around to other side. cy=gridSize-1; else // If the Y-axis co-ordinate is too high... if (cy>gridSize-1) // ...wrap around to other side. cy=0; // Using corrected co-ordinate values, add the current cell's state // to the neighbor count. count+=gridCell[cx][cy].giveState(); } // Subtract out the state of the target cell. We never count ourselves! count-=gridCell[cellX][cellY].giveState(); // Return the final neighbor count. return count; } /****************************************************************************/ // Determine the fate for a given cell, based on given state and neighbor // count values. private int ponderFate(int state, int nCount) { // If cell is alive and has enough neighbors... if (state==ALIVE && (nCount==2 || nCount==3)) return ALIVE; // ...it survives. else // Otherwise, if cell is dead and has enough neighbors... if (state==DEAD && nCount==3) return ALIVE; // ...it's reborn. // All other cases, cell dies. return DEAD; } /****************************************************************************/ // Kill ever single cell in the grid, wiping everything out. private void nukeGrid() { // Generic indexer variables. int x, y; // Sweep through entire grid array... for (x=0; x