battleship.h

1.Create a C header file
2.Be sure header file is stored in same directory as battleship.c
3.Move the following from battleship.c to battleship.h
a.All #define macros
b.All structs
c.All function prototypes

battleship.c

4.Update the start of the preprocessor commands to do the following
a.Include C library ctype.h
b.Include C library time.h
c.After the #include of the C libraries and before the first function, include the battleship.h header file

5.Update function main to do the following
a.After variable declarations add function call srand() passing argument time(0)
b.Update the switch() to do the following
i.For case MANUAL call function manualShipPlace() passing playerOne as a pointer
ii.For case RANDOM call function randomShipPlace () passing playerOne as a pointer
c.After the switch(), call function randomShipPlace () passing playerTwo as a pointer
d.Call function displayGameBoard() passing playerOne
e.Call function displayGameBoard() passing playerTwo

6.Update function initializePlayer() to comment out the following:
a.Call to function displayGameBoard()
b.Call to function displayShips()
c.Calls to function getchar()

7.Write function isValid to do the following
a.Return type boolean
b.Parameter list
i.struct Player player
ii.integer row
iii.integer col
iv.integer length
v.character for ship orientation
c.Declare the following variables
i.Integer for looping the rows
ii.Integer for looping the columns
iii.Boolean for storing if the selected row/column location for the ship is valid initialized to true
d.Perform data validation based on the following:
i.If row is less than 0 or (row + length) is greater than 9, return false
ii.If column is less than 0 or (column + length) is greater than 9, return false
iii.If orientation is NOT H and NOT V, return false
e.Based on the orientation (i.e. horizontal or vertical)
i.Loop through the player's game board 2-d array, for each element relative to the values selected for the ships start row/column and the length of the ship,
1.check if the symbol at that position is NOT the ~ character
a.return false
f.Return the local variable data type Boolean that stores if the location is valid

8.Write function placeShip () to do the following
a.Return type void
b.Parameter list
i.Pointer to struct Player player
ii.integer row
iii.integer col
iv.struct Ship ship
v.character for ship orientation
c.Declare the following variables
i.Integer for looping the rows
ii.Integer for looping the columns
d.Based on the orientation (i.e. horizontal or vertical)
i.Loop through the player's game board 2-d array, for each element relative to the values selected for the ships start row/column and the length of the ship
1.update the symbol stored to the symbol of the ship type passed in

9.Write function manualShipPlace () to do the following
a.Return type void
b.Parameter list
i.struct Player as a pointer named player for local variable
c.Declare a variables
i.integer shipNum
ii.integer row
iii.integer column
iv.character orientation
v.Boolean to store if location is valid initialized to false
d.Loop through the player's ships
i.In a while loop, continue looping until the current ship has successfully been place (i.e. valid is false)
1.Display the players game board
2.Prompt the user the enter a row and column as the start location for the current ship
3.Prompt the user for the orientation of the ship
4.Set Boolean variable for valid equal to function call isValid() passing arguments
a.Player struct (note: dereference the pointer)
b.Row
c.Column
d.Length of current ship
e.Orientation of ship
5.Write if statement passing as an argument the boolean variable
a.True results in call to function placeShip() passing arguments
i.Player as pointer
ii.Row
iii.Column
iv.Current ship
v.Orientation
b.False results in
i.Set Boolean variable to false
ii.Output to player that input was invalid, try again
ii.After the close curly brace for the while loop, set the Boolean variable equal to false

10.Write function randomShipPlace () to do the following
a.Return type void
b.Parameter list
i.struct Player as a pointer named player for local variable
c.Declare a variables
i.integer shipNum
ii.integer row
iii.integer column
iv.character orientation
v.integer constant for orientation randomization (DIR = 2)
vi.integer constant for orientation horizontal (H = 0)
vii.integer constant for orientation vertical (V = 1)
viii.integer to store random value for orientation
ix.Boolean to store if location is valid initialized to false
d.Loop through the player's ships
i.In a while loop, continue looping until the current ship has successfully been place (i.e. valid is false)
1.Generate a random row
2.Generate a random column
3.Generate a random orientation
4.If the random orientation is 0, then character orientation is H
5.If the random orientation is 1, then the character orientation is V
6.Set Boolean variable for valid equal to function call isValid() passing arguments
a.Player struct (note: dereference the pointer)
b.Row
c.Column
d.Length of current ship
e.Orientation of ship
7.Write if statement passing as an argument the boolean variable
a.True results in call to function placeShip() passing arguments
i.Player as pointer
ii.Row
iii.Column
iv.Current ship
v.Orientation
b.False results in
i.Set Boolean variable to false
ii.Output to player that input was invalid, try again
ii.After the close curly brace for the while loop, set the Boolean variable equal to false

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.