Note from Jim Weaver: Just a friendly reminder to reserve the JavaOne Pro JavaFX™ Platform: RIA Enterprise Application Development with JavaFX Technology session that Stephen Chin and I will be presenting. As of May 29, 2009 there are only 96 seats available.
You probably know that the next version of JavaFX is expected to be released around JavaOne 2009. Did you know, however, that the latest build of the JavaFX compiler is always publicly available so that you can play with new language features?
One of the new features is the ability to reference Java arrays. JavaFX has always been able to instantiate and access members of Java classes, but the ability to have a JavaFX variable that references a Java array has not been available until recently. Consequently, to use a Java array it has been necessary to iterate over its elements and insert them into a JavaFX sequence. To demonstrate the ability to reference multi-dimensional Java arrays, and in honor of today being Cinco de Mayo (5/5), I'm going to use some ideas from the three-dimensional chess game variation that uses a 5x5x5 cube. This, by the way, is a different variation from the 3D chess game made famous on Star Trek (see image below).
To start off, here is a Java class that contains a three-dimensional array, with each element having the ability to hold a chess piece. It also defines constants that represent each type of chess piece, in each color. Note: In this chess game variation (Raumschach, German for "space chess"), there is an additional piece: It is a Unicorn, and is represented by an upside-down Knight.
/*
* ThreeDimChessBoard.java
*
* Example of referencing Java arrays from JavaFX.
* This Java class represents the squares on a Raumschach (German for
* "Space Chess") board, as described in the following Wikipedia article:
* http://en.wikipedia.org/wiki/Three-dimensional_chess
*
* Developed 2009 by James L. Weaver as a JavaFX example to be freely used
*/
package javafxpert;
public class ThreeDimChessBoard {
public static int EMPTY = 0;
public static int KING_W = 1;
public static int QUEEN_W = 2;
public static int ROOK_W = 3;
public static int BISHOP_W = 4;
public static int KNIGHT_W = 5;
public static int UNICORN_W = 6;
public static int PAWN_W = 7;
public static int KING_B = 9;
public static int QUEEN_B = 10;
public static int ROOK_B = 11;
public static int BISHOP_B = 12;
public static int KNIGHT_B = 13;
public static int UNICORN_B = 14;
public static int PAWN_B = 15;
public int[][][] boardArray = new int[5][5][5];
public ThreeDimChessBoard() {
// Floor "A"
boardArray[0][0][0] = ROOK_W;
boardArray[0][0][1] = KNIGHT_W;
boardArray[0][0][2] = KING_W;
boardArray[0][0][3] = KNIGHT_W;
boardArray[0][0][4] = ROOK_W;
boardArray[0][1][0] = PAWN_W;
boardArray[0][1][1] = PAWN_W;
boardArray[0][1][2] = PAWN_W;
boardArray[0][1][3] = PAWN_W;
boardArray[0][1][4] = PAWN_W;
// Floor "B"
boardArray[1][0][0] = PAWN_W;
boardArray[1][0][1] = UNICORN_W;
boardArray[1][0][2] = QUEEN_W;
boardArray[1][0][3] = BISHOP_W;
boardArray[1][0][4] = UNICORN_W;
boardArray[1][1][0] = PAWN_W;
boardArray[1][1][1] = PAWN_W;
boardArray[1][1][2] = PAWN_W;
boardArray[1][1][3] = PAWN_W;
boardArray[1][1][4] = PAWN_W;
// Floor "D"
boardArray[3][3][0] = PAWN_B;
boardArray[3][3][1] = PAWN_B;
boardArray[3][3][2] = PAWN_B;
boardArray[3][3][3] = PAWN_B;
boardArray[3][3][4] = PAWN_B;
boardArray[3][4][0] = BISHOP_B;
boardArray[3][4][1] = UNICORN_B;
boardArray[3][4][2] = QUEEN_B;
boardArray[3][4][3] = BISHOP_B;
boardArray[3][4][4] = UNICORN_B;
// Floor "E"
boardArray[4][3][0] = PAWN_B;
boardArray[4][3][1] = PAWN_B;
boardArray[4][3][2] = PAWN_B;
boardArray[4][3][3] = PAWN_B;
boardArray[4][3][4] = PAWN_B;
boardArray[4][4][0] = ROOK_B;
boardArray[4][4][1] = KNIGHT_B;
boardArray[4][4][2] = KING_B;
boardArray[4][4][3] = KNIGHT_B;
boardArray[4][4][4] = ROOK_B;
}
}
With the board and pieces being represented in Java, we'll now define a JavaFX class that uses the array in the Java class and provides some example functions such as movePiece() and getPieceAtSquare(). These functions are part of the chess board model, and protect its integrity. Note: The functions in this class are just examples, so elaborating on them to provide chess game logic is an "exercise for the reader" as they say.
/*
* ThreeDimChessBoardModel.fx
*
* Example of using Java arrays from JavaFX.
* This class has functions such as movePiece() and getPieceAtSquare().
* These functions use, and protect the integrity of, the multi-dimensional
* boardModel array in the ThreeDimChessBoard Java class.
*
* Developed 2009 by James L. Weaver as a JavaFX example to be freely used
*/
package javafxpert;
public class ThreeDimChessBoardModel {
var chessBoard = new ThreeDimChessBoard();
public function getPieceAtSquare(position:SquarePosition):Integer {
return chessBoard.boardArray[position.floor][position.rank][position.file];
}
public function putPieceAtSquare(position:SquarePosition,
piece:Integer):Integer {
chessBoard.boardArray[position.floor][position.rank][position.file] =
piece;
}
public function movePiece(startPosition:SquarePosition,
endPosition:SquarePosition):Void {
// TODO: Insert actual 3D chess logic here, throwing exceptions and such
if (getPieceAtSquare(startPosition) != ThreeDimChessBoard.EMPTY and
getPieceAtSquare(endPosition) == ThreeDimChessBoard.EMPTY) {
putPieceAtSquare(endPosition, getPieceAtSquare(startPosition));
putPieceAtSquare(startPosition, ThreeDimChessBoard.EMPTY);
}
else {
println("Square: {endPosition} already had a "
"{getPieceAtSquare(startPosition)} in it");
}
}
}
Instances of the following class represent locations in the three-dimensional board, and are used as arguments in functions of the preceding class:
/*
* SquarePosition.fx
*
* Example of using Java arrays, including multi-dimensional, from JavaFX.
* This class represents the location of a chess piece on a three-dimensional
* chess board.
*
* Developed 2009 by James L. Weaver as a JavaFX example to be freely used
*/
package javafxpert;
public class SquarePosition {
public var floor:Integer;
public var rank:Integer;
public var file:Integer;
}
Finally, here is a JavaFX program that uses the ThreeDimChessBoardModel JavaFX class to manipulate the chess pieces on the board. Another exercise for the reader would be to create a graphical UI for this 3D chess program, perhaps using JavaFX meets Java 3D capabilities that August Lammersdorf has been developing :-)
/*
* AccessingJavaArraysExample.fx
*
* Example of using Java arrays, including multi-dimensional, from JavaFX.
* This program uses the ThreeDimChessBoardModel JavaFX class, which accesses
* the three dimensional array in the ThreeDimChessBoard Java class.
*
* Developed 2009 by James L. Weaver as a JavaFX example to be freely used
*/
package javafxpert;
var boardModel:ThreeDimChessBoardModel = ThreeDimChessBoardModel {};
var startPosition:SquarePosition;
var endPosition:SquarePosition;
startPosition = SquarePosition {floor: 1 rank: 1 file: 0};
endPosition = SquarePosition {floor: 2 rank: 1 file: 0};
println("Before moving white pawn at Ba2 one step up:");
println("Ba2 contains: {boardModel.getPieceAtSquare(startPosition)}");
println("Ca2 contains: {boardModel.getPieceAtSquare(endPosition)}");
// Move white pawn at Ba2 one step up
boardModel.movePiece(startPosition, endPosition);
println("After moving white pawn at Ba2 one step up:");
println("Ba2 contains: {boardModel.getPieceAtSquare(startPosition)}");
println("Ca2 contains: {boardModel.getPieceAtSquare(endPosition)}");
Thanks to Weiqi Gao, by the way, for informing me of the capability described in this post. As always, please leave a comment if you have any questions.
Live long and prosper,
Jim Weaver
JavaFXpert.com
Recent Comments