0

我有一个简单的游戏,其中有一个 JOptionPane 弹出窗口,可以选择是或否。

因此,如果用户点击“否”,那么游戏当然会关闭,但是如果用户点击“是”,我希望游戏重新开始。

我通过以下方式使它起作用:(只需再次调用 main() 方法。main.Main.main(null);

问题 :

它正在开始游戏的另一个实例,但前一个实例也在那里。

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

这是带有 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.;
}

}

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

http://i.imgur.com/7iKJQwu.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 won\nDo you wish to play again?");
                }
                if (newGame.isGameLost() == true)
                {
                    System.out.println("Game has been lost, the silly AI has beaten you\nDo you wish to play again?");
                    newView.popupWinMessage("Game has been lost, the silly AI has beaten you\nDo you wish to play again?");
                }

            }
            else 
            {
                System.out.println("The listener was not attached to a JButton as expected - " + o.getClass());
            }
        }
    });





}
4

2 回答 2

0

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

于 2013-04-19T02:43:20.193 回答
0

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

于 2013-04-19T02:43:23.460 回答