开发井字游戏

Tic_Tac_Toe

井字游戏的JavaFx程序

假设开始时所有的单元格都是空的,并且第一个玩家用X标记,第二个玩家用O标记。
要在单元格上做标记,玩家应该将鼠标指针放在这个单元格上单击。如果这个单元格为空,则显示标记(X或O)。如果单元格已经被填充,则忽略玩家的动作。

package Chap16;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Cell;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Ellipse;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import org.omg.CORBA.ParameterMode;

public class TicTacToe extends Application {
    //谁的回合,默认先手为X
    private char whoseTurn ='X';
    //井字棋盘3X3
    private Cell[][] cell = new Cell[3][3];
    //游戏状态
    private Label lblStatus = new Label("X's turn to play");

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane pane = new GridPane();
        for(int i=0;i<3;i++)
            for(int j = 0;j < 3;j++)
                pane.add(cell[i][j] = new Cell(),j,i);

            BorderPane borderPane = new BorderPane();
            borderPane.setCenter(pane);
            borderPane.setBottom(lblStatus);

            Scene scene = new Scene(borderPane,450,170);
            primaryStage.setTitle("TicTacToe");
            primaryStage.setScene(scene);
            primaryStage.show();

    }
    public boolean isFull(){
        for (int i =0 ;i<3;i++)
            for(int j = 0;j<3;j++)
                if (cell[i][j].getToken()==' ')
                    return false;
        return true;

    }

    public boolean isWon(char token){
        for(int i = 0;i<3;i++)
            if(cell[i][0].getToken()==token
                && cell[i][1].getToken()==token
                && cell[i][2].getToken()==token){
                return true;
            }
        for (int j =0;j<3;j++)
            if(cell[0][j].getToken()==token
            && cell[1][j].getToken()==token
            && cell[2][j].getToken()==token){
                return true;
            }
        if(cell[0][0].getToken()==token
        &&cell[1][1].getToken()==token
        && cell[2][2].getToken()==token){
            return true;
        }
        if(cell[0][2].getToken()==token
                &&cell[1][1].getToken()==token
                && cell[2][0].getToken()==token){
            return true;
        }
        return false;
    }

    //AN inner class for a cell
    public class Cell extends Pane{
        private char token=' ';

        public Cell(){
            setStyle("-fx-border-color: black");
            this.setPrefSize(2000,2000);
            
            //监听鼠标动作
            this.setOnMouseClicked(e->handleMouseClick());
        }

        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);
                line1.endXProperty().bind(this.widthProperty().subtract(10));
                line1.endYProperty().bind(this.heightProperty().subtract(10));
                Line line2 = new Line(10,this.getHeight()-10,this.getWidth()-10,10);
                line2.startYProperty().bind(
                        this.heightProperty().subtract(10)
                );
                line1.endXProperty().bind(
                        this.widthProperty().subtract(10)
                );
                //显示固定标记X
                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.centerXProperty().bind(this.widthProperty().divide(2));
                ellipse.centerYProperty().bind(this.heightProperty().divide(2));
                ellipse.radiusXProperty().bind(this.widthProperty().divide(2).subtract(10));
                ellipse.radiusXProperty().bind(this.heightProperty().divide(2).subtract(10));
                ellipse.setStroke(Color.BLACK);
                ellipse.setFill(Color.WHITE);

                getChildren().add(ellipse);
            }
        }

        private void handleMouseClick(){
            //If cell is empty and game is not over
            if(token == ' '&&whoseTurn != ' '){
                setToken(whoseTurn);

                if(isWon(whoseTurn)){
                    lblStatus.setText(whoseTurn+" won! The game is over");
                    whoseTurn =' ';//Game is over
                }
                else if (isFull()){
                    lblStatus.setText("Draw! The game is over");
                    whoseTurn = ' ';
                }
                else {
                    whoseTurn=(whoseTurn=='X')?'O':'X';
                    lblStatus.setText(whoseTurn+"'s turn");
                }
            }
        }
    }
}

开发井字游戏_第1张图片

开发井字游戏_第2张图片

开发井字游戏_第3张图片

你可能感兴趣的:(java,java)