-1

I'm searching on the internet for hours and i cannot find the right answer that works for me.

So.. I made a game called 'seabattle' After playing that game the console comes with the question to play again.

System.out.println("Well done! " + whosturn.getName() + ". A ship down!" );
                        System.out.println("Hurraaaaaayyyyy, All boats down!!");
                        System.out.println("Do you want to play this game again? (y/n) ");
                        input = scanner.next().toLowerCase();
                        if(input == "y"){
                            System.out.println("The game should start again now..");
                            System.out.println("Butttttttt, I don't know how to...");
                        }

                        else{
                            System.out.println("Thank you for player Battleships!");
                            System.out.println("See you next time ;)");
                        }
                    }
                    else{
                        FiredPosition.setDisplayValue('*');
                        System.out.println("Good job, you hit a ship!");
                        switchTurn();
                    }

If the user types y in console. The game should start again.

I read something about Main(args) or something like that.. That didn't work for me because this is another class.

I hope you guys can help me?

Ciaoo

  • 1
    Wouldn't it be simpler to make a new object of your game class and start over again? Rather than restart the process by sending command line parameters? – nunespascal Apr 08 '13 at 11:10

2 Answers2

1

If your main class SeaBattle is currently as this:

public class SeaBattle {
 public static void main(String[] args) {
   ...
 }
}

you could change it the following way:

public class SeaBattle {
 private static String[] args;
 public static void run() {
   //do the same than main() previously
 }
 public static void main(String[] args) {
   SeaBattle.args = args;
   run();
 }
}

Then to relaunch the game,

SeaBattle.run();
Walid
  • 1,262
  • 11
  • 10
1

A simple answer is: Whatever you do to start the game in the first place, do it again when you detect the 'y'.

However, a more in depth answer involves the use of an object to store and update the game state, it is not fully clear from your description whether you are currently doing this. So if it is a battleships type game the object may contain a 2d array for the game area, and perhaps a variable to store whose turn it is. This object would have an 'initialise' method where you randomly select where the ships are in the game area, set the value for whose turn it is, etc.

Your main method would interact with this object, so it would call the game object's initialise method, and after each turn it would call another method (e.g. 'isGameFinished') to check whether the game is over. The isGameFinished method would check if all of the ships have been destroyed and return true if so. If it returns true, then the user would be prompted for a new game. If they enter 'y', then you can either create a completely new object of the game state or just call the game state's initialise method again. You will then have a new game state to begin a new game with.

Matt Hyde
  • 1,572
  • 1
  • 13
  • 17