0

I've been at this Battleship code for weeks on and off and I really have trouble putting multi-cell ships in it. The program works fine as is, but I want to add ships that contain 2-4 cells. I've tried all that I could to no avail, if anybody can give me help that would be really great.

Here's my code, it might be a bit confusing:

import java.util.Random;
import java.util.Scanner;

public class Battleship
{
    private static int counter = 0;
    private static boolean flag = true;

    public static void main(String[] args)
    {
        int[][] boardP1 = new int[10][10];
        int[][] boardP2 = new int[10][10];
        int[][] shipsP1 = new int[10][2];
        int[][] shipsP2 = new int[10][2];
        int[] shootP1 = new int[2];
        int[] shootP2 = new int[2];
        int shotHitP1 = 0, shotHitP2 = 0;

        Scanner userInput = new Scanner(System.in);
        String p1name = null, p2name = null;

        // Welcome message
        System.out.println("\033[2J\033[10;28f WELCOME TO BATTLESHIPS!");
        System.out.print("\033[13;31f Player 1: ");
        p1name = userInput.nextLine();
        System.out.print("\033[15;31f Player 2: ");
        p2name = userInput.nextLine();      

        // Clear screen
        System.out.println("\033[2J");

        // Initialize boards (random)
        initBoardsP1(boardP1);
        initBoardsP2(boardP2);
        // Initialize ships (random)
        initShips(shipsP1);
        initShips(shipsP2);                     

        do
        {   // Display boards
            showBoardP1(boardP1);
            showBoardP2(boardP2);

            // P1 ask for shot
            shootP1(shootP1);
            counter++;

            if (hit(shootP2,shipsP2))
            {
                shotHitP1++;

                if (shotHitP1 == 5)
                { System.out.println("\n\n\n" +p1name+ " has won the game!");
                  System.out.println(); }
            }            
            else
            { System.out.println("\033[48;36f Miss!"); }

            changeboardP2(shootP1, shipsP1, boardP1);
            System.out.print("\033[2J");

            // P2 Ask for shot
            showBoardP1(boardP1);
            showBoardP2(boardP2);
            shootP2(shootP2);
            counter++;

            if (hit(shootP1,shipsP1))
            {
                shotHitP2++;

                if (shotHitP2 == 5)
                { System.out.println("\n\n\n" +p2name+ " has won the game!");
                  System.out.println(); }
            }            
            else
            { System.out.println();
              System.out.println("You missed!");
              System.out.println(); }

            changeboardP1(shootP2, shipsP2, boardP2);
            System.out.print("\033[2J");
        } while (shotHitP1 != 5 || shotHitP2 != 5);
    }

    public static void initShips(int[][] ships)
    {
        Random random = new Random();

        for (int ship = 0; ship < 10; ship++)
        {
            ships[ship][0] = random.nextInt(10); // Draws row coordinate
            ships[ship][1] = random.nextInt(10); // Draws column coordinate


            // Check to see if already used combo
            for (int last = 0; last < ship; last++)
            {
                if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))
                    do
                    { ships[ship][0] = random.nextInt(10);
                      ships[ship][1] = random.nextInt(10); }
                    while((ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]));
            }
        }
    }

    public static void initBoardsP1(int[][] boardP1)
    { for (int row = 0; row < 10; row++)
        for (int column = 0; column < 10; column++)
            boardP1[row][column] = -1; }

    public static void initBoardsP2(int[][] boardP2)
    { for (int row = 0; row < 10; row++)
        for (int column = 0; column < 10; column++)
            boardP2[row][column] = -1; }

    public static void showBoardP1(int[][] boardP1)
    {
        if (flag = true)
        { System.out.println("\033[9;15f   PLAYER 2");
          System.out.println("\033[12;5f   1  2  3  4  5  6  7  8  9  10");
          System.out.println("\033[13;4f   +-++-++-++-++-++-++-++-++-++-+"); }
        else
        { System.out.println("\033[9;54f   PLAYER 1");
          System.out.println("\033[12;45f  1  2  3  4  5  6  7  8  9  10");
          System.out.println("\033[13;44f  +-++-++-++-++-++-++-++-++-++-+"); }

        for (int row = 0; row < 10; row++)
        {       
            if (flag = true)
            { System.out.print("\033[1C"); }
            else
            { System.out.print("\033[40C"); }

            if (row <= 8)
            { System.out.print("   " +(row+1)+ " "); }
            else
            { System.out.print("  10 "); }

            for (int column = 0; column < 10; column++)
            {
                System.out.print("|");
                if (boardP1[row][column] == -1)
                { System.out.print(" "); }
                else if (boardP1[row][column] == 0)
                { System.out.print("0"); }
                else if (boardP1[row][column] == 1)
                { System.out.print("\033[1;31mX\033[0m"); }   
                System.out.print("|");
            }

            if (flag = true)
            { System.out.println();
              System.out.println("      +-++-++-++-++-++-++-++-++-++-+"); }
            else
            { System.out.println();
              System.out.println("\033[40C     +-++-++-++-++-++-++-++-++-++-+"); }

            if ((counter % 100) == 0)
            { flag = true; }
            else
            { flag = false; }
        }
    }

    public static void showBoardP2(int[][] boardP2)
    {
        if (flag = true)
        { System.out.println("\033[9;54f   PLAYER 1");
          System.out.println("\033[12;45f  1  2  3  4  5  6  7  8  9  10");
          System.out.println("\033[13;44f  +-++-++-++-++-++-++-++-++-++-+"); }
        else
        { System.out.println("\033[9;15f   PLAYER 2");
          System.out.println("\033[12;5f   1  2  3  4  5  6  7  8  9  10");
          System.out.println("\033[13;4f   +-++-++-++-++-++-++-++-++-++-+"); }

        for (int row = 0; row < 10; row++)
        {   
            if (flag = true)
            { System.out.print("\033[40C"); }
            else
            { System.out.print("\033[1C"); }

            if (row <= 8)
            { System.out.print("   " +(row+1)+ " "); }
            else
            { System.out.print("  10 "); }

            for (int column = 0; column < 10; column++)
            {
                System.out.print("|");
                if (boardP2[row][column] == -1)
                { System.out.print(" "); }
                else if (boardP2[row][column] == 0)
                { System.out.print("0"); }
                else if (boardP2[row][column] == 1)
                { System.out.print("\033[1;31mX\033[0m"); }   
                System.out.print("|");
            }

            if (flag = true)
            { System.out.println();
              System.out.println("\033[40C     +-++-++-++-++-++-++-++-++-++-+"); }
            else
            { System.out.println();
              System.out.println("      +-++-++-++-++-++-++-++-++-++-+"); }

            if ((counter % 100) == 0)
            { flag = true; }
            else
            { flag = false; }
        }
    }

    public static void shootP1(int[] shootP1)
    {
        Scanner userInput = new Scanner(System.in);

        System.out.println("\033[38;26f ----- PLAYER ONE'S SHOT -----");
        System.out.println("\033[40;27f (Legend: 0 - Miss, \033[1;31mX\033[0m - Hit)");
        System.out.println("\033[55;21f Press any non-integer character to quit.");

        System.out.print("\033[44;35f Row: ");
        shootP1[0] = userInput.nextInt();
        shootP1[0]--;

        System.out.print("\033[45;32f Column: ");
        shootP1[1] = userInput.nextInt();
        shootP1[1]--;
    }

    public static void shootP2(int[] shootP2)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("\033[38;26f ----- PLAYER TWO'S SHOT -----");
        System.out.println("\033[40;27f (Legend: 0 - Miss, \033[1;31mX\033[0m - Hit)");
        System.out.println("\033[55;21f Press any non-integer character to quit.");

        System.out.print("\033[44;35f Row: ");
        shootP2[0] = input.nextInt();
        shootP2[0]--;
        System.out.print("\033[45;32f Column: ");
        shootP2[1] = input.nextInt();
        shootP2[1]--;    
    }

    public static boolean hit(int[] shoot, int[][] ships)
    {
        for (int ship = 0; ship < ships.length; ship++)
        {
            if (shoot[0] == ships[ship][0] && shoot[1] == ships[ship][1])
            {
                System.out.println("\033[48;34f KABOOM!!");
                System.out.printf("\033[50;23f You hit a ship located in (%d,%d)!\n\n", shoot[0]+1, shoot[1]+1);
                return true;
            }
        }

        return false;
    }

    public static void changeboardP1(int[] shoot, int[][] ships, int[][] boardP1)
    {
        if (hit(shoot,ships))
        { boardP1[shoot[0]][shoot[1]] = 1; }
        else
        { boardP1[shoot[0]][shoot[1]] = 0; }
    }

    public static void changeboardP2(int[] shoot, int[][] ships, int[][] boardP2)
    {
        if (hit(shoot,ships))
        { boardP2[shoot[0]][shoot[1]] = 1; }
        else
        { boardP2[shoot[0]][shoot[1]] = 0; }
    }
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
KTF
  • 317
  • 3
  • 7
  • 13
  • It is a bit confusing indeed. Try to help us by giving some more details. What exactly do you want the program to do in what specific situation? What does it do instead? – reto Dec 04 '13 at 23:10
  • Right now, if you run the program, it will generate a 10x10 board with 10 1-cell ships placed randomly in it. What I need help with is instead of 1-cell ships, I want to place 2-cell up to 4-cell ships in it if possible. The ships are generated through the initShips(). – KTF Dec 04 '13 at 23:13
  • So your actual question is roughly how to find (2 or 4) connected free cells within a 2D array with potentially some other restrictions. Or is it "how do I represent a 2 cells in an array as 1 entity"? – zapl Dec 04 '13 at 23:20
  • 1
    Why not make a `Ship` class that holds the start and end coordinates of itself? – Blub Dec 04 '13 at 23:20
  • @zapl the latter. But if I have to use the former, I'll do it. – KTF Dec 04 '13 at 23:30

1 Answers1

0

I don't understand the board/ship arrays in your code.
What are the extra arrays for ship for?

I suggest you make 1 array for each player's board filled with 0's at creation by default, and then add the boats by filling in 1's in the boats locations? and possibly later on in the game 2's for missed points and 3 for hits.

The only extra array you will need will be a 2D array containing 5 rows for the 5 ships and 2 columns specifying initial and ending coordinate of the ship that may later on be used to check if the entire ship has been destroyed or not.

ThaBomb
  • 702
  • 6
  • 11