I am about to do a project "Conway's game of life".
This codes are from an existing scenerio of "Conway's game of life" at this website. I am gonna mention the codes of World class of this scenerio (http://www.greenfoot.org/scenarios/1336)
Now I need to understand:
1. Why "saveLastState" is used and what does it do?
2. Explain the parameter here. Specially what does( i, 0, i, endY) do?
myGrid.drawLine(i, 0, i, endY);
3. Explain the parameter here. Specially what does (0, i, endX, i) do?
myGrid.drawLine(0, i, endX, i);
4. Explain the part:
Cell c = new GameOfLifeCell(myCellSize);
myCells = c;
setInitCellState(c);
addObject(c, i, j);
*Why GameOfLifeCell is used? & *what does "addObject(c, i, j);" do?
* What does setInitCellState method do?
5. Why the getRandomNumber(4) is used and what "c.setState(true);" does?
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 ); } } |