I have a new game item in the menu bar, was wondering if anyone got suggestions on how to accomplish that!
Thanks :)

Your question seems pretty vague to me.
However you could keep a copy of the initial game state somewhere and restore it when the game needs to be restarted.
There are different ways to restore to the initial state of the game, and which one to pick depends on the way you've implemented your game.
(even though they might all work, there still is a difference in efficiency and ease of their implementation, that's the reason for why it depends on your implementation)

Edited 3 Years Ago by mvmalderen

sorry for the vaguness! here is the constructor of the main class, it initilise the other classes, not sure tho if calling that when pressing restart will do it!

 public Game() 
    {
        scenery = new Scenery();
        parser = new Parser();
        String p = "Mustafa";
        logger = new Logger();
        protagonistPlayer = new Player(p, scenery);
        gameInterface = new Interface(scenery, logger, protagonistPlayer, this);
        gameInterface.getFrame().setVisible(true);
        gameInterface.update();      
    }

    thanks!

Well, if you instantiate a Game object with the same initial state as the current game, then you've basically implemented a restart feature.
Don't forget to set your reference to the newly instantiated Game object then.

So, if the instantiation of a Game's initial state [1] done by calling the Game() constructor does not directly or indirectly depend on randomisation for intialising the initial state (or on something else that would make it possible for the initial state to end up differently than the original Game's initial state), then that could be a solution for your problem.

You can also apply object cloning, but you'll need to implement your own clone() method then.

[1] with Game state I mean the state in a Game object or in one of the objects that the Game relies on for it's logic.

Edited 3 Years Ago by mvmalderen

This article has been dead for over six months. Start a new discussion instead.