使程序等待,直到用户在循环中单击按钮

问题描述 投票:-1回答:1

我有一个井字游戏,用户可以在轮到他们时点击他们选择的按钮,然后当计算机轮到时,计算机将使用button.fire()方法点击按钮。但是,代码不等待用户的输入/点击,并且计算机需要多次转动,因此游戏结束时计算机一直在赢。

有没有办法等到用户轮到他们点击按钮?

我尝试了很多东西,并花了很多时间来遵循这个概念并使代码运行。我能够使用计时器,但这仍然允许计算机进行多次移动而无需用户输入。

    while (boardInGame.isNotFull() && !boardInGame.isGameOver()) //continue the game is not over
    {
        if (turn == 'x') /*human player is always set to 'x'*/ {
            switch (computerMove){
                case 0: break;
                case 1: button1.fire();break;
                case 2: button2.fire();break;
                case 3: button3.fire();break;
                case 4: button4.fire();break;
                case 5: button5.fire();break;
                case 6: button6.fire();break;
                case 7: button7.fire();break;
                case 8: button8.fire();break;
                case 9: button9.fire();break;
                default:break;
            }

            if (boardInGame.isValidMove("a1")) {
                button1.setOnAction(new EventHandler<ActionEvent>() {
                                        @Override
                                        public void handle(ActionEvent actionEvent) {
                                            button1.setStyle("--fx-font: 48 Arial");
                                            button1.setText("X");
                                            boardInGame.addMove('x', "a1");
                                            save[myInt] = "a1";
                                            myInt++;
                                        }
                                    }
                )
                ;
                turn = 'o';
            }

            if (boardInGame.isValidMove("b1")) {
                button2.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button2.setStyle("--fx-font: 48 Arial");
                        {
                            button2.setText("X");
                            boardInGame.addMove('x', "b1");
                            save[myInt] = "b1";
                            myInt++;
                        }
                    }
                });
            }

            if (boardInGame.isValidMove("c1")) {
                button3.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button3.setStyle("--fx-font: 48 Arial");
                        {
                            button3.setText("X");
                            boardInGame.addMove('x', "c1");
                            save[myInt] = "c1";
                            myInt++;
                        }
                    }
                });
            }

            if (boardInGame.isValidMove("a2")) {
                button4.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button4.setStyle("--fx-font: 48 Arial");
                        {
                            button4.setText("X");
                            boardInGame.addMove('x', "a2");
                            save[myInt] = "a2";
                            myInt++;
                        }
                    }
                });
            }

            if (boardInGame.isValidMove("b2")) {
                button5.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button5.setStyle("--fx-font: 48 Arial");
                        {
                            button5.setText("X");
                            boardInGame.addMove('x', "b2");
                            save[myInt] = "b2";
                            myInt++;
                        }
                    }
                });
            }

            if (boardInGame.isValidMove("c2")) {
                button6.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button6.setStyle("--fx-font: 48 Arial");
                        {
                            button6.setText("X");
                            boardInGame.addMove('x', "c2");
                            save[myInt] = "c2";
                            myInt++;
                        }
                    }
                });
            }

            if (boardInGame.isValidMove("a3")) {
                button7.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button7.setStyle("--fx-font: 48 Arial");
                        {
                            button7.setText("X");
                            boardInGame.addMove('x', "a3");
                            save[myInt] = "a3";
                            myInt++;
                        }
                    }
                });
            }

            if (boardInGame.isValidMove("b3")) {
                button8.setOnAction(new EventHandler<ActionEvent>() {
                                        @Override
                                        public void handle(ActionEvent actionEvent) {
                                            button8.setStyle("--fx-font: 48 Arial");
                                            {
                                                button8.setText("X");
                                                boardInGame.addMove('x', "b3");
                                                save[myInt] = "b3";
                                                myInt++;
                                            }
                                        }
                                    }
                );
            }

            if (boardInGame.isValidMove("c3")) {
                button9.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        button9.setStyle("--fx-font: 48 Arial");
                        {
                            button9.setText("X");
                            boardInGame.addMove('x', "c3");
                            save[myInt] = "c3";
                            myInt++;
                        }
                    }
                });
            }

            turn = 'o';
                        }

        if (turn == 'o' && !boardInGame.isGameOver()) {
            boolean added = false;
            while (!added) {
                move = player2.getComputerMove();
                if (boardInGame.isValidMove(move)) {
                    switch (move) {
                        case ("a1"): {
                            button1.setStyle("--fx-font: 48 Arial");
                            button1.setText("O");
                            added = boardInGame.addMove('O', "a1");
                            save[myInt] = "a1";
                            myInt++;
                            computerMove=1;
                            break;
                        }

                        case ("b1"): {
                            button2.setText("O");
                            added = boardInGame.addMove('O', "b1");
                            save[myInt] = "b1";
                            myInt++;
                            computerMove=2;
                            break;
                        }

                        case ("c1"): {
                            button3.setText("O");
                            added = boardInGame.addMove('O', "c1");
                            save[myInt] = "c1";
                            myInt++;
                            computerMove=3;
                            break;

                        }
                        case "a2": {
                            button4.setText("O");
                            added = boardInGame.addMove('O', "a2");
                            save[myInt] = "a2";
                            myInt++;
                            computerMove=4;
                            break;
                        }

                        case ("b2"): {
                            button5.setStyle("--fx-font: 48 Arial");
                            button5.setText("O");
                            added = boardInGame.addMove('O', "b2");
                            save[myInt] = "b2";
                            myInt++;computerMove=5;
                            break;
                        }

                        case ("c2"): {
                            button6.setStyle("--fx-font: 48 Arial");
                            button6.setText("O");
                            added = boardInGame.addMove('O', "c2");
                            save[myInt] = "c2";
                            myInt++;
                            computerMove=6;break;
                        }

                        case ("a3"): {
                            button7.setStyle("--fx-font: 48 Arial");
                            button7.setText("O");
                            added = boardInGame.addMove('O', "a3");
                            save[myInt] = "a3";
                            myInt++;
                            computerMove=7;
                            break;
                        }
                        case ("b3"): {
                            button8.setStyle("--fx-font: 48 Arial");

                            button8.setText("O");
                            added = boardInGame.addMove('O', "b3");
                            save[myInt] = "b3";
                            myInt++;
                           computerMove=8;
                            break;
                        }
                        case ("c3"): {
                            button9.setStyle("--fx-font: 48 Arial");
                            button9.setText("O");
                            added = boardInGame.addMove('O', "c3");
                            save[myInt] = "c3";
                            myInt++;
                            computerMove=9;
                            break;
                        }
                        default:
                            added = false;
                    }
                }
            }
        }
    }

    /*print the result once the game is over.*/
    if (boardInGame.isTie()) {
        System.out.println("It's a Tie!");
    }
    if (boardInGame.isWinner('x')) {
        System.out.println("Player #1: 'x' wins!");
    }
    if (boardInGame.isWinner('o')) {
        System.out.println("Player #2: 'o' wins!");
    }
}
javafx mouseevent actionlistener
1个回答
0
投票

也许你可以搬家

switch (computerMove){
    case 0: break;
    case 1: button1.fire();break;
    case 2: button2.fire();break;
    case 3: button3.fire();break;
    case 4: button4.fire();break;
    case 5: button5.fire();break;
    case 6: button6.fire();break;
    case 7: button7.fire();break;
    case 8: button8.fire();break;
    case 9: button9.fire();break;
    default:break;
}

使用单独的方法,并在每次玩家移动时使其运行该方法。

© www.soinside.com 2019 - 2024. All rights reserved.