Skip to content

Instantly share code, notes, and snippets.

@danwatford

danwatford/GofLUI.scala

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.