diff --git a/src/Goldfish.java b/src/Goldfish.java index e69a91d..20484b9 100644 --- a/src/Goldfish.java +++ b/src/Goldfish.java @@ -24,15 +24,7 @@ public class Goldfish { } if (!_render.paused) { _render.acquireLock(0); - String rule = _render.rule; - if (rule.equals("Conway")) - _grid = Conway.run(_grid); - else if (rule.equals("Conway4")) - _grid = Conway4.run(_grid); - else if (rule.equals("Life Without Death")) - _grid = LifeWithoutDeath.run(_grid); - else if (rule.equals("Brian's Brain")) - _grid = BriansBrain.run(_grid); + doLogic(_render.rule); _render.releaseLock(0); } _render.setGrid(_grid); @@ -41,60 +33,38 @@ public class Goldfish { } } + private void doLogic(String rule) { + if (rule.equals("Conway")) + _grid = Conway.run(_grid); + else if (rule.equals("Conway4")) + _grid = Conway4.run(_grid); + else if (rule.equals("Life Without Death")) + _grid = LifeWithoutDeath.run(_grid); + else if (rule.equals("Brian's Brain")) + _grid = BriansBrain.run(_grid); + } + public static int getMaxStates(String rule) { - if (rule.equals("Conway")) - return Conway.states; - else if (rule.equals("Conway4")) - return Conway4.states; - else if (rule.equals("Life Without Death")) - return LifeWithoutDeath.states; - else if (rule.equals("Brian's Brain")) - return BriansBrain.states; - return 2; + if (rule.equals("Conway")) + return Conway.states; + else if (rule.equals("Conway4")) + return Conway4.states; + else if (rule.equals("Life Without Death")) + return LifeWithoutDeath.states; + else if (rule.equals("Brian's Brain")) + return BriansBrain.states; + return 2; } private void setup(String rule) { - if (rule.equals("Conway")) { - int[][] glidergun = { - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, - {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, - {1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} - }; - for (int i = 0; i < 36; i++) { - for (int j = 0; j < 9; j++) { - _grid.getPatch(i + 2, j + 2).setState(glidergun[j][i]); - } - } - } else if (rule.equals("Life Without Death")) { - int[][] pattern = { - {1,1,1,1,0,1}, - {1,0,1,1,1,1} - }; - for (int i = 0; i < 6; i++) { - for (int j = 0; j < 2; j++) { - _grid.getPatch(i + ((_grid.getHeight() - 6) / 2), - j + ((_grid.getWidth() - 2) / 2)).setState(pattern[j][i]); - } - } - } else if (rule.equals("Brian's Brain")) { - int[][] pattern = { - {2,0,2,0,2}, - {2,0,2,0,2}, - {0,1,0,1,0} - }; - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 3; j++) { - _grid.getPatch(i + ((_grid.getHeight() - 5) / 2), - j + ((_grid.getWidth() - 3) / 2)).setState(pattern[j][i]); - } - } - } + if (rule.equals("Conway")) + Conway.setup(_grid); + else if (rule.equals("Conway4")) + Conway4.setup(_grid); + else if (rule.equals("Life Without Death")) + LifeWithoutDeath.setup(_grid); + else if (rule.equals("Brian's Brain")) + BriansBrain.setup(_grid); } public static void main(String[] args) { diff --git a/src/rules/BriansBrain.java b/src/rules/BriansBrain.java index 5370110..cb767b1 100644 --- a/src/rules/BriansBrain.java +++ b/src/rules/BriansBrain.java @@ -23,4 +23,18 @@ public class BriansBrain extends RuleSet { } return newGrid; } + + public static void setup(Grid g) { + int[][] pattern = { + {2,0,2,0,2}, + {2,0,2,0,2}, + {0,1,0,1,0} + }; + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 3; j++) { + g.getPatch(i + ((g.getHeight() - 5) / 2), + j + ((g.getWidth() - 3) / 2)).setState(pattern[j][i]); + } + } + } } diff --git a/src/rules/Conway.java b/src/rules/Conway.java index 8f87911..cef7359 100644 --- a/src/rules/Conway.java +++ b/src/rules/Conway.java @@ -6,7 +6,7 @@ import edu.stuy.goldfish.Patch; public class Conway extends RuleSet { public static int states = 2; - public static Grid run (Grid g) { + public static Grid run(Grid g) { Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false); for (int i = 0; i < g.getWidth(); i++) { for (int j = 0; j < g.getHeight(); j++) { @@ -24,4 +24,23 @@ public class Conway extends RuleSet { } return newGrid; } + + public static void setup(Grid g) { + int[][] glidergun = { + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, + {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, + {1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} + }; + for (int i = 0; i < 36; i++) { + for (int j = 0; j < 9; j++) { + g.getPatch(i + 2, j + 2).setState(glidergun[j][i]); + } + } + } } diff --git a/src/rules/Conway4.java b/src/rules/Conway4.java index 4f1d566..5e95630 100644 --- a/src/rules/Conway4.java +++ b/src/rules/Conway4.java @@ -7,7 +7,7 @@ import edu.stuy.goldfish.Patch; public class Conway4 extends RuleSet { public static int states = 2; - public static Grid run (Grid g) { + public static Grid run(Grid g) { Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false); for (int i = 0; i < g.getWidth(); i++) { for (int j = 0; j < g.getHeight(); j++) { @@ -25,4 +25,17 @@ public class Conway4 extends RuleSet { } return newGrid; } + + public static void setup(Grid g) { + int[][] pattern = { + {1,1,1,1,0,1}, + {1,0,1,1,1,1} + }; + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 2; j++) { + g.getPatch(i + ((g.getHeight() - 6) / 2), + j + ((g.getWidth() - 2) / 2)).setState(pattern[j][i]); + } + } + } } diff --git a/src/rules/LifeWithoutDeath.java b/src/rules/LifeWithoutDeath.java index c32fbbc..205ec7c 100644 --- a/src/rules/LifeWithoutDeath.java +++ b/src/rules/LifeWithoutDeath.java @@ -7,7 +7,7 @@ import edu.stuy.goldfish.Patch; public class LifeWithoutDeath extends RuleSet { public static int states = 2; - public static Grid run (Grid g) { + public static Grid run(Grid g) { Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false); for (int i = 0; i < g.getWidth(); i++) { for (int j = 0; j < g.getHeight(); j++) { @@ -21,4 +21,17 @@ public class LifeWithoutDeath extends RuleSet { } return newGrid; } + + public static void setup(Grid g) { + int[][] pattern = { + {1,1,1,1,0,1}, + {1,0,1,1,1,1} + }; + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 2; j++) { + g.getPatch(i + ((g.getHeight() - 6) / 2), + j + ((g.getWidth() - 2) / 2)).setState(pattern[j][i]); + } + } + } } diff --git a/src/rules/RuleSet.java b/src/rules/RuleSet.java index 36e39a2..6c16b41 100644 --- a/src/rules/RuleSet.java +++ b/src/rules/RuleSet.java @@ -12,7 +12,15 @@ public class RuleSet { * * @return The new grid */ - public static Grid run (Grid g) { + public static Grid run(Grid g) { return g; } + + /** + * Setup the grid with an interesting initial pattern. + * + * @param g The grid this is running on + */ + public static void setup(Grid g) { + } }