单击菜单栏项后尝试启动新的井字游戏

时间:2019-08-29 20:17:02

标签: java swing user-interface

我有一个用Java GUI制作的井字游戏,并试图通过让用户单击菜单栏项来实现重启游戏的方法。

请让我知道我可以尝试使用其他什么方式来获得正常的重启功能。

到目前为止,我尝试的是单击菜单栏项时调用playGame()方法,但由于playGame方法中的while循环使其卡住而无法正常工作。


public TicTacToe()
       {
           /* Create JFrame and set up the size and visibility */
           frame = new JFrame("Tic Tac Toe");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           /* Create menu bar*/
           JMenuBar menubar = new JMenuBar();
           JMenu game = new JMenu("Game");
           newGame = new JMenuItem("New Game");
           quit = new JMenuItem("Quit");

           newGame.addActionListener(new ActionListener() 
           {
               public void actionPerformed(ActionEvent evt) {
                   newGameActionPerformed(evt);
                }
           });

           quit.addActionListener(new ActionListener() 
           {
               public void actionPerformed(ActionEvent evt) {
                   quitActionPerformed(evt);
                }
           });

           game.add(newGame);
           game.add(quit);
           menubar.add(game);
           frame.setJMenuBar(menubar);

           /* Add text area to frame. */
           frame.add(scrollableTextArea);
           frame.setLocationRelativeTo(null);
           frame.setSize(400,400);
           frame.setVisible(false); 
       }

       private void quitActionPerformed(ActionEvent evt){
           System.exit(0);
       }

       private void newGameActionPerformed(ActionEvent evt) {
           clearBoard();
       }
public void playGame()
       {
          frame.setVisible(true); 
          clearBoard(); // clear the board
          // loop until the game ends
          while (winner==EMPTY) { // game still in progress
             while (playerWent == false){
                if (topRight.getModel().isPressed()){
                    topRight.setText(player);
                    topRight.setEnabled(false);
                    playerWent = true;
                 } 
                 else if (topMid.getModel().isPressed()){
                    topMid.setText(player);
                    topMid.setEnabled(false);
                    playerWent = true;
                    }
                 else if (topLeft.getModel().isPressed()){
                    topLeft.setText(player);
                    topLeft.setEnabled(false);
                    playerWent = true;
                    }
                 else if (midRight.getModel().isPressed()){
                    midRight.setText(player);
                    midRight.setEnabled(false);
                    playerWent = true;
                    }
                 else if (center.getModel().isPressed()){
                    center.setText(player);
                    center.setEnabled(false);
                    playerWent = true;
                    }
                 else if (midLeft.getModel().isPressed()){
                    midLeft.setText(player);
                    midLeft.setEnabled(false);
                    playerWent = true;
                    }
                 else if (botRight.getModel().isPressed()){
                    botRight.setText(player);
                    botRight.setEnabled(false);
                    playerWent = true;
                    }
                 else if (botMid.getModel().isPressed()){
                    botMid.setText(player);
                    botMid.setEnabled(false);
                    playerWent = true;
                    }
                 else if (botLeft.getModel().isPressed()){
                    botLeft.setText(player);
                    botLeft.setEnabled(false);
                    playerWent = true;
                    }  
             } 
             numFreeSquares--;            // decrement number of free squares

             // see if the game is over
             if (haveWinner()) {
                winner = player; // must be the player who just went
                if (winner.equals(PLAYER_X)){
                    gameState.setText("Player X wins");
                    return;
                } else {
                    gameState.setText("Player O wins");
                    return;
                }
            }
             else if (numFreeSquares==0) {
                winner = TIE; // board is full so it's a tie
                gameState.setText("Tie game");
                return;
                }


             // change to other player (this won't do anything if game has ended)
             if (player==PLAYER_X) {
                player=PLAYER_O;
                gameState.setText("Game in progress, Player O's turn");
                playerWent = false;
            }
             else {
                player=PLAYER_X;
                gameState.setText("Game in progress, Player X's turn");
                playerWent = false;
            }
          }
       }

1 个答案:

答案 0 :(得分:0)

该循环将覆盖您尝试实现的重启功能。我的建议是您尝试重构代码以使事件驱动。为了简单明了:事件驱动的编程是用户界面响应(并确认)用户的输入。您可以通过管理游戏回合的类来做到这一点。

这里有一些示例代码可供参考,我已将其用于自己的tic-tac-toe项目(JavaFX框架):

 private void playersGameProcess() {
        //Looping through the button field to add mouseclick events, if there is pressed on a button the game's state switches between CROSS and NOUGHT.
        for (int i = 0; i < buttons2D.length; i++) {
            for (int j = 0; j < buttons2D[i].length; j++) {
                Button button = buttons2D[i][j];
                buttons2D[i][j].setOnMouseClicked(event -> {
                    if (game.getGameState() == "X" && button.getId() == null) {
                        game.incrementTurnCount();
                        System.out.println("Turn: " + game.getTurnCount());
                        notification.setText("It's nought's turn!");
                        game.setGameState("O");
                        button.setId("buttonClickX");
                        checkWinningConditions("X");
                    } else if (game.getGameState() == "O" && button.getId() == null) {
                        game.incrementTurnCount();
                        System.out.println("Turn: " + game.getTurnCount());
                        notification.setText("It's cross's turn!");
                        button.setId("buttonClickO");
                        game.setGameState("X");
                        checkWinningConditions("O");
                    }
                });
            }
        }
    }