Skip to content

Instantly share code, notes, and snippets.

@PaNaVTEC PaNaVTEC/asdf
Created Oct 22, 2016

Embed
What would you like to do?
import org.scalatest._
class CellTest extends FlatSpec with Matchers {
"a cell" should "die when has less than 2 neighbours" in {
val cell = Cell(1, true)
val isAliveNextGeneration = cell.nextGeneration()
isAliveNextGeneration shouldBe false
}
"a cell" should "survive when it has 2 neighbours" in {
val cell = Cell(2, true)
val isAliveNextGeneration = cell.nextGeneration()
isAliveNextGeneration shouldBe true
}
"a cell" should "survive when it has 3 neighbours" in {
val cell = Cell(3, true)
val isAliveNextGeneration = cell.nextGeneration()
isAliveNextGeneration shouldBe true
}
"a cell" should "die if has more than 3 neighbours" in {
val cell = Cell(4, true)
val isAliveNextGeneration = cell.nextGeneration()
isAliveNextGeneration shouldBe false
}
"a dead cell" should "remain dead with 2 neighbours" in {
val cell = Cell(2, false)
val isAliveNextGeneration = cell.nextGeneration()
isAliveNextGeneration shouldBe false
}
"a dead cell" should "become alive when it has 3 neighbours" in {
val cell = Cell(3, false)
val isAliveNextGeneration = cell.nextGeneration()
isAliveNextGeneration shouldBe true
}
"a world that is empty" should "be empty after tick" in {
val initialCellList = List(Cell(0, false))
val world = World(initialCellList)
val newDeadWorld = world.tick()
newDeadWorld.cells shouldBe initialCellList
}
"a world with 1 cell" should "be dead after tick (underpopulation)" in {
val initialCellList = List(Cell(0, true))
val world = World(initialCellList)
val newDeadWorld = world.tick()
newDeadWorld.cells shouldBe List(Cell(0, false))
}
"a world with 4 cells" should "be dead after tick (underpopulation)" in {
val initialCellList = List(
Cell(1, false), Cell(1, false),
Cell(1, false), Cell(0, true)
)
val world = World(initialCellList)
val newDeadWorld = world.tick()
newDeadWorld.cells shouldBe List(
Cell(0, false), Cell(0, false),
Cell(0, false), Cell(0, false)
)
}
case class Cell(neighbours: List[Cell], isAlive: Boolean) {
def nextGeneration() = {
if (isAlive) aliveNeighbours > 1 && aliveNeighbours < 4
else aliveNeighbours == 3
}
def aliveNeighbours = neighbours.filter(_.isAlive).size
def nextAliveNeightbours() = {
0
}
}
case class World(cells: List[Cell]) {
def tick(): World = {
World(cells.map(a => Cell(a.nextAliveNeightbours(), a.nextGeneration())))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.