From 02021b38b7f7ee48dfcfa8353cfa76b8f74a640d Mon Sep 17 00:00:00 2001 From: Josh Hofing Date: Tue, 15 Jan 2013 11:04:38 -0500 Subject: [PATCH] LifeWithoutDeath implementation and test --- src/Goldfish.java | 14 +++++++------- src/rules/LifeWithoutDeath.java | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/rules/LifeWithoutDeath.java diff --git a/src/Goldfish.java b/src/Goldfish.java index 872346c..fc89776 100644 --- a/src/Goldfish.java +++ b/src/Goldfish.java @@ -15,15 +15,15 @@ public class Goldfish { public void run () { //TODO: make it run. - _grid.getPatch(1,0).setState(2); - _grid.getPatch(2,1).setState(2); - _grid.getPatch(2,2).setState(2); - _grid.getPatch(1,2).setState(2); - _grid.getPatch(0,2).setState(2); + _grid.getPatch(1,0).setState(1); + _grid.getPatch(2,1).setState(1); + _grid.getPatch(2,2).setState(1); + _grid.getPatch(1,2).setState(1); + _grid.getPatch(0,2).setState(1); System.out.println(_grid); - for (int i = 0; i < 6; i++) { - _grid = BriansBrain.run(_grid); + for (int i = 0; i < 10; i++) { + _grid = LifeWithoutDeath.run(_grid); System.out.println("------------"); System.out.println(_grid); } diff --git a/src/rules/LifeWithoutDeath.java b/src/rules/LifeWithoutDeath.java new file mode 100644 index 0000000..afeeccc --- /dev/null +++ b/src/rules/LifeWithoutDeath.java @@ -0,0 +1,27 @@ +package edu.stuy.goldfish.rules; + +import edu.stuy.goldfish.Grid; +import edu.stuy.goldfish.Patch; + +// Conway, without dying cells +public class LifeWithoutDeath extends RuleSet { + public static int states = 2; + + public static Grid run (Grid g) { + Grid newGrid = new Grid(g.getWidth(), g.getHeight()); + for (int i = 0; i < g.getWidth(); i++) { + for (int j = 0; j < g.getHeight(); j++) { + Patch[] neighbors = g.getPatch(i, j).get8Neighbors(); + int numAlive = 0; + for (Patch neighbor : neighbors) + if (neighbor.getState() == 1) numAlive++; + Patch p = g.getPatch(i,j).clone(newGrid); + if (numAlive == 3) + p.setState(1); //Born with 3 neighbors. + newGrid.setPatch(i,j,p); + } + } + + return newGrid; + } +}