Last active
August 29, 2015 14:01
-
-
Save danwatford/6c5581301d3b18e28db6 to your computer and use it in GitHub Desktop.
UI for Game of Life in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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