Java Reference
In-Depth Information
int y = cellY + directionY;
while (x >=0 && x < BOARD_SIZE && y >=0 && y < BOARD_SIZE) {
bind(board[x][y]);
x += directionX;
y += directionY;
}
}
@Override
protected boolean computeValue() {
Owner turnVal = turn.get();
int x = cellX + directionX;
int y = cellY + directionY;
boolean first = true;
while (x >=0 && x < BOARD_SIZE && y >=0 && y < BOARD_SIZE && board[x][y].get() != Owner.NONE) {
if (board[x][y].get() == turnVal) {
return !first;
}
first = false;
x += directionX;
y += directionY;
}
return false;
}
};
}
The last step in highlighting legal moves is to wire the
legalMove
model function up to the squares. This involves
binding the
style
property of the
ReversiSquare
class to the
legalMove
method (changes in bold).
public ReversiSquare(
final int x, final int y
) {
styleProperty().bind(Bindings.when(model.legalMove(x, y))
.then("-fx-background-color: derive(dodgerblue, -60%)")
.otherwise("-fx-background-color: burlywood"));
Light.Distant light = new Light.Distant();
■
the
derive
function used is a JavaFX-specific Css function that lets you create a new color based on an
existing one. the second parameter is brightness, which can range from -100% (black) to 100% white.
Tip
Also, don't forget to add in the static model variable on which this code depends:
private static ReversiModel model = ReversiModel.getInstance();
And also to update the
ReversiSquare
construction in the
tiles()
method to pass in the x and y board coordinates:
ReversiSquare square = new ReversiSquare(i, j);
Now on running the application, it correctly highlights the same four moves for black that were described in the
“Board Layout and Basic Rules” section, as shown in Figure
5-15
.
Search WWH ::
Custom Search