Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //John Conway's Game of Life Clone - by Virgile Grob
- static ArrayList<Cell> actors = new ArrayList<Cell>();
- final int[] adjOrdx = {-1, 0, 1, 1, 1, 0, -1, -1};
- final int[] adjOrdy = {-1, -1, -1, 0, 1, 1, 1, 0};
- void setup() {
- fill(255, 100);
- rect(0,0,width,height);
- fill(0, 100);
- actors.add(new Cell(200, 200));
- actors.get(0).isAlive = true;
- actors.add(new Cell(196, 196));
- actors.get(1).isAlive = true;
- actors.add(new Cell(204, 196));
- actors.get(2).isAlive = true;
- actors.add(new Cell(200, 204));
- actors.get(3).isAlive = true;
- actors.add(new Cell(204, 200));
- actors.get(4).isAlive = true;
- frameRate(2);
- size(1200, 800);
- }
- void draw(){
- fill(255, 255);
- rect(0,0,width,height);
- fill(0, 100);
- if(actors.size() > 0){
- for(int a = 0; a<actors.size(); a++)
- rect(actors.get(a).location.x - 2, actors.get(a).location.y - 2, 4, 4);
- ArrayList<Cell> birth = willBeBorn();
- //System.out.println("There are " + birth.size() + " items in birth.");
- death();
- for(int b = 0; b<birth.size(); b++)
- actors.add(birth.get(b));
- }
- }
- class Cell{
- PVector location;
- boolean isAlive;
- public Cell(float xloc, float yloc){
- location = new PVector(xloc, yloc);
- isAlive = false;
- }
- public int getNumAdj(){
- int n = 0;
- for(int i=0; i<8; i++)
- for(int a=0; a<actors.size(); a++)
- if((actors.get(a).location.x == location.x+(adjOrdx[i]*4)) && (actors.get(a).location.y == location.y+(adjOrdy[i]*4)))
- n++;
- System.out.println(n);
- return n;
- }
- public boolean willSurvive(){
- if(getNumAdj() == 2 || getNumAdj() == 3)
- return true;
- return false;
- }
- public boolean isntAlreadyAlive(){
- for (int a = 0; a<actors.size(); a++)
- if(actors.get(a).location.x == location.x && actors.get(a).location.y == location.y)
- return false;
- return true;
- }
- public boolean isntAlreadyBorn(ArrayList<Cell> born){
- for (int b = 0; b<born.size(); b++)
- if(born.get(b).location.x == location.x && born.get(b).location.y == location.y)
- return false;
- return true;
- }
- public boolean isBorn(){
- if(getNumAdj() == 3 && isntAlreadyAlive())
- return true;
- return false;
- }
- public ArrayList<Cell> getAdjCells(){
- ArrayList<Cell> adj = new ArrayList<Cell>();
- for(int i=0; i<8; i++)
- adj.add(new Cell(location.x+(adjOrdx[i]*4), location.y+(adjOrdy[i]*4)));
- return adj;
- }
- }
- public static void death(){
- for(int a=0; a<actors.size(); a++){
- System.out.println(actors.get(a).willSurvive());
- if(!actors.get(a).willSurvive())
- actors.remove(a);
- }
- }
- public static ArrayList willBeBorn(){
- ArrayList<Cell> temp = new ArrayList<Cell>();
- for(int a = 0; a<actors.size(); a++){
- ArrayList<Cell> adjCells = actors.get(a).getAdjCells();
- for(int b = 0; b<adjCells.size(); b++)
- if(adjCells.get(b).isBorn() && adjCells.get(b).isntAlreadyBorn(temp)){
- adjCells.get(b).isAlive = true;
- temp.add(adjCells.get(b));
- }
- }
- return temp;
- }
Add Comment
Please, Sign In to add comment