Skip to content

Commit e12b479

Browse files
Merge pull request aaronsilinskas#2 from aaronsilinskas/tv-codereview-2
Move Cell to its own file since multiple classes use it. Moved GridTesting to GridFixture and some other naming cleanup.
2 parents b206c8b + 03e8698 commit e12b479

File tree

8 files changed

+40
-39
lines changed

8 files changed

+40
-39
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
case class Cell(x: Int, y: Int) {
3+
def neighbors: Set[Cell] = {
4+
val neighborCells = for {
5+
neighborX <- x - 1 to x + 1
6+
neighborY <- y - 1 to y + 1
7+
if !(neighborX == x && neighborY == y)
8+
} yield Cell(neighborX, neighborY)
9+
10+
neighborCells.toSet
11+
}
12+
}

practice/game-of-life/src/main/scala/Game.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,3 @@ class Game(rules: Rules) {
2121
new Grid(survivingCells.toSet)
2222
}
2323
}
24-
25-
trait Rules {
26-
def isLiveCellSurviving(cell: Cell, liveNeighbors: Int): Boolean
27-
28-
def isDeadCellReviving(cell: Cell, liveNeighbors: Int): Boolean
29-
}

practice/game-of-life/src/main/scala/Grid.scala

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11

2-
case class Cell(x: Int, y: Int) {
3-
def neighbors: Set[Cell] = {
4-
val neighborCells = for {
5-
neighborX <- x - 1 to x + 1
6-
neighborY <- y - 1 to y + 1
7-
if !(neighborX == x && neighborY == y)
8-
} yield Cell(neighborX, neighborY)
9-
10-
neighborCells.toSet
11-
}
12-
}
13-
142
class Grid(val liveCells: Set[Cell]) {
153

164
def isAlive(cell: Cell): Boolean = {

practice/game-of-life/src/main/scala/DefaultRules.scala renamed to practice/game-of-life/src/main/scala/Rules.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11

2+
3+
trait Rules {
4+
def isLiveCellSurviving(cell: Cell, liveNeighbors: Int): Boolean
5+
6+
def isDeadCellReviving(cell: Cell, liveNeighbors: Int): Boolean
7+
}
8+
9+
210
class DefaultRules extends Rules {
311

412
override def isLiveCellSurviving(cell: Cell, liveNeighbors: Int): Boolean = {
@@ -9,5 +17,4 @@ class DefaultRules extends Rules {
917
liveNeighbors == 3
1018
}
1119

12-
}
13-
20+
}

practice/game-of-life/src/test/scala/GameStepSpec.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import GridTesting._
1+
22
import org.scalatest.{FeatureSpec, GivenWhenThen, Matchers}
33

44
import scala.collection.mutable
55

6-
class GameStepSpec extends FeatureSpec with Matchers with GivenWhenThen {
6+
class GameStepSpec extends FeatureSpec with Matchers with GivenWhenThen with GridFixture {
77

88
feature("Each game step applies the rules to all applicable cells.") {
99

@@ -65,10 +65,10 @@ class GameStepSpec extends FeatureSpec with Matchers with GivenWhenThen {
6565
val randomGroupingOfLiveCells = randomNeighborhood(randomCell(), 5)
6666

6767
val grid = new Grid(randomGroupingOfLiveCells)
68-
val liveCellNeighborCountsBeforeRules = countLiveNeighbors(grid, randomGroupingOfLiveCells)
68+
val liveCellLiveNeighborCountsBeforeRules = countLiveNeighbors(grid, randomGroupingOfLiveCells)
6969

7070
val deadCellsWhereRulesApply = getDeadNeighbors(randomGroupingOfLiveCells)
71-
val deadCellNeighborCountsBeforeRules = countLiveNeighbors(grid, deadCellsWhereRulesApply)
71+
val deadCellLiveNeighborCountsBeforeRules = countLiveNeighbors(grid, deadCellsWhereRulesApply)
7272

7373
And("a rule that all live cells die and their dead neighbors become alive")
7474
val rules = new InvertCellsRules with NeighborCountTracking
@@ -78,8 +78,8 @@ class GameStepSpec extends FeatureSpec with Matchers with GivenWhenThen {
7878
val nextGrid = game.step(grid)
7979

8080
Then("live neighbor counts reflect the original counts and not any cell deaths or births")
81-
rules.liveNeighborCounts shouldBe liveCellNeighborCountsBeforeRules
82-
rules.deadNeighborCounts shouldBe deadCellNeighborCountsBeforeRules
81+
rules.liveCellLiveNeighborCounts shouldBe liveCellLiveNeighborCountsBeforeRules
82+
rules.deadCellLiveNeighborCounts shouldBe deadCellLiveNeighborCountsBeforeRules
8383

8484
And("all live cells are now dead and dead neighbors are now alive")
8585
nextGrid.liveCells.intersect(randomGroupingOfLiveCells) shouldBe empty
@@ -100,16 +100,16 @@ class GameStepSpec extends FeatureSpec with Matchers with GivenWhenThen {
100100
}
101101

102102
private trait NeighborCountTracking extends Rules {
103-
val liveNeighborCounts = mutable.Map[Cell, Int]()
104-
val deadNeighborCounts = mutable.Map[Cell, Int]()
103+
val liveCellLiveNeighborCounts = mutable.Map[Cell, Int]()
104+
val deadCellLiveNeighborCounts = mutable.Map[Cell, Int]()
105105

106106
abstract override def isLiveCellSurviving(cell: Cell, liveNeighbors: Int): Boolean = {
107-
liveNeighborCounts += (cell -> liveNeighbors)
107+
liveCellLiveNeighborCounts += (cell -> liveNeighbors)
108108
super.isLiveCellSurviving(cell, liveNeighbors)
109109
}
110110

111111
abstract override def isDeadCellReviving(cell: Cell, liveNeighbors: Int): Boolean = {
112-
deadNeighborCounts += (cell -> liveNeighbors)
112+
deadCellLiveNeighborCounts += (cell -> liveNeighbors)
113113
super.isDeadCellReviving(cell, liveNeighbors)
114114
}
115115
}

practice/game-of-life/src/test/scala/GridTesting.scala renamed to practice/game-of-life/src/test/scala/GridFixture.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import scala.annotation.tailrec
22
import scala.util.Random
33

4-
object GridTesting {
4+
trait GridFixture {
55

66
def randomCell(): Cell = {
77
new Cell(Random.nextInt(), Random.nextInt())
88
}
99

1010
@tailrec
11-
def randomCells(count: Int = 1000, excluding: Set[Cell] = Set.empty, including: Set[Cell] = Set.empty): Set[Cell] = {
11+
final def randomCells(count: Int = 1000, excluding: Set[Cell] = Set.empty, including: Set[Cell] = Set.empty): Set[Cell] = {
1212
if (count == 0) {
1313
including
1414
} else {
@@ -30,12 +30,12 @@ object GridTesting {
3030
)
3131
}
3232

33-
def randomNeighborhood(seed: Cell, recursiveDepth: Int, neighborsToRecurse:Int = 3): Set[Cell] = {
33+
def randomNeighborhood(seed: Cell, recursiveDepth: Int, liveNeighborsPerSeed: Int = 3): Set[Cell] = {
3434
if (recursiveDepth == 0) {
3535
Set(seed)
3636
} else {
3737
val allNeighbors = getNeighborCells(seed)
38-
val randomSubsetOfNeighbors = Random.shuffle(allNeighbors).take(neighborsToRecurse)
38+
val randomSubsetOfNeighbors = Random.shuffle(allNeighbors).take(liveNeighborsPerSeed)
3939
randomSubsetOfNeighbors.flatMap { neighbor =>
4040
randomNeighborhood(neighbor, recursiveDepth - 1)
4141
}

practice/game-of-life/src/test/scala/GridSpec.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import GridTesting._
1+
22
import org.scalatest.{FeatureSpec, GivenWhenThen, Matchers}
33

44
import scala.util.Random
55

6-
class GridSpec extends FeatureSpec with Matchers with GivenWhenThen {
6+
class GridSpec extends FeatureSpec with Matchers with GivenWhenThen with GridFixture {
77

88
feature("A 2D grid of living and dead cells.") {
99
scenario("The grid is 32 bits, two dimensional, and holds living and dead cells.") {
@@ -41,7 +41,8 @@ class GridSpec extends FeatureSpec with Matchers with GivenWhenThen {
4141

4242
Then("the correct live neighbor count is given for each cell")
4343
for (s <- testScenarios) {
44-
s.grid.countLiveNeighbors(s.cell) shouldBe s.expectedLiveNeighborCount
44+
import s._
45+
grid.countLiveNeighbors(cell) shouldBe expectedLiveNeighborCount
4546
}
4647
}
4748

practice/game-of-life/src/test/scala/RulesSpec.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
import GridTesting._
32
import org.scalatest._
43

5-
class RulesSpec extends FeatureSpec with Matchers {
4+
class RulesSpec extends FeatureSpec with Matchers with GridFixture {
65

76
val rules = new DefaultRules()
87
val anyCell = randomCell()

0 commit comments

Comments
 (0)