Skip to content

Instantly share code, notes, and snippets.

@danwatford
Last active August 29, 2015 14:01
Show Gist options
  • Save danwatford/6c5581301d3b18e28db6 to your computer and use it in GitHub Desktop.
Save danwatford/6c5581301d3b18e28db6 to your computer and use it in GitHub Desktop.
UI for Game of Life in Scala
object GofUI extends SimpleSwingApplication {
val grid = new OnOffGrid(20, 20) {
onOff = gridCellIsLive
}
val tickButton = new Button {
text = "Tick"
}
def top = new MainFrame {
title = "Game of Life"
contents = new BorderPanel {
layout(grid) = BorderPanel.Position.Center
layout(tickButton) = BorderPanel.Position.West
}
listenTo(tickButton)
listenTo(grid)
reactions += {
case ButtonClicked(`tickButton`) =>
model = model.tick
grid.repaint
case CellClicked(`grid`, x, y) =>
model = model.toggleCell(Cell(x, y))
grid.repaint
}
}
/**
* Holds the GoF model for the current generation. The model is changed in response to a 'tick'
* or to cells being toggled by the user.
*/
var model = GofModel(Cell(0, 0), Cell(0, 2), Cell(2, 2))
/**
* Function passed to the OnOffGrid to determine whether any particular cell is dead of alive.
*/
def gridCellIsLive: (Int, Int) => Boolean = { (x, y) => model.getCells contains (Cell(x, y)) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment