Search code examples
javaarraysbluej

Implementing a second ship into one dimensional battleship game Java


I have designed the battleship game to only have one ship hidden and now I have to implement another ship into the game. I am new to Java and was wondering if anybody could offer me a simple way to go about this. Do I need to create another method or just adjust the display river method?

        public static void displayRiver(int[] river, boolean showShip) {
            System.out.println();
            System.out.print("|");
            for (int val : river) {
                switch (val) {
                    case -1: // No Ship
                    System.out.print("x");
                    break;
                    case 0: // Unknown
                    System.out.print(" ");
                    break;
                    case 1: // Ship Found
                    System.out.print(showShip ? "Y" : " ");
                    break;
                }//switch
                System.out.print("|");
            }//for
            System.out.println();
            System.out.println();
        }//displayRiver
    
        // main method
        public static void main(String[] arg) {
            int riverLength = promptForInt("Please, enter the river lenght"); 
            int [] shipArray = new int[riverLength]; 
         
            int randomBattleshipLocation = new Random().nextInt(riverLength); 
            shipArray[randomBattleshipLocation] = 1; 
         
            boolean showShip = false ; 
            int userGuess; 
    
            do
            {
                displayRiver (shipArray, false);
                userGuess = promptForInt(String.format("Guess, enter a location from 1 to " + riverLength));
                userGuess = userGuess -1; 
    
                if(shipArray[userGuess] == 1) 
                {
                    System.out.println("Boom! ");
                    showShip = true; 
                    displayRiver(shipArray, true);
                }
                else if(shipArray[userGuess] == -1)
                {
                    System.out.println("Location was already hit, try again! ");
                }
                else if(shipArray[userGuess] == 0) 
                {
                    System.out.println("Splash...");
                    shipArray[userGuess] = -1 ; 
                }
    
            } while(!showShip); 
    
            System.exit(0);
    
        }
    }

Solution

  • Your logic seems to be that an 1 in the array indicates a ship, and your ships apparently are never more than one in width.

    You currently use the following to create one ship

    int randomBattleshipLocation = new Random().nextInt(riverLength); 
    shipArray[randomBattleshipLocation] = 1;
    

    So you could turn that into a method that creates a battleship, then call that as many times as you want for multiple ships. Just make sure that you don't assign a ship on top of another ship, or make another logical error (like trying to put 5 ships into a river of size 4, and it will loop forever trying to find space for ships).

    Pseudo-code and not-so-pseudo-code:

    for(int i = 0;i < shipsToAdd; i++) {
        addShip(shipArray);
    }
    
    // Use a shared class-level Random object, don't do new Random().nextInt();
    private static Random rnd = new Random();
    private static addShip(int[] array) {
        // Here you should loop to check if the array is already full of ships
        // otherwise it's a design flaw that will result in an infinite loop with bad input
    
        // loop until we find a shipless array index
        int index = rnd.nextInt(array);
        while(array[index] == 1)
           index = rnd.nextInt(array);
        array[index] = 1;
    }