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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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; } } |