/* * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * @author Mikhail Gorshenev */ package fifteen; import javafx.stage.*; import javafx.scene.*; import javafx.scene.shape.*; import javafx.scene.image.*; import javafx.scene.text.*; import javafx.scene.paint.*; import javafx.scene.input.*; import javafx.animation.*; import javafx.animation.transition.*; public class Main extends CustomNode { public var size = 4; public var numTiles = bind size*size -1; var random = new java.util.Random(); public var showImages = true; var tiles: Tile[]; var noTile:Tile = Tile { num: size*size visible: false } public function initialize() { for (i in [1..numTiles]) { var tile = Tile { num: i img: "{__DIR__}img{i}.png" showImage: bind showImages } insert tile into tiles; } insert noTile into tiles; initTiles(); } public function solve() { var oldtiles = tiles; for (i in [0..numTiles]) { var num = oldtiles[i].num; println("num={num}"); tiles[num-1] = oldtiles[i]; } initTiles(); } public function initTiles() { for (num in [0..numTiles]) { tiles[num].xpos = num mod size; tiles[num].ypos = num / size; } } public function shuffle() { for (i in [1..100]) { var rnd = 2*random.nextInt(2)-1; println("move 0,{rnd}"); move(0, rnd, true); rnd = 2*random.nextInt(2)-1; println("move {rnd},0"); move(rnd, 0, true); } initTiles(); } override var onKeyPressed = function(e: KeyEvent) { if (e.code == KeyCode.VK_UP) { move(0, -1, false); } else if (e.code == KeyCode.VK_DOWN) { move(0, 1, false); } else if (e.code == KeyCode.VK_LEFT) { move(-1, 0, false); } else if (e.code == KeyCode.VK_RIGHT) { move(1, 0, false); } if (e.code == KeyCode.VK_1 or e.code == KeyCode.VK_SOFTKEY_0) { shuffle(); } else if (e.code == KeyCode.VK_3 or e.code == KeyCode.VK_SOFTKEY_1) { solve(); } else if (e.code == KeyCode.VK_ENTER) { showImages = not showImages; } } public function move(xdir: Integer, ydir: Integer, shuffling: Boolean) { // find an empty space var empty:Integer = 0; while (tiles[empty] != noTile) { empty++; } var emptyX = empty mod size; var emptyY = empty / size; var otherX = emptyX - xdir; var otherY = emptyY - ydir; if (otherX >=0 and otherX =0 and otherY xpos*size tween Interpolator.EASEBOTH ] } ] }.play(); } public var ypos: Integer on replace { Timeline { keyFrames: [ KeyFrame { time: 1s values: [ y => ypos*size tween Interpolator.EASEBOTH ] } ] }.play(); } public var size = 60; var x = xpos * size; var y = ypos * size; public var img: String; override function create():Node { var result = Group { translateX: bind x translateY: bind y content: [ Rectangle { width: size -1 height: size -1 fill: Color.LIGHTGREY stroke: Color.BLACK } Text { font: Font {size: (size*2/3)} content: "{num}" x: bind (if (num < 10) then size*1/6 else 0) y: bind size*5/6 } ImageView { image: Image {url: img} visible: bind showImage } Rectangle { width: size -1 height: size -1 fill: null stroke: Color.BLACK } ] } return result; } public function move(xdir: Integer, ydir: Integer) { xpos += xdir; ypos += ydir; } } public function run() { var stage: Stage; var game = Main{ translateX: bind if (stage.height < stage.width) then (stage.width- stage.height)/2 else 0; translateY: bind if (stage.height > stage.width) then (stage.height- stage.width)/2 else 0; }; stage = Stage { title: "Picture puzzle" scene: Scene { fill: Color.BLACK content: game } }; game.requestFocus(); return stage; }