JavaFX五子棋

package com.example.javafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class TicTacToe1 extends Application {
    public GridPane pane = new GridPane();
    public BorderPane borderPane = new BorderPane();
    public BorderPane borderPaneForButton = new BorderPane();
    
    private Cell[][] cells = new Cell[3][3];
    private Label lblStatus = new Label();
    public Button button = new Button("再来一局");

    public char whoseTurn;
    
    public void init(){
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++)
                pane.add(cells[i][j] = new Cell(), i, j);
        }
        whoseTurn = 'X';
        lblStatus.setText("X is turn");
    }
    //判断输赢
    public boolean isWon(char token) {
        //8种情况
        for (int i = 0; i < 3; i++) {
            if (cells[i][0].getToken() == token
                    && cells[i][1].getToken() == token
                    && cells[i][2].getToken() == token) {
                return true;
            }
        }
        for (int i = 0; i < 3; i++) {
            if (cells[0][i].getToken() == token
                    && cells[1][i].getToken() == token
                    && cells[2][i].getToken() == token) {
                return true;
            }
        }

        if (cells[0][0].getToken() == token && cells[1][1].getToken() == token && cells[2][2].getToken() == token) {
            return true;
        }

        if (cells[0][2].getToken() == token && cells[1][1].getToken() == token && cells[2][0].getToken() == token) {
            return true;
        }
        return false;
    }
    //细胞类
    public class Cell extends Button {
        private char token = ' ';

        public Cell() {
            this.setPrefSize(200000, 200000);
            this.setOnMouseClicked(mouseEvent -> {
                if (token == ' ' && whoseTurn != ' ') {
                    setToken(whoseTurn);

                    if (isWon(whoseTurn)) {//赢了-结束
                        lblStatus.setText(whoseTurn + " won");
                        whoseTurn = ' ';
                    } else {
                        whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                        lblStatus.setText(whoseTurn + "'s turn");
                    }
                }
            });
        }

        public char getToken() {
            return token;
        }

        public void setToken(char c) {
            token = c;
            if (token == 'X') {
                Line line1 = new Line(10, 10, this.getWidth() - 10, this.getHeight() - 10);
                Line line2 = new Line(10, this.getHeight() - 10, this.getWidth() - 10, 10);
                this.getChildren().addAll(line1, line2);
            } else if (token == 'O') {
                Ellipse ellipse = new Ellipse(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2 - 10, this.getHeight() / 2 - 10);
                ellipse.setFill(Color.ORANGE);
                ellipse.setStroke(Color.BLACK);
                this.getChildren().add(ellipse);
            }
        }
    }
    //启动程序
    @Override
    public void start(Stage primaryStage) {
        init();
        borderPaneForButton.setCenter(lblStatus);
        borderPaneForButton.setRight(button);
        
        borderPane.setCenter(pane);//中间是主界面
        borderPane.setBottom(borderPaneForButton);//底下是标签和按钮

        button.setOnAction(actionEvent -> {init();});

        Scene scene = new Scene(borderPane, 400, 500);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TicTacToe");
        primaryStage.show();
    }
    //主程序
    public static void main(String[] args) {
        launch(args);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值