-2

I was hoping to get some help. I have a java game of battleship and wanted to give the player the option to play again when they finished, but don't really know how. Any help will be great!

public class Battle {

public static void main(String[] args) throws IOException{

    int[][] board = new int[5][5];
    int[][] ships = new int[3][2];
    int[] shoot = new int[2];
    int size;//for the fleet, under construction!
    int health= 3;
    int attempts=0,
        shotHit=0;
    boolean sendout = false;

    //reads the rules

    File myFile = new File ("Rules.txt");
    Scanner inputFile = new Scanner(myFile);

    while (inputFile.hasNext())
        {
            String str = inputFile.nextLine();
            System.out.println(str);
        }
    inputFile.close();


    Scanner position = new Scanner(System.in);

    whatShip ship = new whatShip(health);
    health = ship.WhatShip(health);



    System.out.println("\n");
    System.out.println("Enter the name of your vessel:");
    String name = position.nextLine();

    //intro read

    System.out.println("\n");
    System.out.println("INTRO");
    System.out.println("\n");
    System.out.println("Onbard the "+name);
    File myFile1 = new File ("Intro.txt");
    Scanner inputFile1 = new Scanner(myFile1);

    while (inputFile1.hasNext())
    {
        String str = inputFile1.nextLine();
        System.out.println(str);
    }
    inputFile1.close();
    System.out.println("\n");



    int row = entryIntMinMax("Enter "+name+"'s longitude, row", 1, 5);





    int col = entryIntMinMax("Enter "+name+"'s latitude, column",1,5);





    System.out.println("ALL HANDS, MAN YOUR STATIONS");

    initBoard(board);
    initShips(ships);

    System.out.println();

    do{
        showBoard(board);
        shoot(shoot);
        attempts++;

        if(hit(shoot,ships)){
            hint(shoot,ships,attempts);
            shotHit++;
        }                
        else
            hint(shoot,ships,attempts);
        health =enemyShoot(row, col, health);
        health =enemyShoot(row, col, health);
        health =enemyShoot(row, col, health);
        health =enemyShoot(row, col, health);
        health =enemyShoot(row, col, health);
        health =enemyShoot(row, col, health);
        health =enemyShoot(row, col, health);

        changeboard(shoot, ships, board);


    }


    while(shotHit!=3);

    System.out.println("\n\n Enemy fleet destroyed sir! You sunk 3 ships in "+attempts+" attempts");
    showBoard(board);
}

Here is my main method. Any criticism would also be great!

  • 4
    i´d start by not writing everything into a big single `main` method, but by creating `Object`'s and using `methods` and `variables` to start, pause, end. restart games or set a status for whatever you like. Calling `main` somewhere at some point just to restart the whole process is probably not what you want but have to do currently. – SomeJavaGuy Dec 15 '16 at 08:03
  • 1
    @KevinEsche Yes i'm working on upgrading it and moving everything into methods, the main used to be over 150 lines of code. But i don't have the technical know how to make a method that restarts the game. – ChunkierLizard Dec 15 '16 at 08:06
  • once the game is finished, you can pop a question asking for a next round. if the answer is yes, you can call `main(args);` to run it all again and if the answer is no then simply end it. if you want to work with methods, run the game in a loop and at the end call the method if they want to restart. in that method you set some condition and depending on the answer, the loop continues or not – XtremeBaumer Dec 15 '16 at 08:06
  • @XtremeBaumer You can call the main method like that? – ChunkierLizard Dec 15 '16 at 08:07
  • 1
    @ChunkierLizard yes, but don´t do it. It´s not the optimal solution, it should be working but that´s not what you would want it to look like in the end. – SomeJavaGuy Dec 15 '16 at 08:07
  • @KevinEsche it is what he wants, but not the best solution for it – XtremeBaumer Dec 15 '16 at 08:08
  • @XtremeBaumer That seems perfectly logical, thank you I will do that! – ChunkierLizard Dec 15 '16 at 08:09
  • you should only do it in the beginning and later on move towards methods – XtremeBaumer Dec 15 '16 at 08:11
  • @XtremeBaumer If I have time I will certainly move this into methods, finals are taking up a lot of my time and I will defiantly optimize it if I have the time. – ChunkierLizard Dec 15 '16 at 08:13

2 Answers2

0

First, put (almost) everthing in the first section of the above code (until the do-while statement) in a introPhase() method or something.

Secondly, put your do-while in a second gamePhase() method or something.

Finally, surround both with a conditional loop statement of some sort.

You should end up with something like this:

boolean finishFlag = false;
while(!finishFlag) {
    introPhase();
    gamePhase();
    finishFlag = getExitInfoFromUser();
}

Good luck!

MordechayS
  • 1,552
  • 2
  • 19
  • 29
0

Heres another more abstract and simple example on how it could look like.

Define classes, method and variables in order to make it more easy to work with the things you are programming.

// Game class, here you define what the game would look like
public class Game {
    private Scanner scanner = new Scanner(System.in);
    // Could probably create a class Board and work on this class.
    // private Board board;
    private int[][] board;
    // Could probably create a class Ship and work on this class.
    // Could also include the two dimensional array in the Board class
    // private Ship[][] ships board;
    private int[][] ships;
    // Could probably create a class Player and work on this class.
    // private Player board;
    private int attemtps;
    private int health;

    // Define a status the game is currently in
    private enum Status {
        IDLE, STARTED, ENDED;
    }

    // Variable for the current game status
    private Status gamestatus;

    // Nothing done, default is idle when new game class is initialized
    // Maybe you have options and stuff and a menu, where you IDLE at.
    public Game() {
        gamestatus = Status.IDLE;
    }

    // This method "starts" the game, though defines variables to 
    // have Game beeing started
    public void start() {
        System.out.println("Welcome to battleship");
        // Change status, maybe you want to validate the current gamestatus 
        // at some point
        gamestatus = Status.STARTED;
        // this method could initialize some variables and stuff to default values in order to represent a fresh game
        setInitialValuesOfGame();
        // Random loop that gets 5 inputs and emulates a game due to this.
        int i = 0;
        while(gamestatus.equals(Status.STARTED)) {
            System.out.println("Test input: ");
            String input = scanner.nextLine();
            // Just make it look like there would happen something
            board[i%5][0] = i;
            ships[i%3][0] = i;
            // Make game look like it did end after a few iteration
            ++i;
            if(i == 5) {
               end();
            }
        }
        // Loop is over, ask for restart
        restart();
    }

    public void restart() {
        System.out.println("Hey would you like to restart: yes"); // Making it look like input
        String input = "yes"; // Input here, but make it a literal for showing purposes
        if (input.equals("yes")) {
            start();
        } else {
            System.out.println("bye bye");
        }
    }

    public void end() {
        gamestatus = Status.ENDED;
        // Do more stuff that would make the game end
    }

    // Setting default values
    private void setInitialValuesOfGame() {
        board = new int[5][5];
        ships = new int[3][2];
        attemtps = 0;
        health = 3;
    }

    // This is just your starting point, i´d programm as less as possible in here.
    public static void main(String[] args) {
        Game game = new Game();
        game.start();
    }
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33