/* Judy Yang Laura Toma * * * The GameOfLife class uses a 2-D array to model a grid of cells. * This class provides a variety of seeds (acorn, beehive, beacon, * exploder, Gosper's glider gun (ggGun), small exploder (sExploder)) * from which it can generate the next generations by calling the * nextGen method. * * The nextGen method uses the calcNextGen method which calculates if * each cell is LIVE or DEAD in the next generation depending on the * states of the cells surrounding it (surrounding i.e. cells to the * right, left, top bottom and four diagonals). If a live cell has * fewer than 2 live neighbors, it dies in the next generation. If a * live cell has exactly 2 or 3 live neighbors, it lives on to the * next generation. If a live cell has more than 3 live neighbors, it * dies in the next generation. If a dead cell has exactly 3 * neighbors, it lives on to the next generation. * The nextGen method also uses the setNextGen() method which sets all * the values from the gameGrid array and sets the as the values from * the temporary nextGame array which is only used to hold the values * of the next generation of cells while still calculating the values * of the next generation from the current generation. * This class also has a change method which changes the value of the * cell to LIVE if the cell is currently DEAD or vice versa. * The get method returns the value at the location in gameGrid. * The set method sets the a value at the specified location in * nextGame to the specified value. * The getSize method returns the size of the square arrays. * THe isLive method returns whether or not a cell is alive. * The print method helps debug by printing out the values of the * cells in the array in a grid format. * The clear method clears the entire grid by resetting all the cells * to DEAD. */ public class GameOfLife { // width and length of array containing grid values private static final int GAME_SIZE = 40; // possible states of each cell private static final int LIVE = 1; private static final int DEAD = 0; // array holding the cells private int[][] grid; //creates the grid and initializes its values to DEAD public GameOfLife(){ // creates the two arrays with size GAME_SIZE grid = new int[GAME_SIZE][GAME_SIZE]; //reset all cells to dead clear(); } // resets all the cells to dead public void clear() { for (int i = 0; i < GAME_SIZE; i++){ for (int j = 0; j < GAME_SIZE; j++){ set(i,j, DEAD); } } } // sets the value at (i, j) to value a public void set(int i, int j, int a){ grid[i][j] = a; } // returns the value at (i, j) in the current game public int get(int i, int j){ return grid[i][j]; } // counts the number of live neighbors (right, left, top, // bottom, diagonals) the point (i, j) has public int numLiveNeighbors(int i, int j) { // keeps track of number of neighbors int count = 0; // loops through all the neighbors of point (i, j) for (int k = -1; k <= 1; k++){ for (int l = -1; l <= 1; l++){ if (k == 0 && l == 0) continue; if ((i+k) >= 0 && (i+k)< GAME_SIZE && (j+l) >= 0 && (j+l)< GAME_SIZE && (grid[i+k][j+l] == LIVE)) // if (i+k, j+l) is live, increase count count++; } } // return the final value of count return count; } // returns the size of the square 2-D array public int getSize() { return GAME_SIZE; } // returns if cell is LIVE public boolean isLive(int i, int j) { return grid[i][j] == LIVE; } // changes to live if cell is dead and changes to dead if cell is // live public void change(int i, int j){ if (grid[i][j] == LIVE) grid[i][j] = DEAD; else grid[i][j] = LIVE; } // prints out the values of the grid public void print() { // loops through all the values of grid and // prints them in a grid form for (int i = 0; i < GAME_SIZE; i++){ for (int j = 0; j < GAME_SIZE; j++){ System.out.print(grid[i][j] + " "); } System.out.println(); } } /* ******************************************************** */ // sets the grid to the blinker pattern public void blinker() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form a blinker. * You can locate the blinker anywhere you want in the * grid */ // FILL IN } // sets the grid to the block pattern public void block() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form a block. * You can locate the block anywhere you want in the * grid */ // FILL IN } // sets the grid to the toad pattern public void toad() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form a toad. * You can locate the toad anywhere you want in the * grid */ // FILL IN } // sets the grid to the beehive pattern public void beehive(){ /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form a beehive. You * can locate the beehive anywhere you want in the grid */ //FILL IN } // sets the grid to the beacon oscillator public void beacon() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form a beacon. You * can locate the beacon anywhere you want in the grid */ //FILL IN } // sets the grid to the acorn pattern public void acorn() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form an acorn. You * can locate the acorn anywhere you want in the grid */ //FILL IN } // sets the grid to the Gosper's Glider Gun pattern public void ggGun() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form a ggun. You * can locate the ggun anywhere you want in the grid */ // FILL IN } // sets the grid to the exploder pattern public void exploder() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form an exploder. * You can locate the exploder anywhere you want in the * grid */ //FILL IN } // sets the grid to the f-pentomino pattern public void fpentomino() { /* you first need to clear the grid by calling clear(), then * you need to set some indivuduals in the grid as LIVE. The * individuals that you set LIVE need to form an f-pentomino. * You can locate the f-pentomino anywhere you want in the * grid */ //FILL IN } // calculate the next generation public void nextGen() { // this is the grid holding the next generation of cells int[][] nextGrid; nextGrid = new int[GAME_SIZE][GAME_SIZE]; //let's assume that every individual in the next generation //starts as DEAD for (int i = 0; i < GAME_SIZE; i++){ for (int j = 0; j < GAME_SIZE; j++) { nextGrid[i][j]=DEAD; } } /* You need to set each individual in the next generation to be live or dead based on the rules. To set a cell at location (i,j) to be alive, use nextGrid[i][j]=LIVE To set a cell at location (i,j) to be dead, use nextGrid[i][j]=DEAD */ //FILL IN YOUR CODE HERE //the next generation becomes now the current generation grid = nextGrid; } } //end of class