Created
February 2, 2014 01:24
-
-
Save trikitrok/4b2e6da5cc5a18a5b03c to your computer and use it in GitHub Desktop.
Generation in Conways game of life
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game; | |
public class Generation { | |
private LivingCells livingCells; | |
private Rules rules; | |
public Generation(LivingCells cells, Rules rules) { | |
this.livingCells = cells; | |
this.rules = rules; | |
} | |
public boolean isExtinct() { | |
return livingCells.empty(); | |
} | |
public Generation produceNextGeneration() { | |
LivingCells next = new LivingCells(); | |
addSurvivors(next); | |
addNewCells(next); | |
return new Generation(next, rules); | |
} | |
private void addSurvivors(LivingCells next) { | |
for (Cell cell : livingCells) { | |
if (willSurvive(cell)) | |
next.add(cell); | |
} | |
} | |
private void addNewCells(LivingCells next) { | |
Cells notAliveNeighbors = livingCells.getNotAliveNeighbors(); | |
for (Cell cell : notAliveNeighbors) { | |
if (willBeBorn(cell)) | |
next.add(cell); | |
} | |
} | |
private boolean willSurvive(Cell cell) { | |
return rules.shouldStayAlive(livingCells | |
.getAliveNeighborsNumberFor(cell)); | |
} | |
private boolean willBeBorn(Cell cell) { | |
return rules.shouldBeBorn(livingCells | |
.getAliveNeighborsNumberFor(cell)); | |
} | |
@Override | |
public String toString() { | |
return "[" + livingCells.toString() + "]"; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Generation other = (Generation) obj; | |
if (livingCells == null) { | |
if (other.livingCells != null) | |
return false; | |
} else if (!livingCells.equals(other.livingCells)) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment