基于JOptionPane的选择重新启动java游戏



我有一个简单的游戏,我有一个JOptionPane弹出给选择是或否。

所以如果用户点击NO,那么游戏当然会关闭,但是如果用户点击YES,我希望游戏重新开始。

我已经通过以下方式使其工作:(只需再次调用main()方法。main.Main.main(空);

问题:

它正在启动游戏的另一个实例,但是之前的实例也在那里。

如何在启动新实例之前关闭前一个实例?或者还有什么其他方法可以绕过这个?

下面是带有JOptionPane 的类代码
    public void popupWinMessage(String message)
{
     //default icon, custom title
    int n = JOptionPane.showConfirmDialog(
        null,
        message,
        "",
        JOptionPane.YES_NO_OPTION);
    if(n == JOptionPane.YES_OPTION){
        JOptionPane.showMessageDialog(null, "Alright, here we go again");
        main.Main.main(null);
    }
    else {
        JOptionPane.showMessageDialog(null, "Thanks for playing the battleships game");
        System.exit(0);
    }

这是我更新的主方法(我仍然很困惑发生了什么):

package main;
imports
public class Main {
public Controller theController = new Controller(new Game(), new Frame());
/**
 * @param args
 */
public static void main(String[] args) 
{
    newGame();
}

public static void newGame()
{
    theController.;
}

}

我的整个游戏目录是这样的

https://i.stack.imgur.com/kze94.png

JOption窗格所在的方法位于视图的Frame类中。

public Controller(final Game newGame, final Frame newView) 
{
    this.game = newGame;
    this.view = newView;
    this.view.setVisible(true);

    //Gets the board arrays into these local variables here for us to use
    playerBoard = newGame.DisplayPlayerBoard();
    computerBoard = newGame.DisplayCompBoard();

    colourGrids();
    this.view.addGridActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            Object o = ae.getSource();
            if(o instanceof JButton) 
            {
                JButton btn = (JButton) o;
                int xValue = (Integer) btn.getClientProperty("row");
                int yValue =  (Integer) btn.getClientProperty("column");
                int tempShipType = computerBoard[xValue][yValue];
                newGame.tryHitComputer(xValue, yValue);
                btn.setEnabled(false);

                if(tempShipType != -1)
                {
                   if(newGame.checkShipStatus(tempShipType) == true)
                      {
                         System.out.println(shipDowns[tempShipType]);
                         newView.popupMessage(shipDowns[tempShipType]);
                      }
                }
                //Code here for the computer to try hit your ships
                newGame.tryHitPlayer();

                colourGrids();
                System.out.println(newGame.isGameWon());
                if (newGame.isGameWon() == true)
                {
                    System.out.println("Game has been won, do something to stop it at some point");
                    newView.popupWinMessage("Game has been wonnDo you wish to play again?");
                }
                if (newGame.isGameLost() == true)
                {
                    System.out.println("Game has been lost, the silly AI has beaten younDo you wish to play again?");
                    newView.popupWinMessage("Game has been lost, the silly AI has beaten younDo you wish to play again?");
                }
            }
            else 
            {
                System.out.println("The listener was not attached to a JButton as expected - " + o.getClass());
            }
        }
    });


}

不要再调用main方法了。有一个start()方法来启动你的游戏,并让对话框在答案是"是"时调用它

一种选择是将restartGame()方法添加到Controller类中。由于您从Controller构造函数调用popupWinMessage(),您可以将其修改为接受Controller对象并将this引用传递给它。然后popupWinMessage()方法可以简单地调用传递给它的实例上的restartGame()方法。

相关内容

  • 没有找到相关文章

最新更新