javaFX實(shí)現(xiàn)五子棋小游戲
javaFX實(shí)現(xiàn)五子棋游戲,供大家參考,具體內(nèi)容如下
做課程設(shè)計(jì)的時(shí)候做到這個(gè),分享出來大家參考一下吧,圖片為游戲運(yùn)行過程
最下的代碼就是整個(gè)實(shí)現(xiàn)整個(gè)需求的



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';//隨機(jī)回合
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);//調(diào)用單元格的偵聽器
winer = ' ';
});
// 給底部文本設(shè)置初始情況
tf.setText(whoseTurn == 'X' ? "X's turn" : "O's turn");
// 給每個(gè)面板設(shè)置一個(gè)監(jiān)聽器
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';
}
}
//點(diǎn)擊鼠標(biāo) 在#字表格里面顯示一個(gè)隨機(jī)位置的的X or O
public void setListenerForCells(Cell[] cell){
numberOfClick = 0;//點(diǎn)擊次數(shù)清零
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){
//構(gòu)建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){
///構(gòu)建O面板
Circle circle = new Circle(75);//半徑為75
circle.setFill(Color.WHITE);//填充為白色
circle.setStroke(Color.BLACK);//邊框?yàn)楹谏?
StackPane pane2 = new StackPane();
pane2.getChildren().add(circle);
super.setCenter(pane2);
contain = 'O';
}
}
}
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)添加條碼或二維碼到Word文檔
這篇文章主要介紹了如何在Word文檔中添加條碼、二維碼??稍谖臋n正文段落中添加,也可在頁眉頁腳中添加,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-05-05
Spring?Boot?2.6.x整合Swagger啟動失敗報(bào)錯(cuò)問題的完美解決辦法
這篇文章主要給大家介紹了關(guān)于Spring?Boot?2.6.x整合Swagger啟動失敗報(bào)錯(cuò)問題的完美解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
springboot 如何使用jackson來處理實(shí)體類
這篇文章主要介紹了springboot使用jackson來處理實(shí)體類的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java 多線程Thread與runnable的區(qū)別
這篇文章主要介紹了java 多線程Thread與runnable的區(qū)別的相關(guān)資料,java線程有兩種方法繼承thread類與實(shí)現(xiàn)runnable接口,下面就提供實(shí)例幫助大家理解,需要的朋友可以參考下2017-08-08
Kafka Java Producer代碼實(shí)例詳解
這篇文章主要介紹了Kafka Java Producer代碼實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

