javaFX实现五子棋游戏,供大家参考,具体内容如下
做课程设计的时候做到这个,分享出来大家参考一下吧,图片为游戏运行过程
最下的代码就是整个实现整个需求的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
package Version3;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class Version3 extends Application {
private char winer = ' ' ; //胜者
TextField tf = new TextField();
private char whoseTurn = ( int )(Math.random() * 2 ) == 0 ? 'X' : 'O' ; //随机回合
private int numberOfClick = 0 ;
@Override
public void start(Stage primaryStage) {
Button bt = new Button( "New game" ); //按钮
//TextField tf = new TextField();
Cell [] cell = new Cell[ 9 ];
for ( int i= 0 ; i< 9 ;i++){
cell[i] = new Cell( 2 , 1 );
}
GridPane gpane = new GridPane();
int num = 0 ;
for ( int i= 0 ; i< 3 ;i++){
for ( int j= 0 ; j< 3 ;j++){
gpane.add(cell[num],j,i);
num++;
}
}
tf.setEditable( false ); //文本不可编辑
BorderPane pane = new BorderPane();
pane.setTop(bt);
pane.setAlignment(bt,Pos.CENTER);
pane.setCenter(gpane);
pane.setBottom(tf);
//按钮事件 重新开始游戏
bt.setOnAction(e ->{
gpane.getChildren().clear();
for ( int i = 0 ; i < 9 ; i++) {
cell[i] = new Cell( 2 , 1 );
}
int k = 0 ;
for ( int i = 0 ; i < 3 ; i++) {
for ( int j = 0 ; j < 3 ; j++) {
gpane.add(cell[k], j, i);
k++;
}
}
whoseTurn = ( int )(Math.random() * 2 ) == 0 ? 'X' : 'O' ;
tf.setText(whoseTurn == 'X' ? "X's turn" : "O's turn" );
setListenerForCells(cell); //调用单元格的侦听器
winer = ' ' ;
});
// 给底部文本设置初始情况
tf.setText(whoseTurn == 'X' ? "X's turn" : "O's turn" );
// 给每个面板设置一个监听器
setListenerForCells(cell);
Scene scene = new Scene(pane, 495 , 550 );
primaryStage.setTitle( "version3" );
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void judgeWhoWin(Cell[] cell){
// 判断行
for ( int i = 0 ; i < 3 ; i++) {
if (cell[i * 3 ].contain == 'X' && cell[i * 3 + 1 ].contain == 'X' && cell[i * 3 + 2 ].contain == 'X' ) {
winer = 'X' ;
} else if (cell[i * 3 ].contain == 'O' && cell[i * 3 + 1 ].contain == 'O' && cell[i * 3 + 2 ].contain == 'O' ) {
winer = 'O' ;
}
}
// 判断列
for ( int i = 0 ; i < 3 ; i++) {
if (cell[i].contain == 'X' && cell[i + 3 ].contain == 'X' && cell[i + 6 ].contain == 'X' ) {
winer = 'X' ;
} else if (cell[i].contain == 'O' && cell[i + 3 ].contain == 'O' && cell[i + 6 ].contain == 'O' ) {
winer = 'O' ;
}
}
// 判断主、副对角线
if (cell[ 0 ].contain == 'X' && cell[ 4 ].contain == 'X' && cell[ 8 ].contain == 'X' ||
cell[ 2 ].contain == 'X' && cell[ 4 ].contain == 'X' && cell[ 6 ].contain == 'X' ) {
winer = 'X' ;
} else if (cell[ 0 ].contain == 'O' && cell[ 4 ].contain == 'O' && cell[ 8 ].contain == 'O' ||
cell[ 2 ].contain == 'O' && cell[ 4 ].contain == 'O' && cell[ 6 ].contain == 'O' ) {
winer = 'O' ;
}
}
//点击鼠标 在#字表格里面显示一个随机位置的的X or O
public void setListenerForCells(Cell[] cell){
numberOfClick = 0 ; //点击次数清零
for ( int i = 0 ; i < cell.length; i++) {
Cell temp = cell[i];
temp.setOnMouseClicked(e -> {
if (winer == ' ' ) {
if (whoseTurn == 'X'
&& e.getButton() == MouseButton.PRIMARY
&& temp.editable) {
temp.setContain( 1 , 1 );
temp.editable = false ; //不可编辑
winer = ' ' ;
whoseTurn = 'O' ; //下一次换回和
judgeWhoWin(cell); //判断输赢
if (winer == ' ' ){
numberOfClick++;
if (numberOfClick == 9 ){
tf.setText( "the game is a draw" );
}
else {
tf.setText(whoseTurn + "'s turn" );
}
}
else {
tf.setText( "Game is over, and the winner is " + winer);
}
} else if (whoseTurn == 'O'
&& e.getButton() == MouseButton.PRIMARY
&& temp.editable) {
temp.setContain( 1 , 2 );
temp.editable = false ; //不可编辑
winer = ' ' ;
whoseTurn = 'X' ; //下一次换回和
judgeWhoWin(cell); //判断输赢
if (winer == ' ' ){
numberOfClick++;
if (numberOfClick == 9 ){
tf.setText( "the game is a draw" );
}
else {
tf.setText(whoseTurn + "'s turn" );
}
}
else {
tf.setText( "Game is over, and the winner is " + winer);
}
}
}
});
}
}
class Cell extends BorderPane{
public char contain = ' ' ;
int num1 = 0 ,num2 = 0 ;
public boolean editable = true ;
public Cell( int num1, int num2){
super .setPadding( new Insets( 5 ));
super .setStyle( "-fx-border-color: black" );
super .setPrefSize( 2000 , 2000 );
this .setContain(num1,num2);
}
public void setContain( int num1, int num2){
if (num1== 1 && editable){
if (num2== 1 ){
//构建X面板
Line line1 = new Line( 0 , 0 , 150 , 150 );
Line line2 = new Line( 150 , 0 , 0 , 150 );
StackPane pane1 = new StackPane();
pane1.getChildren().addAll(line1,line2);
super .setCenter(pane1);
contain = 'X' ;
}
else if (num2== 2 && editable){
///构建O面板
Circle circle = new Circle( 75 ); //半径为75
circle.setFill(Color.WHITE); //填充为白色
circle.setStroke(Color.BLACK); //边框为黑色
StackPane pane2 = new StackPane();
pane2.getChildren().add(circle);
super .setCenter(pane2);
contain = 'O' ;
}
}
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/As_Yan_Do/article/details/107580520