From f4c144f22c7a06cdd589ba73f0e4e32c75f78b89 Mon Sep 17 00:00:00 2001 From: Josh Hofing Date: Mon, 14 Jan 2013 10:07:26 -0500 Subject: [PATCH] Implement Conway, add states variable to ruleset. --- src/rules/Conway.java | 27 ++++++++++++++++++++++----- src/rules/RuleSet.java | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/rules/Conway.java b/src/rules/Conway.java index 22cd1b2..7b7b6a7 100644 --- a/src/rules/Conway.java +++ b/src/rules/Conway.java @@ -4,13 +4,30 @@ import edu.stuy.goldfish.Grid; import edu.stuy.goldfish.Patch; public class Conway implements RuleSet { - - public Conway () { - Patch p = new Patch(); - } + states = 2; @Override public static Grid run (Grid g) { - return g; //TODO: do stuff. + Grid newGrid = new Grid(g.getWidth(), g.getHeight()); + for (int i = 0; i < g.getWidth(); i++) { + for (int j = 0; j < j.getHeight(); j++) { + Patch[] neighbors = g.getPatch(i, j).get8Neighbors(); + int numAlive = 0; + for (Patch p : neighbors) + if (p.getState() == 1) numAlive++; + Patch p = g.getPatch(i,j).clone(); + if (numAlive < 2) { + p.setState(0); //Dies by underpopulation + } + if (numAlive > 3) { + p.setState(0); //Dies by overpopulation + } + if (numAlive == 3) + p.setState(1); //Born with 3 neighbors. + newGrid.setPatch(i,j,p); + } + } + return newGrid; } + } diff --git a/src/rules/RuleSet.java b/src/rules/RuleSet.java index df8d585..7609b30 100644 --- a/src/rules/RuleSet.java +++ b/src/rules/RuleSet.java @@ -3,6 +3,7 @@ package edu.stuy.goldfish.rules; import edu.stuy.goldfish.Grid; public interface RuleSet { + public static int states; /** * Run this ruleset on a grid, returning the result. @@ -13,5 +14,4 @@ public interface RuleSet { */ public static Grid run (Grid g); - }