I need helping writing this part of my program in c: I have to make a battleship
ID: 3577716 • Letter: I
Question
I need helping writing this part of my program in c:
I have to make a battleship program and I am having trouble placing the ships on the board in the correct spots. It is a 10 by 10 board and there are 5 ships for each player (there are two players). the ships have a length of 5, 4, 3, 3, and 2 and can only be placed horizontal or vertical. I can put these ships on the board without a problem by I can't figure out how to make statements that check if the move is valid. Each ship placed has to have the same x or y co-ordinates in order for them to be horizontal or vertical. I tried doing something like getting the first point on the board where a ship is placed and then if the next move made is not plus or minus a x or y co-ordinate then it is invalid, but could not get it. Can someone write the function (or program) that will check to see if the points entered by the user are valid so that a ship can be placed on the board.
Explanation / Answer
Here I think it will help you
void PlaceShipsOnGameBoard (Cell gameBoard[][COLS], WaterCraft ship[]) {
Coordinate position;
int direction = -1;
int i = 0;
for (i = 0; i < NUM_OF_SHIPS; i++) {
while (TRUE) {
direction = getRandomNumber (0, 1); /* 0 -> horizontal, 1 -> vertical */
position = generatePosition (direction, ship[i].length);
if (isValidLocation (gameBoard, position, direction, ship[i].length)) break;
}
putShipOnGameBoard (gameBoard, ship[i], position, direction);
}
}
This function will place the ship randomly in a board.
****************Thank you*******************