Skip to content

Commit

Permalink
removed a var and fixed a bug that made the program crash when all ce…
Browse files Browse the repository at this point in the history
…lls died out
  • Loading branch information
shadaj committed Apr 8, 2012
1 parent fd03b63 commit 9ece817
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/main/scala/conways/ConwaysGameProcessing.scala
Expand Up @@ -18,14 +18,14 @@ object ConwaysGameProcessing extends App {
class ConwaysGame extends PApplet {
val black = 0
val white = 255
val screenSize = 1000
val screenSize = 500

var current = new Grid(20)
var started = false
var boxThickness = screenSize / current.size
def boxThickness = screenSize / current.size

override def setup() = {
frameRate(10)
frameRate(20)
size(screenSize, screenSize)
background(0)
}
Expand All @@ -38,7 +38,6 @@ class ConwaysGame extends PApplet {
}

def drawGrid() {
boxThickness = screenSize / current.size
background(0)
current.liveCells.foreach {c =>
fill(white)
Expand All @@ -57,6 +56,6 @@ class ConwaysGame extends PApplet {
}

override def keyPressed(e: KeyEvent) {
started = true
started = !started
}
}
16 changes: 9 additions & 7 deletions src/main/scala/conways/Grid.scala
Expand Up @@ -28,13 +28,15 @@ class Grid(minSize: Int) {
}

def fit {
val xMin = liveCells.map(_.x).min
val yMin = liveCells.map(_.y).min
if (xMin < 0 || yMin < 0) {
liveCells = liveCells.map(e => Cell(e.x - xMin, e.y - yMin))
}
if ((xMin > 0 || yMin > 0) && size > minSize) {
liveCells = liveCells.map(e => Cell(e.x - (xMin - 1), e.y - (yMin - 1)))
if (!liveCells.isEmpty) {
val xMin = liveCells.map(_.x).min
val yMin = liveCells.map(_.y).min
if (xMin < 0 || yMin < 0) {
liveCells = liveCells.map(e => Cell(e.x - xMin, e.y - yMin))
}
if ((xMin > 0 || yMin > 0) && size > minSize) {
liveCells = liveCells.map(e => Cell(e.x - (xMin - 1), e.y - (yMin - 1)))
}
}
}
}
Expand Down

0 comments on commit 9ece817

Please sign in to comment.