Recently, I am working on a project named "Conway's game of life". Unluckily, I am not allowed to use the Greenfoot library function getNeighbours(…) in the Actor class. By the way, I found some some well made scenerios of "Conway's game of life" at this website, but there, the Greenfoot library function getNeighbours(…) is used. I even can't come up with good solution to make it without the library function getNeighbours(…). Can you please help me.
I am going to copy+paste the codes I found from this side.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * The Grid consists of the lines and cells of the CA. * * @David Brown * @03/16/2010 */ public class CellularAutomata extends World { private GreenfootImage myGrid; private Cell[][] myCells; private int myWidth, myHeight, myCellSize; /** * Constructor for objects of class Grid. Calls methods to draw the * grid lines, to create the array of cells and to place 23 * Fibonacci ants onto the grid. * */ public CellularAutomata() { super(50, 50, 10); myHeight = getHeight(); myWidth = getWidth(); myCellSize = getCellSize(); drawGrid(); createCells(); } /** * Tell each cell to save its last state in preparation for the * next time step. */ public void act() { for(int i = 0; i < myHeight; i++) for(int j = 0; j < myWidth; j++) myCells[i][j].saveLastState(); } /** * Draw the lines of the cell grid. */ private void drawGrid() { myGrid = getBackground(); myGrid.setColor(Color.red); int endY = myCellSize * myHeight; int endX = myCellSize * myWidth; for (int i = 0; i < endX; i += myCellSize) myGrid.drawLine(i, 0, i, endY); for (int i = 0; i < endY; i += myCellSize) myGrid.drawLine(0, i, endX, i); } /** * Create the array of cells and turn some of them on. */ private void createCells() { myCells = new Cell[myHeight][myWidth]; for (int i = 0; i < myHeight; i++) for (int j = 0; j < myWidth; j++) { Cell c = new GameOfLifeCell(myCellSize); myCells[i][j] = c; setInitCellState(c); addObject(c, i, j); } } /** * Overridden in subclassed automata. This default routine does nothing. */ public void setInitCellState(Cell c) { if (Greenfoot.getRandomNumber(4) == 0) c.setState(true); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.util.List; /** * These are the cells of the CA. They save their last states to allow * for parallel reevaluation of neighboring cells. * * @David Brown * @03/16/2010 */ public class Cell extends Actor { Boolean myFirstTime = true; Boolean myState, myLastState; GreenfootImage myBody; int myNumNeighbors; int myBodySize; Cell [] myNeighbors; /** * Create the cell body's image and initialize state. */ public Cell(int cellSize) { super(); myState = myLastState = false; myBody = new GreenfootImage(cellSize, cellSize); setImage(myBody); myBodySize = cellSize - 2; showState(); } /** * On first execution, find and save neighboring cell references. * After that, apply cell rule and show cell state. */ public void act() { if (myFirstTime) { List neighbors = getNeighbours(1, true, Cell.class); myNumNeighbors = neighbors.size(); myNeighbors = new Cell[myNumNeighbors]; for(int i = 0; i < myNumNeighbors; i++) myNeighbors[i] = (Cell)neighbors.get(i); myFirstTime = false; } applyRule(); showState(); } /** * Save the previous state in preparation for the next time step. */ public void saveLastState() { myLastState = myState; } /** * Set the state of the cell. */ public void setState(boolean state) { myState = state; showState(); } /** * Get the state of the cell. */ public boolean getState() { return(myState); } /** * Flip the state of the cell. */ public void flipState() { setState(!myState); } /** * Count the surrounding cells that were on in the previous time step. */ public int countNeighbors() { int sum = 0; for(int i = 0; i < myNumNeighbors; i++) if (myNeighbors[i].myLastState) sum++; return(sum); } /** * Apply cell rule for timestep. Subclasses should override this method * which does nothing. */ public void applyRule() { } /** * Draw the body of the cell based on the state. */ private void showState() { if (myState) myBody.setColor(Color.black); else myBody.setColor(Color.white); myBody.fillRect(1, 1, myBodySize, myBodySize); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GameOfLifeCell here. * * @author (your name) * @version (a version number or a date) */ public class GameOfLifeCell extends Cell { public GameOfLifeCell(int cellSize) { super(cellSize); } /** * Apply the Game of Life rules to the current cell situation. */ public void applyRule() { int numNeighborsOn = countNeighbors(); boolean state = myState; if (numNeighborsOn < 2) state = false; else if (numNeighborsOn > 3) state = false; else if (numNeighborsOn == 3) state = true; myState = state; } }