java - 单击菜单栏项目后尝试启动新的 Tic Tac Toe 游戏

标签 java swing user-interface

我有一个用 java GUI 制作的 tic tac toe 游戏,并试图通过让用户单击菜单栏项来实现一种重新启动游戏的方法。

请告诉我还有哪些其他方法可以尝试获得正常重启功能。

到目前为止,我尝试的是在单击菜单栏项时调用 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;
            }
          }
       }

最佳答案

循环将覆盖您尝试实现的重新启动功能。我的建议是,您尝试重构代码以实现事件驱动。为了保持简短:事件驱动编程是响应(并确认)用户输入的用户界面。您可以通过管理游戏回合的类来完成此操作。

这里有一些示例代码供引用,我已将其用于我自己的井字游戏项目(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");
                    }
                });
            }
        }
    }

关于java - 单击菜单栏项目后尝试启动新的 Tic Tac Toe 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57717234/

相关文章:

java - 无法从 JFrame 正确创建 JPanel

java - iText 5.4.2 是开源的吗?

JAVA/JAXB 需要帮助

java - 如何针对不同的形状使用不同的颜色

Java GUI 按钮无法按下

c++ - 在 Windows 窗体中嵌入 GLFW 窗口

java - 具有同一可运行类的不同对象的多个线程可以重叠

java 编译与运行时计算

Java Applet GUI 组件问题

java - 滚动或调整框架大小时,JPanel 中的绘图消失