/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
* Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Oracle Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package reversi;
import javafx.animation.*;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.paint.*;
public class BoardView extends CustomNode {
def BORDER_COLOR = Color.GREY;
def BOARD_COLOR = Color.GREEN;
def LINE_COLOR = Color.GREY;
public-init var board: Board;
public var onChanged: function();
def borderSize = if (Config.IS_MOBILE) 0 else 20;
def lineSize = 1;
def cellSize = if (Config.IS_MOBILE) 29 else 69;
def shadowSize = if (Config.IS_MOBILE) 2 else 3;
var pieces: PieceNode[];
var compDark = false;
def STATE_MAN = 1;
def STATE_COMPUTER = 2;
var state = STATE_MAN;
def timeline: Timeline = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames : [
KeyFrame {
time: 1s
action: function () {
def best = board.run();
board.makeMove(best);
invalidateBoard();
if (not isComputerMove()) {
state = STATE_MAN;
timeline.stop();
}
}
}
]
}
override public function create(): Node {
for (y in [0..7], x in [0..7]) {
insert PieceNode {
translateX: bind borderSize + x * (cellSize + lineSize) + shadowSize
translateY: bind borderSize + (7 - y) * (cellSize + lineSize) + shadowSize
} into pieces;
}
invalidateBoard();
Group {
content: [
ImageView {
smooth: true
image: Config.boardImage
onMousePressed: function (e) {
if (state != STATE_MAN) {
return;
}
var x = convertPointToCoord(e.x);
var y = convertPointToCoord(e.y);
if (x < 0 or y < 0) {
return;
}
y = 7 - y;
for (coord in board.getMoves()) {
if (coord.x == x and coord.y == y) {
board.makeMove(coord);
invalidateBoard();
if (isComputerMove()) {
state = STATE_COMPUTER;
timeline.play();
}
break;
}
}
}
}
pieces
]
}
}
public function isComputerMove(): Boolean {
return board.getGameResult() == Board.GameResult.UNKNOWN and
compDark == board.isDark();
}
public function startGame(compDark: Boolean) {
this.compDark = compDark;
board.setStartPosition();
invalidateBoard();
if (isComputerMove()) {
state = STATE_COMPUTER;
timeline.play();
} else {
state = STATE_MAN;
}
}
function convertPointToCoord(point: Number): Integer {
var i: Integer = (point - borderSize) as Integer;
if (i < 0 or i >= 8 * (cellSize + lineSize) or
i mod (cellSize + lineSize) >= cellSize) {
return -1;
}
return i / (cellSize + lineSize);
}
function invalidateBoard() {
for (x in [0..7], y in [0..7]) {
def piece = board.getPiece(x, y);
def node = pieces[y * 8 + x];
if (piece == Board.PIECE_EMPTY) {
node.visible = false;
} else {
node.dark = piece == Board.PIECE_DARK;
node.scaleX = 1;
node.scaleY = 1;
node.visible = true;
}
}
for (coord in board.getMoves()) {
def node = pieces[coord.y * 8 + coord.x];
node.dark = board.isDark();
node.scaleX = 0.2;
node.scaleY = 0.2;
node.visible = true;
}
if (onChanged != null) {
onChanged();
}
}
}
class PieceNode extends CustomNode {
public var dark: Boolean;
override public function create(): Node {
ImageView {
smooth: true
translateX: (cellSize - Config.darkImage.width) / 2
translateY: (cellSize - Config.darkImage.height) / 2
image: bind if (dark) Config.darkImage else Config.lightImage
}
}
}