I need help writing code for my latest C++ assignment. I have to be able to place battleships randomly on a 10x10 grid. Two of the ships need to be horizontal (++++++) and two need to be vertical (++
++
++)
Below, I have included what code I have written already. What I cant figure out is how to have the ships randomly placed on the grid that I created using nested FOR loops. To achieve this, I am supposed to use a seed for the RAND function, but I am just not sure how to use that in conjunction with the ship placement. The ships can fall off the grid, I'm allowed to re-do the seed and RAND function to correct for that.
-Should I have used an array to make the map/grid? I am now thinking that doing that would make the random ship placement much easier for me, but I'm not sure how to use the array, much less how to print it out onto the screen.
I think a two-dimentional array would be a great idea.
To fill the grid, obtain a random number such that the number becomes a valid index into the grid (that way your ship won't "fall off" of the grid. I suggest that it might be useful to think of the random number as one end (left, or right, or top, or bottom) of a ship. Once you have the first end, fill the rest of the grid with plus signs for the ship. For horizontal ships, the number must be an index that can extend the ship out either to the left or right. The grid spaces for the ship must be empty (i.e., not be occupied by another ship.)
This is for a horizontal ship:
#define COLUMNS 10
#define ROWS 10
int grid[ ROWS ][ COLUMNS ];
while:
get a random number
if random number is not valid
continuewhileif there is room to the right forthis ship
fill in the rest of the grid forthis ship
break out of while
end ifif there is room to the left forthis ship
fill in the rest of the grid to the left forthis ship
break out of while
end if
end while
No need to make the array 2 dimensional, that will only make it a pain to write functions for it. You can easily pretend to have a 2 dimensional array of the size ROWS * COLUMNS with grid[row*COLUMNS+column];.
The question is - when would a random number be not valid? I don't quite understand that part.
The random number might be not valid if it causes a piece of the ship to be clipped "off screen" maybe? I know I recently started a battleship type game and that issue came up for me. I was mapping the ships based on the far left or bottom of the spaces it takes up and sometimes it would do this, I simply limited the space where it could be put, but this was a poor work around and I scrapped the project to start again later.
Ok, so I went to my professor and got some help. Now my program is a bit more organized, but i cant get the ships to print onto the grid that I created. Should I use a FOR loop to print it onto the grid? and what should I do about variables? I use X and Y a LOT now, and I wonder if that is the reason I am having trouble printing to the screen. What I need to happen is for 2 horizontal and 2 vertical ships to be randomly placed randomly in the 10x10 grid. I have to use the RAND function to place them, and I have to be able to label each coordinate as SHIP, HIT, MISS, or FREE. For now, all I need to be able to do is place the ships on the grid and print them out on the grid. To do that, the program needs to declare the state of each coordinate as SHIP, HIT, MISS, or FREE and print out an appropriate character to represent that. As it is, it wont print anything other than the grid. [note that at this point in the assignment, I only need to worry about FREE or SHIP]. Can any of you give me some advice on how to correctly print the ships? Here is my code: