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);
    }
}

你可能感兴趣的:(java)