Hello everybody
I have got the code below for "the game of life" from greenfoot website but I'm having some problems understanding all the code, would it be possible to get some help in answering my questions? if it's ok I'll start with a couple of Qs then I can continue if anybody responds.
Any help or comment is appreciated
Here is the code for the WORLD which is the only place where the code is written & there is no actors at all:
The first two questions are:
1- why are we importing: javax.swing.JOptionPane and what does it do or mean? and in declaring the variables why are we using "final" with ALIVE, DEAD and "static" with int my, my while other are boolean?
2- is there a possible short easy to understand explanation for the act method?
Thanks in advance
import greenfoot.Greenfoot; import greenfoot.GreenfootImage; import greenfoot.MouseInfo; import greenfoot.World; import java.awt.Color; import javax.swing.JOptionPane; /** * The only class in 'Ninto's Game of Life'. * <br><i>//Made with the rules and gameplay from John Conway's 'Conway's Game of Life' </i>(C). * <br><i>//Inspiration from tkiesel's <a href="http://greenfootgallery.org/scenarios/663">'Conway's Game of Life'</a></i>. * <br><i>//Chack also <a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life">Wikipedia's entry to 'Conway's Game of Life'</a> out</i>. * <br> * <br>This game simulates the life of a lot of cells, that are "dead"(white) or "alive"(black). * <br> * <br>Rules: * <ul type=1> * <li>Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation. * <li>Any live cell with more than three live neighbours dies, as if by overcrowding. * <li>Any live cell with two or three live neighbours lives, unchanged, to the next generation. * <li>Any dead cell with exactly three live neighbours becomes a live cell. * </ul> * Controls: * <br>-space = run. * <br>-a = act once and stop. * <br>-c = kill all cells (clear). * <br>-r = randomize all cells. * * @author Ninto * @version 0.1 */ public class CellWorld extends World { /**The images symbolising alive and dead cells*/ private final GreenfootImage ALIVE, DEAD; /**The position of the mouse*/ private static int mx, my; /**State booleans*/ private boolean running, adding, removing; /**All the cells*/ private boolean[][] cells, input; /** * Constructor. */ public CellWorld() { super(48, 32, 10); if(JOptionPane.showConfirmDialog(null, "Do you want a visible grid?", "Question", JOptionPane.YES_NO_OPTION)== 0) { getBackground().setColor(Color.LIGHT_GRAY); getBackground().fill(); ALIVE = new GreenfootImage(9, 9); DEAD = new GreenfootImage(9, 9); } else { ALIVE = new GreenfootImage(10, 10); DEAD = new GreenfootImage(10, 10); } ALIVE.fill(); DEAD.setColor(Color.WHITE); DEAD.fill(); printWelcome(); update(); Greenfoot.setSpeed(100); Greenfoot.start(); } /** * Action-method. */ public void act() { if(running) { for(int y=0; y<cells[0].length; y++) for(int x=0; x<cells.length; x++) { int n = numberOfNeighbours(x, y); if(cells[x][y]) { if(n != 2 && n != 3) input[x][y]= false; } else if(n == 3) input[x][y]= true; } } for(int y=0; y<cells[0].length; y++) for(int x=0; x<cells.length; x++) cells[x][y]= input[x][y]; checkMouse(); update(); } /** * Check input stuff. */ private void checkMouse() { String key = Greenfoot.getKey(); if(key != null) { if(key.equals("space")) running = !running; if(key.equalsIgnoreCase("a")) { running = true; act(); running = false; } if(key.equalsIgnoreCase("r")) randomize(); if(key.equalsIgnoreCase("c")) clear(); } if(Greenfoot.getMouseInfo()!= null) { mx = Greenfoot.getMouseInfo().getX(); my = Greenfoot.getMouseInfo().getY(); } if(Greenfoot.mousePressed(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse.getButton()== 1) adding = true; if(mouse.getButton()== 3) removing = true; } if(Greenfoot.mouseClicked(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse.getButton()== 1) adding = false; if(mouse.getButton()== 3) removing = false; } if(adding && mx >= 0 && mx <= cells.length-1 && my >= 0 && my <= cells[0].length-1) input[mx][my]= true; if(removing && mx >= 0 && mx <= cells.length-1 && my >= 0 && my <= cells[0].length-1) input[mx][my]= false; } /** * Randomize the state of all cells. */ private void randomize() { cells = new boolean[getWidth()][getHeight()]; input = new boolean[getWidth()][getHeight()]; for(int y=0; y<cells[0].length; y++) for(int x=0; x<cells.length; x++) { cells[x][y]= Greenfoot.getRandomNumber(2)== 1; input[x][y]= cells[x][y]; } } /** * Kill all cells. */ private void clear() { cells = new boolean[getWidth()][getHeight()]; input = new boolean[getWidth()][getHeight()]; } /** * Update the background image. */ private void update() { GreenfootImage bg = getBackground(); for(int y=0; y<cells[0].length; y++) for(int x=0; x<cells.length; x++) { if(cells[x][y]) bg.drawImage(ALIVE, x*10, y*10); else bg.drawImage(DEAD, x*10, y*10); } } /** * Get the number of all alive cells around this spot. * * @param x The x-coordinate. * @param y The y-coordinate. * @return The number of all alive cells around this spot. */ private int numberOfNeighbours(int x, int y) { int r = x+1, l = x-1, d = y+1, u = y-1; if(r > cells.length-1) r = 0; if(l < 0) l = cells.length-1; if(d > cells[0].length-1) d = 0; if(u < 0) u = cells[0].length-1; int n = 0; if(cells[r][y]) n++; if(cells[l][y]) n++; if(cells[x][d]) n++; if(cells[x][u]) n++; if(cells[r][d]) n++; if(cells[r][u]) n++; if(cells[l][d]) n++; if(cells[l][u]) n++; return n; } /** * Build a cell-collection at the specified point. */ private void build(int xx, int yy, int[][] code) { int width = code[0].length, height = code.length; if(xx<0 || xx>cells.length-width || yy<0 || yy>cells[0].length-height) return; for(int y=0; y<height; y++) for(int x=0; x<width; x++) input[xx+x][yy+y]= code[y][x]== 1; } private void printWelcome() { cells = new boolean[getWidth()][getHeight()]; input = new boolean[getWidth()][getHeight()]; int[][] message = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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