Java Reference
In-Depth Information
White gets the second turn and has three available options highlighted in Figure 5-2 . Let's assume white goes in
the lowermost position, flipping one black piece. Now it is black's turn again with five available positions (shown in
Figure 5-3 ).
Figure 5-3. The board position after the next move by white
Play continues this way, alternating between black and white unless one player has no moves, in which case
they pass on that turn. The game is over when both players have no moves, and the winner is the player with the most
pieces on the final board.
Building a JavaFX Model for Reversi
Now that you are armed with knowledge of how Reversi works, it is time to translate that into a set of Java classes that
represent the game model.
Your ReversiModel class needs to contain two primary pieces of information: the current board position and the
player whose turn it is. Listing 5-1 shows a basic model class to get you started.
Listing 5-1. ReversiModel Class for the Reversi Application
public class ReversiModel {
public static int BOARD_SIZE = 8;
public ObjectProperty<Owner> turn = new SimpleObjectProperty<>(Owner.BLACK);
public ObjectProperty<Owner>[][] board = new ObjectProperty[BOARD_SIZE][BOARD_SIZE];
private ReversiModel() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = new SimpleObjectProperty<>(Owner.NONE);
}
 
Search WWH ::




Custom Search