在JavaFX中完成游戏后重新启动应用程序

时间:2018-10-19 12:14:38

标签: java eclipse javafx

我正在做一个作业,我必须创建一个简单的井字游戏。我已经基本完成了所有操作,但应用程序的要求之一是允许玩家再次玩游戏并刷新游戏。我实际上已经尝试了一切,下面发布的代码是我所能使用的最接近的代码。当我这样做时,它会创建一个新窗口,但实际上并不允许我再次玩。

有人知道如何使您的应用程序在JavaFX中重新启动吗?任何帮助表示赞赏!

public class TicTacToe extends Application{

    private boolean gameOver = false;
    private int nFilled = 0;
    private char whoseTurn = 'X'; // 'X' or 'O'
    private Image imageX = new Image("file:x.jpg");
    private Image imageO = new Image("file:o.jpg");
    private Cell[][] cell =  new Cell[3][3];
    private Label statusLabel = new Label("X's turn to play");
    private Stage primaryStage;
    BorderPane borderPane = new BorderPane();


    void startGame(Stage stage, BorderPane bP)
    {

            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.setCenter(pane);
        borderPane.setBottom(statusLabel);

        pane.setStyle("-fx-background-color:#ffffff; -fx-opacity:1;");

        Scene scene = new Scene(borderPane, 600, 600);
        stage.setTitle("TicTacToe");
        stage.setScene(scene);

        stage.show();
    }


    @Override
    public void start(Stage primaryStage) {
                this.primaryStage = primaryStage;
                this.borderPane = new BorderPane();
                startGame(this.primaryStage, this.borderPane);
    }

    public boolean isFull() {
        return nFilled >= 9; // > should never happen
    }

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

    public class Cell extends Pane {

        private char token = ' ';   // one of blank, X, or O

        public Cell() {
            setStyle("-fx-border-color: black"); 
            setPrefSize(200, 200);
            setOnMouseClicked(e -> handleMouseClick());
        }

        public char getToken() {
            return token;
        }

        public void drawX() {   
                if(token == ' ') {
                    ImageView imageV = new ImageView(imageX);
                    imageV.setFitHeight(100);
                    imageV.setFitWidth(100);

                    imageV.setTranslateX(50);
                    imageV.setTranslateY(50);

                getChildren().add(imageV);
                }
        }

        public void drawO() {
                if(token == ' ') {
                    ImageView imageV = new ImageView(imageO);
                    imageV.setFitHeight(100);
                    imageV.setFitWidth(100);

                    imageV.setTranslateX(50);
                    imageV.setTranslateY(50);

                getChildren().add(imageV);
                }
        }

        public void setToken(char c) {
            if (c == 'X')
                drawX();
            else 
                drawO();
            token = c;
            nFilled ++;
        }

        private void handleMouseClick() {
            String s = "";
            if (!gameOver) {
                setToken(whoseTurn);
                if (hasWon(whoseTurn)) {
                    gameOver = true;
                    s = "Congratulations, " + whoseTurn + " wins the game!";
                    Button btnNewGame = new Button("Play Again?");
                    btnNewGame.setOnAction( event ->
                            {
                                Stage pStage = new Stage();
                                start(pStage);
                            });
                    ToolBar toolBar = new ToolBar();
                    toolBar.getItems().addAll( new Separator(), btnNewGame);

                    borderPane.setTop(toolBar);

                    primaryStage.show();
                }
                else if (isFull()) {
                    gameOver = true;
                    s = "Draw!";
                    Button btnNewGame = new Button("Play Again?");
                    btnNewGame.setOnAction( event ->
                            {
                                Stage pStage = new Stage();
                                start(pStage);
                            });

                    ToolBar toolBar = new ToolBar();
                    toolBar.getItems().addAll( new Separator(), btnNewGame);

                    borderPane.setTop(toolBar);

                    primaryStage.show();
                }
                else {
                    whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                    s = whoseTurn + "'s turn";
                }
                statusLabel.setText(s);
            }

        }




    }

    public static void main(String[] args) {
        launch(args);
    }
}

0 个答案:

没有答案