@@ -9,20 +9,31 @@ public class Goldfish { | |||
private Render _render; | |||
public Goldfish() { | |||
int width = 128; | |||
int height = 128; | |||
int width = 512; | |||
int height = 512; | |||
_grid = new Grid(width, height); | |||
_render = new Render(width, height, _grid, RULES); | |||
} | |||
public void run() { | |||
setup(_render.rule); | |||
int runs = 0; | |||
double average = 0; | |||
while (true) { | |||
if (_render.reset) { | |||
setup(_render.rule); | |||
_render.reset = false; | |||
} | |||
if (!_render.paused) { | |||
long start = System.currentTimeMillis(); | |||
String rule = _render.rule; | |||
if (rule.equals("Conway")) | |||
_grid = Conway.run(_grid); | |||
@@ -32,6 +43,17 @@ public class Goldfish { | |||
_grid = LifeWithoutDeath.run(_grid); | |||
else if (rule.equals("Brian's Brain")) | |||
_grid = BriansBrain.run(_grid); | |||
long diff = System.currentTimeMillis() - start; | |||
average = (diff + (average * runs)) / (runs + 1); | |||
runs++; | |||
if (runs % 30 == 0) | |||
System.out.println(runs + ": " + average); | |||
} | |||
_render.setGrid(_grid); | |||
_render.run(); | |||
@@ -70,29 +92,29 @@ public class Goldfish { | |||
} | |||
} | |||
} 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), | |||
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), | |||
} | |||
} | |||
} 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]); | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} | |||
public static void main(String[] args) { | |||
@@ -39,28 +39,66 @@ public class Patch { | |||
return "" + ((_state == 0) ? "." : _state); | |||
} | |||
public Patch[] get4Neighbors() { | |||
Patch[] neighbors = new Patch[4]; | |||
neighbors[0] = _grid.getPatch(_xcor + 1, _ycor); | |||
neighbors[1] = _grid.getPatch(_xcor - 1, _ycor); | |||
neighbors[2] = _grid.getPatch(_xcor, _ycor + 1); | |||
neighbors[3] = _grid.getPatch(_xcor, _ycor - 1); | |||
public int get4Neighbors(int state, int max) { | |||
int neighbors = 0; | |||
for (int i = 0; i < 4; i++) { | |||
Patch p; | |||
switch(i) { | |||
case 0: | |||
p = _grid.getPatch(_xcor + 1, _ycor); break; | |||
case 1: | |||
p = _grid.getPatch(_xcor - 1, _ycor); break; | |||
case 2: | |||
p = _grid.getPatch(_xcor, _ycor + 1); break; | |||
default: | |||
p = _grid.getPatch(_xcor, _ycor - 1); break; | |||
} | |||
if (p.getState() == state) | |||
neighbors++; | |||
if (neighbors == max) | |||
break; | |||
} | |||
return neighbors; | |||
} | |||
public Patch[] get8Neighbors() { | |||
Patch[] neighbors = new Patch[8]; | |||
neighbors[0] = _grid.getPatch(_xcor + 1, _ycor); | |||
neighbors[1] = _grid.getPatch(_xcor - 1, _ycor); | |||
neighbors[2] = _grid.getPatch(_xcor, _ycor + 1); | |||
neighbors[3] = _grid.getPatch(_xcor, _ycor - 1); | |||
neighbors[4] = _grid.getPatch(_xcor + 1, _ycor + 1); | |||
neighbors[5] = _grid.getPatch(_xcor + 1, _ycor - 1); | |||
neighbors[6] = _grid.getPatch(_xcor - 1, _ycor + 1); | |||
neighbors[7] = _grid.getPatch(_xcor - 1, _ycor - 1); | |||
public int get8Neighbors(int state, int max) { | |||
int neighbors = 0; | |||
for (int i = 0; i < 8; i++) { | |||
Patch p; | |||
switch(i) { | |||
case 0: | |||
p = _grid.getPatch(_xcor + 1, _ycor); break; | |||
case 1: | |||
p = _grid.getPatch(_xcor - 1, _ycor); break; | |||
case 2: | |||
p = _grid.getPatch(_xcor, _ycor + 1); break; | |||
case 3: | |||
p = _grid.getPatch(_xcor, _ycor - 1); break; | |||
case 4: | |||
p = _grid.getPatch(_xcor + 1, _ycor + 1); break; | |||
case 5: | |||
p = _grid.getPatch(_xcor + 1, _ycor - 1); break; | |||
case 6: | |||
p = _grid.getPatch(_xcor - 1, _ycor + 1); break; | |||
default: | |||
p = _grid.getPatch(_xcor - 1, _ycor - 1); break; | |||
} | |||
if (p.getState() == state) | |||
neighbors++; | |||
if (neighbors == max) | |||
break; | |||
} | |||
return neighbors; | |||
} | |||
public int get4Neighbors(int state) { | |||
return get4Neighbors(state, 4); | |||
} | |||
public int get8Neighbors(int state) { | |||
return get8Neighbors(state, 8); | |||
} | |||
public Patch clone(Grid grid) { | |||
return new Patch(grid, _xcor, _ycor, _state); | |||
} | |||
@@ -15,9 +15,7 @@ public class BriansBrain extends RuleSet { | |||
if (orig.getState() == 1) p.setState(0); // Dying cells die. | |||
else if (orig.getState() == 2) p.setState(1); // Make living cells dying. | |||
else { | |||
int numAlive = 0; | |||
for (Patch neighbor : orig.get8Neighbors()) | |||
if (neighbor.getState() == 2) numAlive++; | |||
int numAlive = orig.get8Neighbors(2, 3); | |||
if (orig.getState() == 0 && numAlive == 2) p.setState(2); | |||
} | |||
newGrid.setPatch(i,j,p); | |||
@@ -10,18 +10,14 @@ public class Conway extends RuleSet { | |||
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++) { | |||
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 < 2) { | |||
Patch orig = g.getPatch(i, j); | |||
int numAlive = orig.get8Neighbors(1, 4); | |||
Patch p = orig.clone(newGrid); | |||
if (numAlive < 2) | |||
p.setState(0); // Dies by underpopulation | |||
} | |||
if (numAlive > 3) { | |||
else if (numAlive > 3) | |||
p.setState(0); // Dies by overpopulation | |||
} | |||
if (numAlive == 3) | |||
else if (numAlive == 3) | |||
p.setState(1); // Born with 3 neighbors | |||
newGrid.setPatch(i, j, p); | |||
} | |||
@@ -11,23 +11,18 @@ public class Conway4 extends RuleSet { | |||
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++) { | |||
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(newGrid); | |||
if (numAlive < 2) { | |||
Patch orig = g.getPatch(i, j); | |||
int numAlive = orig.get4Neighbors(1); | |||
Patch p = orig.clone(newGrid); | |||
if (numAlive < 2) | |||
p.setState(0); // Dies by underpopulation | |||
} | |||
if (numAlive > 3) { | |||
else if (numAlive > 3) | |||
p.setState(0); // Dies by overpopulation | |||
} | |||
if (numAlive == 3) | |||
p.setState(1); // Born with 3 neighbors. | |||
else if (numAlive == 3) | |||
p.setState(1); // Born with 3 neighbors | |||
newGrid.setPatch(i, j, p); | |||
} | |||
} | |||
return newGrid; | |||
} | |||
} |
@@ -11,13 +11,11 @@ public class LifeWithoutDeath extends RuleSet { | |||
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++) { | |||
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); | |||
Patch orig = g.getPatch(i, j); | |||
int numAlive = orig.get8Neighbors(1, 4); | |||
Patch p = orig.clone(newGrid); | |||
if (numAlive == 3) | |||
p.setState(1); //Born with 3 neighbors. | |||
p.setState(1); // Born with 3 neighbors | |||
newGrid.setPatch(i, j, p); | |||
} | |||
} | |||