Permalink
Browse files

removed a var and fixed a bug that made the program crash when all ce…

…lls died out
  • Loading branch information...
1 parent fd03b63 commit 9ece8174d86691b52d5e800f3c2cb21ad09740aa shadaj committed Apr 8, 2012
Showing with 13 additions and 12 deletions.
  1. +4 −5 src/main/scala/conways/ConwaysGameProcessing.scala
  2. +9 −7 src/main/scala/conways/Grid.scala
@@ -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)
}
@@ -38,7 +38,6 @@ class ConwaysGame extends PApplet {
}
def drawGrid() {
- boxThickness = screenSize / current.size
background(0)
current.liveCells.foreach {c =>
fill(white)
@@ -57,6 +56,6 @@ class ConwaysGame extends PApplet {
}
override def keyPressed(e: KeyEvent) {
- started = true
+ started = !started
}
}
@@ -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)))
+ }
}
}
}

0 comments on commit 9ece817

Please sign in to comment.