Skip to content

Commit

Permalink
Results of the dojo
Browse files Browse the repository at this point in the history
What we managed to complete during the dojo.
  • Loading branch information
Jyrki Puttonen committed Mar 30, 2012
1 parent 1b9527d commit 6da2a92
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 146 deletions.
5 changes: 5 additions & 0 deletions src/main/scala/org/agilefinland/examples/Customer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.agilefinland.examples;

class Customer {
val complaints = 0
}
86 changes: 86 additions & 0 deletions src/main/scala/org/agilefinland/gameoflife/BoardParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.agilefinland.gameoflife
import scala.collection.mutable.ListBuffer

case class Cell(val isAlive: Boolean)

class Board(val width: Int, val height: Int, val cellList: Seq[Seq[Cell]]) {
def cellAt(x: Int, y: Int): Cell = {
cellList(y)(x)
}

def aliveCellAt(x: Int, y: Int): Int = {
if (x < 0 || y < 0 || x >= width || y >= height)
0
else if (cellList(y)(x).isAlive)
1
else
0
}

override def equals(other: Any): Boolean = {
cellList.equals(other.asInstanceOf[Board].cellList)
}

def deduce(x: Int, y: Int): Boolean = {
if (shouldDie(x, y))
false
else if (shouldBeBorn(x, y))
true
else
cellAt(x, y).isAlive
}

def nextGeneration(): Board = {
var newCellLines = ListBuffer[List[Cell]]()
for (x <- 0 until width) {
var newCellLine = ListBuffer[Cell]()
for (y <- 0 until height) {
val alive = deduce(x, y)
newCellLine += Cell(alive)
}
newCellLines += newCellLine.toList
}
new Board(width, height, newCellLines.toList)
}

def countNeighboursAt(x: Int, y: Int): Int = {
//cellList(y-1).foldLeft(0)( cell.x > x-1 && cell.y < x+1 && cell.isAlive)
// Your code is bad and you should feel bad.
aliveCellAt(x - 1, y - 1) + aliveCellAt(x, y - 1) + aliveCellAt(x + 1, y - 1) +
aliveCellAt(x - 1, y) + aliveCellAt(x + 1, y) +
aliveCellAt(x - 1, y + 1) + aliveCellAt(x, y + 1) + aliveCellAt(x + 1, y + 1)
}

def shouldDie(x: Int, y: Int): Boolean = {
if (cellAt(x, y).isAlive) {
if (countNeighboursAt(x, y) < 2) {
return true;
} else if (countNeighboursAt(x, y) > 3) {
return true;
}
}

false
}
def shouldBeBorn(x: Int, y: Int): Boolean = {
aliveCellAt(x, y) == 0 && countNeighboursAt(x, y) == 3
}

}

object BoardParser {

def parseLine(line: String): Seq[Cell] = {
line.map(c => new Cell(c equals '*'))
}

def parse(lines: List[String]): Board = {
val numberRegex = "(\\d+) (\\d+)".r
val numberRegex(xSize, ySize) = lines(0)

val list = lines.tail.map(parseLine);

new Board(xSize.toInt, ySize.toInt, list)
}

}
59 changes: 0 additions & 59 deletions src/main/scala/org/agilefinland/gameoflife/Main.scala

This file was deleted.

102 changes: 102 additions & 0 deletions src/test/scala/org/agilefinland/gameoflife/BoardParserTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.agilefinland.gameoflife
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
import org.scalatest.matchers.MustMatchers

@RunWith(classOf[JUnitRunner])
class MainScalaTestMust extends FunSuite with MustMatchers {

def parseBoard(lines: String*) = {
BoardParser parse (List(lines: _*))
}

test("size 1x1") {
val board = parseBoard("1 1", ".")
board.width must equal(1)
board.height must equal(1)
}

test("cell must die") {
val board = parseBoard(
"2 2",
"..",
"*.")
board.shouldDie(0, 1) must equal(true)
}

test("cell must bring alive") {
val board = parseBoard(
"2 2",
"**",
".*")
board.shouldBeBorn(0, 1) must equal(true)
}

test("dead cell") {
var board = parseBoard("1 1", ".")
board.cellAt(0, 0).isAlive must equal(false)
}

test("alive cell") {
var board = parseBoard("1 1", "*")
board.cellAt(0, 0).isAlive must equal(true)
}

test("2x2 board") {
var board = parseBoard("2 2", ".*", "*.")
board.cellAt(0, 0).isAlive must equal(false)
board.cellAt(1, 0).isAlive must equal(true)
}

test("Count neighbours") {
var board = parseBoard("3 3",
"*..",
".*.",
".*.")
board.countNeighboursAt(1, 1) must equal(2);
board.countNeighboursAt(0, 1) must equal(3);
}

test("equals") {
var nextBoard = parseBoard("3 3",
"*..",
".*.",
".*.")

var expectedBoard = parseBoard("3 3",
"*..",
".*.",
".*.")

nextBoard must equal(expectedBoard)
}

test("must not equal") {
var nextBoard = parseBoard("3 3",
"*..",
".*.",
".*.")

var expectedBoard = parseBoard("3 3",
"*..",
"...",
".*.")

nextBoard must not equal(expectedBoard)
}

test("The next generation!") {
var nextBoard = parseBoard("3 3",
"*..",
".*.",
".*.").nextGeneration()

var expectedBoard = parseBoard("3 3",
"...",
".*.",
"...")

nextBoard must equal(expectedBoard)
}
}
63 changes: 0 additions & 63 deletions src/test/scala/org/agilefinland/gameoflife/MainScalaTest.scala

This file was deleted.

14 changes: 0 additions & 14 deletions src/test/scala/org/agilefinland/gameoflife/MainSpecs2Test.scala

This file was deleted.

10 changes: 0 additions & 10 deletions src/test/scala/org/agilefinland/gameoflife/MainTest.scala

This file was deleted.

Loading

0 comments on commit 6da2a92

Please sign in to comment.