From 8d89e4df7807a94e1067f67bec57a90b3784c7f4 Mon Sep 17 00:00:00 2001 From: Josh Hofing Date: Mon, 14 Jan 2013 10:30:00 -0500 Subject: [PATCH] Added Conway4, a Moore Neighborhood Conway. --- src/rules/Conway4.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/rules/Conway4.java diff --git a/src/rules/Conway4.java b/src/rules/Conway4.java new file mode 100644 index 0000000..bd140fe --- /dev/null +++ b/src/rules/Conway4.java @@ -0,0 +1,33 @@ +package edu.stuy.goldfish.rules; + +import edu.stuy.goldfish.Grid; +import edu.stuy.goldfish.Patch; + +// An implementation of conway using Moore Neighborhoods. +public class Conway4 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).get4Neighbors(); + 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; + } + +}