Basic rules of demo for college:
-Make living and dead cells in grid using arrays
-If a given cell has 1 or >3 neighbours, it dies
-If a dead cell has 3 neighbours, it is reborn
-Cells with 2-3 neighbours continue living
Here is my uploaded demo.
http://www.greenfoot.org/scenarios/6977
I have simplified the input so that you just click with the mouse to add cells before running it. There is a little redundant source code for randomising the initial cells, so that can be ignored.
From the world I check through a loop the number of neighbouring cells that are alive and then execute their change of state.
If anyone is willing to have a look at my source code, that would be great. The application simply isn't running as seen in real working examples: http://www.greenfoot.org/scenarios/669
Maybe my cells aren't checking their neighbours properly? I'm really not sure.
Additionally, here is the source code. I hate to be annoying and post it all, but I'm just not sure what's the problem.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class life here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Life extends World
{
Cell[] cell = new Cell[2500];
int i;
static final int width = 50;
static final int height = 50;
boolean start = false;
public Life()
{
super(50, 50, 10);
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
int random = Greenfoot.getRandomNumber(1);
if(random == 0)
{
cell[(x+(y*width))] = new Cell(0);
addObject(cell[(x+(y*width))], (x), (y));
}
else if(random ==1)
{
cell[(x+(y*width))] = new Cell(1);
addObject(cell[(x+(y*width))], (x), (y));
}
}
}
}
public void act()
{
checkKeyPress();
startSimulation();
}
private void checkKeyPress()
{
if(Greenfoot.isKeyDown("space"))
{
start = true;
}
if(Greenfoot.isKeyDown("x"))
{
start = false;
}
}
private void startSimulation()
{
if(start==true)
{
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
cell[(x+(y*width))].countAlive();
}
}
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
cell[(x+(y*width))].checkCounter();
}
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import greenfoot.MouseInfo;
import java.util.*;
/**
* Write a description of class Cell here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cell extends Actor
{
public GreenfootImage img = new GreenfootImage(10,10);
private boolean alive;
int counter = 0;
public Cell(int birth)
{
if(birth == 0)
{
setImage(img);
img.drawRect(10,10,1,1);
alive = false;
}
else if(birth == 1)
{
setImage(img);
img.fill();
alive = true;
}
}
public void act()
{
mousePressed();
}
private void mousePressed()
{
if(Greenfoot.mousePressed(this))
{
if(alive == true)
{
makeDie();
}
else if(alive == false)
{
makeBorn();
}
}
}
public void makeDie()
{
img.clear();
alive = false;
}
public void makeBorn()
{
img.fill();
alive = true;
}
public boolean getStatus(){
return alive;
}
public void countAlive()
{
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY()< getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(1,0,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(-1,0,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(1,-1,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(0,1,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(0,-1,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(-1,-1,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(1,1,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
if (getX()>0 && getX()< getWorld().getWidth()-1 && getY()>0 && getY() < getWorld().getHeight()-1)
{
Cell temp = (Cell) getOneObjectAtOffset(-1,1,Cell.class);
if(temp.getStatus() == true)
{
counter++;
}
}
}
public void checkCounter()
{
if(counter <2)
{
makeDie();
counter = 0;
}
if(counter >3)
{
makeDie();
counter = 0;
}
if(counter == 3)
{
makeBorn();
counter = 0;
}
}
}



