Taking All the Colored Pieces: Reversi Game
JavaFX technology enables you to easily write a full-screen game.
Understanding the Code
The Main class contains the entry point of the sample. The Board class contains the logic and AI of the game and was written in the Java language. Another important class is the BoardView class, which represents the game board in its current position.
Code Details
To reiterate, the Main class contains the entry point of the sample. The class creates a Stage object with background, an instance of BoardView class, and some additional information, as shown in Figure 1.
def stage: Stage = Stage {
title: "Reversi"
fullScreen: "{__PROFILE__}" != "browser"
scene: Scene {
fill: LinearGradient {
proportional: true
startX: 0
endX: 0
startY: 0
endY: 1
stops: [
Stop{ offset: 0 color: Color.web("#B6B6B6") },
Stop{ offset: 1 color: Color.web("#121212") }
]
}
width: if (Config.IS_MOBILE) WIDTH else 600
height: if (Config.IS_MOBILE) HEIGHT else 400
content: [
Group {
transforms: [
Scale {
x: bind scale
y: bind scale
pivotX: 0
pivotY: 0
}
]
content: [
boardView,
...
]
}
}
Figure 1: Main Class
The logic of the game and AI are implemented in the Board class. To improve performance (and therefore strengthen the computer opponent), that class was written in the Java language, without using the JavaFX Script language. The board API is shown in Figure 2.
/**
* @author Pavel Porvatov
*/
public class Board {
public static enum GameResult {
UNKNOWN,
DRAW,
DARK_WINS,
LIGHT_WINS
}
/**
* Cell value for an empty cell
*/
public static final int PIECE_EMPTY = 0;
/**
* Cell value for a dark piece
*/
public static final int PIECE_DARK = 1;
/**
* Cell value for a light piece
*/
public static final int PIECE_LIGHT = -1;
/**
* Returns a piece on the specified board position
*
* @see #PIECE_EMPTY
* @see #PIECE_DARK
* @see #PIECE_LIGHT
*/
public int getPiece(int x, int y) {...}
/**
* Prepares start position on the board
*/
public void setStartPosition() {...}
/**
* Determines piece color of current move
*/
public boolean isDark() {...}
/**
* Returns state of the game
*/
public GameResult getGameResult() {...}
/**
* Returns possible moves in the current position
*/
public Coord[] getMoves() {...}
/**
* Perform specified move
*/
public void makeMove(Coord move) {...}
/**
* Returns count of dark pieces on the board
*/
public int getDarkPiecesCount() {...}
/**
* Returns count of light pieces on the board
*/
public int getLightPiecesCount() {...}
/**
* Calculates the best move in the current position and
* returns the best move that the computer has found
*
* @see #getBestMove()
*/
public Coord run() {...}
How to Play
Reversi, like chess and checkers, is a game for two players. After the game is launched, you see the board in its initial position. The two colors of pieces played on the board are "light" and "dark." The player who owns the dark pieces makes the first move. A move is established by a player placing a colored piece on a free cell of the board. The move is valid if there is an occupied line of the opponent's pieces (at least one piece) between the new piece and another player's piece. After the move, all opponent pieces that lie between the new piece and the other player's pieces are replaced by pieces in the initial player's color.
The game is over when neither opponent can make a valid move. The winner is the player who has the most pieces on the board at game's end.
