Java Reference
In-Depth Information
66
67
if
(cell[
0
][
0
].getToken() == token
68 && cell[
1
][
1
].getToken() == token
69 && cell[
2
][
2
].getToken() == token) {
70
check major diagonal
return true
;
71 }
72
73
if
(cell[
0
][
2
].getToken() == token
74 && cell[
1
][
1
].getToken() == token
75 && cell[
2
][
0
].getToken() == token) {
76
check subdiagonal
return true
;
77 }
78
79
return false
;
80 }
81
82
// An inner class for a cell
83
public class
Cell
extends
Pane {
inner class
Cell
84
// Token used for this cell
85
private char
token =
' '
;
86
87
public
Cell() {
88 setStyle(
"-fx-border-color: black"
);
89
this
.setPrefSize(
2000
,
2000
);
90
this
.setOnMouseClicked(e -> handleMouseClick());
register listener
91 }
92
93
/** Return token */
94
public char
getToken() {
95
return
token;
96 }
97
98
/** Set a new token */
99
public void
setToken(
char
c) {
100 token = c;
101
102
if
(token ==
'X'
) {
103 Line line1 =
new
Line(
10
,
10
,
104
this
.getWidth() -
10
,
this
.getHeight() -
10
);
105 line1.endXProperty().bind(
this
.widthProperty().subtract(
10
));
106 line1.endYProperty().bind(
this
.heightProperty().subtract(
10
));
107 Line line2 =
new
Line(
10
,
this
.getHeight() -
10
,
108
this
.getWidth() -
10
,
10
);
109 line2.startYProperty().bind(
110
this
.heightProperty().subtract(
10
));
111 line2.endXProperty().bind(
this
.widthProperty().subtract(
10
));
112
113
display X
// Add the lines to the pane
114
this
.getChildren().addAll(line1, line2);
115 }
116
else if
(token ==
'O'
) {
117 Ellipse ellipse =
new
Ellipse(
this
.getWidth() /
2
,
118
this
.getHeight() /
2
,
this
.getWidth() /
2
-
10
,
119
this
.getHeight() /
2
-
10
);
120 ellipse.centerXProperty().bind(
121
this
.widthProperty().divide(
2
));
122 ellipse.centerYProperty().bind(
123
this
.heightProperty().divide(
2
));
124 ellipse.radiusXProperty().bind(
125
display O
this
.widthProperty().divide(
2
).subtract(
10
));
Search WWH ::
Custom Search