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?
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); } }