Browse Source

Move the setup code for each rule to their respective files.

master
Ben Kurtovic 12 years ago
parent
commit
5fbcbb123e
6 changed files with 100 additions and 63 deletions
  1. +29
    -59
      src/Goldfish.java
  2. +14
    -0
      src/rules/BriansBrain.java
  3. +20
    -1
      src/rules/Conway.java
  4. +14
    -1
      src/rules/Conway4.java
  5. +14
    -1
      src/rules/LifeWithoutDeath.java
  6. +9
    -1
      src/rules/RuleSet.java

+ 29
- 59
src/Goldfish.java View File

@@ -24,15 +24,7 @@ public class Goldfish {
} }
if (!_render.paused) { if (!_render.paused) {
_render.acquireLock(0); _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.releaseLock(0);
} }
_render.setGrid(_grid); _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) { 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) { 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) { public static void main(String[] args) {


+ 14
- 0
src/rules/BriansBrain.java View File

@@ -23,4 +23,18 @@ public class BriansBrain extends RuleSet {
} }
return newGrid; 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]);
}
}
}
} }

+ 20
- 1
src/rules/Conway.java View File

@@ -6,7 +6,7 @@ import edu.stuy.goldfish.Patch;
public class Conway extends RuleSet { public class Conway extends RuleSet {
public static int states = 2; 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); Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false);
for (int i = 0; i < g.getWidth(); i++) { for (int i = 0; i < g.getWidth(); i++) {
for (int j = 0; j < g.getHeight(); j++) { for (int j = 0; j < g.getHeight(); j++) {
@@ -24,4 +24,23 @@ public class Conway extends RuleSet {
} }
return newGrid; 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]);
}
}
}
} }

+ 14
- 1
src/rules/Conway4.java View File

@@ -7,7 +7,7 @@ import edu.stuy.goldfish.Patch;
public class Conway4 extends RuleSet { public class Conway4 extends RuleSet {
public static int states = 2; 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); Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false);
for (int i = 0; i < g.getWidth(); i++) { for (int i = 0; i < g.getWidth(); i++) {
for (int j = 0; j < g.getHeight(); j++) { for (int j = 0; j < g.getHeight(); j++) {
@@ -25,4 +25,17 @@ public class Conway4 extends RuleSet {
} }
return newGrid; 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]);
}
}
}
} }

+ 14
- 1
src/rules/LifeWithoutDeath.java View File

@@ -7,7 +7,7 @@ import edu.stuy.goldfish.Patch;
public class LifeWithoutDeath extends RuleSet { public class LifeWithoutDeath extends RuleSet {
public static int states = 2; 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); Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false);
for (int i = 0; i < g.getWidth(); i++) { for (int i = 0; i < g.getWidth(); i++) {
for (int j = 0; j < g.getHeight(); j++) { for (int j = 0; j < g.getHeight(); j++) {
@@ -21,4 +21,17 @@ public class LifeWithoutDeath extends RuleSet {
} }
return newGrid; 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]);
}
}
}
} }

+ 9
- 1
src/rules/RuleSet.java View File

@@ -12,7 +12,15 @@ public class RuleSet {
* *
* @return The new grid * @return The new grid
*/ */
public static Grid run (Grid g) {
public static Grid run(Grid g) {
return 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) {
}
} }

Loading…
Cancel
Save