Skip to content

Commit

Permalink
12/refactor the app (#14)
Browse files Browse the repository at this point in the history
* Add Scalatest for unit testing

* Add scalatest to the project
Add some tests for model layer

* Change Scala version to 2.11.11
Add scoverage

* Add more tests for Board and Cell models

* Fix typos and solve import warnings

* Refactor Cell constructor

* The ApplicationView is refactored to use the model now.
  • Loading branch information
juanitodread authored Oct 5, 2017
1 parent 21d80df commit b08e4c9
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
package org.juanitodread.conwaygameoflife

import javax.swing.SwingUtilities
import org.juanitodread.conwaygameoflife.view.ApplicationView
import com.typesafe.scalalogging._

import org.juanitodread.conwaygameoflife.view.ApplicationView

/**
* Main application. Instantiate the frame in a new Thread
*
Expand Down
29 changes: 14 additions & 15 deletions src/main/scala/org/juanitodread/conwaygameoflife/model/Board.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.juanitodread.conwaygameoflife.model

import scala.collection.mutable.ArrayBuffer

import org.juanitodread.conwaygameoflife.model.cell.{
Cell,
State
Expand All @@ -9,25 +10,25 @@ import org.juanitodread.conwaygameoflife.model.cell.{
class Board(val size: Int = Board.MinSize) {
require(size >= Board.MinSize && size <= Board.MaxSize)

private val board = ArrayBuffer.fill(size, size)(Cell())
private var board = createBoard()

def reset(): Unit = {
board = createBoard()
}

def cellAt(row: Int, col: Int): Cell = {
require(isValidCellPosition(row, col))
val state = board(row)(col).isDead match {
case true => State.Dead
case false => State.Alive
}
Cell(state)
Cell(row, col, board(row)(col).state)
}

def aliveCell(row: Int, col: Int): Unit = {
require(isValidCellPosition(row, col))
board(row)(col) = Cell(State.Alive)
board(row)(col) = Cell(row, col, State.Alive)
}

def deadCell(row: Int, col: Int): Unit = {
require(isValidCellPosition(row, col))
board(row)(col) = Cell(State.Dead)
board(row)(col) = Cell(row, col, State.Dead)
}

def countAliveNeighborsForCell(row: Int, col: Int): Int = {
Expand All @@ -43,20 +44,19 @@ class Board(val size: Int = Board.MinSize) {
def calculateCellState(row: Int, col: Int): State.Value = {
require(isValidCellPosition(row, col))
val currentCell = this.cellAt(row, col)
(currentCell.isAlive, this.cellShouldAlive(row, col)) match {
case (true, true) => State.Alive
(currentCell.isAlive, this.countAliveNeighborsForCell(row, col)) match {
case (true, 2) => State.Alive
case (_, 3) => State.Alive
case _ => State.Dead
}
}

def cellShouldAlive(row: Int, col: Int): Boolean = {
Board.ValidNeighborsCount.contains(this.countAliveNeighborsForCell(row, col))
}

override def toString(): String = {
board.map(row => row.mkString(",")).mkString("\n")
}

private def createBoard() = ArrayBuffer.tabulate(size, size)(Cell(_, _))

private def isValidCellPosition(row: Int, col: Int): Boolean = {
row >= 0 && row < this.size && col >= 0 && col < this.size
}
Expand Down Expand Up @@ -86,7 +86,6 @@ object Board {
// $COVERAGE-OFF$
private final val MinSize = 30
private final val MaxSize = 100
private final val ValidNeighborsCount = List(2, 3)
// $COVERAGE-ON$

def apply() = new Board()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.juanitodread.conwaygameoflife.model.cell

class Cell(val state: State.Value = State.Dead) {
class Cell private (val id: String, val state: State.Value = State.Dead) {

def isAlive() = this.state == State.Alive

Expand All @@ -13,6 +13,15 @@ class Cell(val state: State.Value = State.Dead) {
}

object Cell {
def apply() = new Cell()
def apply(state: State.Value) = new Cell(state)
private final lazy val IdDelimiter = ":"
def apply(row: Int, col: Int) = new Cell(makeId(row, col))
def apply(row: Int, col: Int, state: State.Value) = new Cell(makeId(row, col), state)

def makeId(row: Int, col: Int) = s"$row$IdDelimiter$col"
def parseId(id: String) = {
val parsedId = id.split(IdDelimiter, 2)
if (parsedId.size != 2) throw new IllegalArgumentException()

(parsedId(0).toInt, parsedId(1).toInt)
}
}
Loading

0 comments on commit b08e4c9

Please sign in to comment.