Need Help Programming Battleship Location Selector/Checker

11,233

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.

Share:
11,233
Admin
Author by

Admin

Updated on June 08, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • apacay
    apacay about 13 years
    I also like MarcoS's idea, except when he states that there are 4 directions. I feel like that is overcoding... Still, it works. Optimizing this I see two possible directions, horizontal and vertical and I saved the code done for the other two cardinals.
  • MarcoS
    MarcoS about 13 years
    if you consider only two directions (horizontal and vertical) starting from a random cell at row r and column c, then you're still left with the problem of how to place your ship. For example you may decided that (r,c) is the cell at the middle of your ship, or the one at the left-most side, or the upper side ... if you consider 4 directions, then when you have the random cell (r,c) and a direction you know exactly how to place your ship :)
  • apacay
    apacay about 13 years
    But if you consider always left to right and up to down as default (and only) directions, how to place your ship is not a problem, because you know you are in your initial position (start position to the ship placement) and from there, depending on the isHorizontal or isVertical flag, you know where to go end position. (eg randShipFreeXY() returns [2,3] randPlacement() returns Horizontal, and your ship size is 3 then your start pos is [2,3] your end pos is [5,3], and you have to check, first if it fits in the gameBoard, and then if [3,3]; [4,3] and [5;3] are ship free).