Skip to content

Instantly share code, notes, and snippets.

@heimp
Created July 27, 2012 17:44
Show Gist options
  • Save heimp/3189323 to your computer and use it in GitHub Desktop.
Save heimp/3189323 to your computer and use it in GitHub Desktop.
Flood It/Pixelated clone in GroovyFX
import static groovyx.javafx.GroovyFX.start
import groovyx.javafx.beans.FXBindable
import javafx.scene.*
import javafx.scene.text.*
import javafx.scene.shape.*
import javafx.scene.paint.*
import javafx.scene.layout.*
import javafx.beans.property.*
import javafx.event.*
class Game {
Random random = new Random()
@FXBindable int turnNumber = 0
@FXBindable int lowestTurns = 0
List colors = [Color.PURPLE, Color.LAWNGREEN, Color.DODGERBLUE, Color.PINK, Color.CRIMSON, Color.ORANGE]
int gridWidth = 20
int gridHeight = 10
int rectSize = 21
Rectangle[][] playfield = generatePlayfield()
def restart() {
turnNumber = 0
randomizePlayfield()
}
def generatePlayfield() {
playfield = new Rectangle[gridHeight][gridWidth]
for(r in 0..<gridHeight) {
for(c in 0..<gridWidth) {
playfield[r][c] = new Rectangle(width: rectSize, height: rectSize, fill: colors[random.nextInt(colors.size())])
GridPane.setConstraints(playfield[r][c], c, r)
}
}
return playfield
}
def randomizePlayfield() {
playfield.flatten().each {
it.fill = colors[random.nextInt(colors.size())]
}
}
}
start {
def game = new Game()
def primaryStage = stage(title: "assimilate!", visible: true) {
scene(fill: black, width: 600, height: 600) {
vbox(padding: 80, spacing: 20) {
gridPane(children: game.playfield.flatten())
hbox {
game.colors.each {
rectangle(width: 70, height: 32, fill: it, onMouseClicked {
game.turnNumber++
def oldColor = game.playfield[0][0].fill
def newColor = it.target.fill
def visited = []
def visit
visit = {c, r ->
game.playfield[r][c].fill = newColor
visited << game.playfield[r][c]
if(c - 1 >= 0 && !visited.contains(game.playfield[r][c - 1]) && game.playfield[r][c - 1].fill == oldColor) {
visit(c - 1, r)
}
if(r - 1 >= 0 && !visited.contains(game.playfield[r - 1][c]) && game.playfield[r - 1][c].fill == oldColor) {
visit(c, r - 1)
}
if(c + 1 < game.gridWidth && !visited.contains(game.playfield[r][c + 1]) && game.playfield[r][c + 1].fill == oldColor) {
visit(c + 1, r)
}
if(r + 1 < game.gridHeight && !visited.contains(game.playfield[r + 1][c]) && game.playfield[r + 1][c].fill == oldColor) {
visit(c, r + 1)
}
}
visit(0, 0)
def completed = true
game.playfield.flatten().each {
if(it.fill != newColor) {
completed = false;
}
}
if(completed) {
def msg
if(game.lowestTurns == 0) {
game.lowestTurns = game.turnNumber
msg = "good job\nnow try to beat ${game.lowestTurns}"
} else {
if(game.turnNumber < game.lowestTurns) {
msg = "so you beat ${game.lowestTurns},\nbut can you beat ${game.turnNumber}?"
game.lowestTurns = game.turnNumber
} else if(game.turnNumber > game.lowestTurns) {
msg = "um, not quite\nmight as well try again"
} else {
msg = "whoa a tie\ntry again or something"
}
}
msg += "\n(click to try again)"
def popper
popper = popup {
onHidden: {game.restart()}
stackPane {
rectangle(width: primaryStage.width, height: primaryStage.height, fill: cyan)
text(text: msg)
rectangle(width: primaryStage.width, height: primaryStage.height, fill: transparent, onMouseClicked: {popper.hide()})
}
}
popper.show(primaryStage)
}
}
)
}
}
label(text: bind{"# of turns: " + game.turnNumber}, font: "12pt Courier", textFill: cyan)
label(text: bind{game.lowestTurns == 0 ? "" : "the lowest number of turns is " + game.lowestTurns}, font: "12pt Courier", textFill: cyan)
stackPane {
rectangle(width: 200, height: 30, fill: aliceblue)
text(text: "click me to restart this game")
rectangle(width: 200, height: 30, fill: transparent, onMouseClicked: {game.restart()})
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment