Need Help Programming Battleship Location Selector/Checker

Joe picture Joe · Apr 11, 2011 · Viewed 10.8k times · Source

I am trying to develop a 1-sided Battleship game and I have nearly everything set up. I only need to incorporate an array which holds at this time, 5, Ships objects. The class I created for each ship is called Ships.java. Earlier I was having problems initializing the array but that has been settled.

The problem arises when I try to pull in the length of a ship (2, 3, 4 or 5) from an index in the array. I'm not sure how to conceptually go about placing ships.

I feel like I've tried every combination of do-whiles, for loops and if statements. Even tried a switch-case.

The goal is to have the computer select positions for the five ships and set each cell in a grid (ROWSxCOLS) to be equal to NC_SHIP (not clicked, plus ship). The problem is making it check the positions of cells adjacent to a random location on the grid. Also has to check if the ship in question will fit (pulling from the ships[i].getShipLength()).

Here is the code I have so far:

int shipsPlaced = 0;

    for (int i = 0; i < ships.length; i++)
    {
        boolean shipPlaced = false;

        do
        {
            int randomRow = (int)(Math.random()*ROWS);
            int randomCol = (int)(Math.random()*COLS);
            int p = 0;

            if (randomRow - ships[p].getShipLength() >= 0 && gameBoard[(randomRow - p)][randomCol] == NC_EMPTY)
            {
                for (int x = 0; x < ships[x].getShipLength(); x++)
                {
                    gameBoard[(randomRow - x)][randomCol] = NC_SHIP;
                    shipsPlaced = shipsPlaced + 1;
                    if (x == ships[x].getShipLength())
                    {
                        shipPlaced = true;
                        p = p + 1;
                    }
                }
            }
        }while (shipPlaced == false);

    }

Everything has been initialized and set if it's not visible here. The problem is about the math/logic used to place the ships in 'random' locations.

Answer

apacay picture apacay · Apr 11, 2011

First of all: all your ships will go horizontal, you should also randomize the placement direction of the ship.

There are two ways I would face that problem:

  1. First fit initial pos then look if ship fits.
  2. First list all the available positions, then randomize for a number equal to the list length

1 - Make a recursive look for random initial position (x,y) (which should be free, if not re-throw a position). In the recursive "lookForPos" method, make a randomPlacementDirection, and from that, a flag (eg isHorizontal). If it doesn't fit (length from start to final position overflows size of the matrix), re-throw. Cover the positions (position, position+1, position+2, ..., position+n) where 'n' is the length of your ship and position is the x,y pair and the +1 affects only one of the cardinals (depending if isHorizontal or not) if any of them is also occupied re-throw. Eventually you'll have what you need.

2 - Make a list of all the positions where it fits (a 'for' structure), both horizontal and vertical, then randomize for the length of the list.