tags:

views:

125

answers:

3

Hi, I am working on this program where at the end of the game I ask the user if they want to play again. If they say yes, I need to start a new game. I made a restart() method:

public void restart(){
    Game g = new Game();
    g.playGame();
}

However when I call this method some of the values in my program stay at what they were during the previous game.

Is there a game to just clear everything and create an new instance of the game with all the default values?

+7  A: 

Without more information, I'd guess that your problem is likely that you use static variables, the values of which will persist across all instances of a given class. If you make them all into member variables and initialize them in your constructor, it should work.

Pesto
+1  A: 

Verify if those values that don't reset are reset in the Game constructor. Chances are they are not.

Also are those values static? Static values don't reset by the constructor.

Journeyman Programmer
A: 

Do you have any static variables? If yes, it is likely the problem. The value of static variables is maintained for new instances of your class. You can try to remove the static modifier and make sure you initialize all variables in the class constructor.

Daniel H.