Skip to content

Instantly share code, notes, and snippets.

@dpolivaev
Created December 15, 2013 14:20
Show Gist options
  • Select an option

  • Save dpolivaev/7973576 to your computer and use it in GitHub Desktop.

Select an option

Save dpolivaev/7973576 to your computer and use it in GitHub Desktop.
GameOfLife based on a general cell distribution function, #gdcr13
package gameoflife;
public class Game {
public interface Distribution {
boolean cellExists(int x, int y);
}
final private Distribution distribution;
public Game(Distribution initialDistribution) {
this.distribution = initialDistribution;
}
public Game next() {
return new Game(nextDistribution());
}
public Distribution nextDistribution() {
return new Distribution() {
@Override
public boolean cellExists(int x, int y) {
return livingCellHasTwoNeighbours(x, y) || cellHasTreeNeighbours(x, y);
}
private boolean cellHasTreeNeighbours(int x, int y) {
return countNeighbours(distribution, x, y) == 3;
}
public boolean livingCellHasTwoNeighbours(int x, int y) {
return distribution.cellExists(x, y) && countNeighbours(distribution, x, y) == 2;
}
private int countNeighbours(Distribution distribution, int x, int y) {
return cellNumber(x+1, y+1) +
cellNumber(x+1, y) +
cellNumber(x+1, y-1) +
cellNumber(x, y+1) +
cellNumber(x, y-1) +
cellNumber(x-1, y+1) +
cellNumber(x-1, y) +
cellNumber(x-1, y-1);
}
private int cellNumber(int x, int y) {
return distribution.cellExists(x, y) ? 1 : 0;
}
};
}
public Distribution distribution() {
return distribution;
}
}
package gameoflife;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import gameoflife.Game.Distribution;
import org.junit.Test;
public class GameTest {
final int someY = 0;
final int someX = 0;
@Test
public void emptyDistribution_generatesNoCellsForx0_y0() {
Distribution emptyDistribution = new Distribution() {
@Override
public boolean cellExists(int x, int y) {
return false;
}
};
final Game game = new Game(emptyDistribution);
final Game nextBoard = game.next();
Distribution nextDistribution = nextBoard.distribution();
assertThat(false, equalTo(nextDistribution.cellExists(someX, someY)));
}
@Test
public void singlePointDistribution_generatesNoCellsForx0_y0() {
Distribution singlePointDistribution = new Distribution() {
@Override
public boolean cellExists(int x, int y) {
return someX == x && someY == y;
}
};
final Game game = new Game(singlePointDistribution);
final Game nextBoard = game.next();
Distribution nextDistribution = nextBoard.distribution();
assertThat(false, equalTo(nextDistribution.cellExists(someX, someY)));
}
@Test
public void verticalThreePointsDistribution() {
Distribution distribution = new Distribution() {
@Override
public boolean cellExists(int x, int y) {
return someX == x && (someY == y-1 || someY == y || someY == y+1);
}
};
final Game game = new Game(distribution);
final Game nextBoard = game.next();
Distribution nextDistribution = nextBoard.distribution();
assertThat(true, equalTo(nextDistribution.cellExists(someX, someY)));
}
@Test
public void angularThreePointsDistribution() {
Distribution distribution = new Distribution() {
@Override
public boolean cellExists(int x, int y) {
return someX == x && someY == y+1
|| someX == x + 1 && someY == y+1
|| someX == x + 1 && someY == y;
}
};
final Game game = new Game(distribution);
final Game nextBoard = game.next();
Distribution nextDistribution = nextBoard.distribution();
assertThat(true, equalTo(nextDistribution.cellExists(someX, someY)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment