battleship.h

1.Add macros as #define constants
a.HIT 'X'
b.MISS '.'
2.Add function prototypes for the following functions defined later in the document:
a.void playGame(Player * playerOne, Player * playerTwo);
b.int switchPlayer(int currentPlayer);
c.Location getTarget(Player player);
d.Location getRandomTarget();
e.void checkShot(Player * player, Location target);
f.void updatePlayerData(Player * player, Location location, char result, char ship);
g.void checkShips(Player * player, Location location, char ship);
h.void endGame(Player player);

battleship.c

3.Update function main to comment out the following function calls:
a.displayGameBoard() passing playerOne
b.displayGameBoard() passing playerTwo
c.Add call to function playGame(&playerOne, &playerTwo); before the return 0; statement

4.Write function void playGame(Player * playerOne, Player * playerTwo) to do the following
a.Return type void
b.Parameter list
i.Pointer for playerOne
ii.Pointer for playerTwo
c.Declare local variables
i. int currentPlayer = PLAYER1;
ii. Location target;
d.In a while(true) loop
i.switch (currentPlayer)
1.case PLAYER1
a.Output that it is player one's turn (using their name)
b.Call function displayGameBoard(*playerOne)
c.Set local variable target equal to function call getTarget(*playerOne)
d.Call function checkShot(playerTwo, target)
2.case PLAYER2
a.Output that it is player twos turn (using their name)
b.Set local variable target equal to function call getRandomTarget ()
c.Call function checkShot(playerOne, target)
ii.Set local variable currentPlayer equal to function call switchPlayer(currentPlayer)
iii.Call function clearScreen()

5.Write function int switchPlayer(int currentPlayer) to do the following
a.Return type int
b.Parameter list int currentPlayer
c.If currentPlayer is PLAYER1, return PLAYER2
d.Else if currentPlayer is PLAYER2 return PLAYER1

6.Write function Location getTarget(Player player) to do the following
a.Return type Location
b.Parameter list struct Player player
c.Declare local variable Location target
d.Call function fflush (stdin)
e.Output to the user to enter the desired row col as the target using the player's name
f.Call function scanf () to store the row and column in struct Location target
g.Call function fflush (stdin)
h.Return target

7.Write function Location getRandomTarget() to do the following
a.Return type Location
b.Empty parameter list
c.Declare local variable Location target
d.Set target.row equal to a random value based on constant ROWS
e.Set target.column equal to a random value based on constant COLS
f.Return target

8.Write function void checkShot(Player * player, Location target) to do the following
a.Return type void
b.Parameter list
i.Pointer to struct Player player
ii.Struct Location target
iii.Declare variable char symbol set equal to player->gameBoard.board[target.row][target.column]
iv.switch (symbol)
1.case WATER
a.Output to the user that the row and col was a miss!
b.Call function getchar()
c.Call function updatePlayerData(player, target, MISS, symbol)
2.case CARRIER:, case BATTLESHIP:, case CRUISER:, case SUBMARINE:, case DESTROYER:
a.Output to the user that the row and col was a hit!
b.Call function getchar()
c.Call function updatePlayerData(player, target, HIT, symbol)
3.case HIT:,case MISS:, default
a.Output to the user that they already guessed that location, lost turn!
b.Call function getchar()

9.Write function void updatePlayerData(Player * player, Location location, char result, char ship) to do the following
a.Return type void
b.Parameter list
i.Struct Player player as a pointer
ii.Struct Location location
iii.char result
iv.char ship
v.Update the player's board to set player->gameBoard.board[location.row][location.column] equal to the value of result
vi.If result is equal to HIT
1.Call function checkShips(player, location, ship)

10.Write function void checkShips(Player * player, Location location, char ship) to do the following
a.Return type void
b.Parameter list
i.Struct Player player as a pointer
ii.Struct Location location
iii.char ship
c.Create constants based on the order you placed the ships in the player's ship array
i.const int battleshipIdx
ii.const int carrierIdx
iii.const int cruiserIdx
iv.const int destroyerIdx
v.const int submarineIdx
d.Create variables
i. int sunkCount = 0;
ii. int shipNum;
e.switch(ship)
i.Write a case label for each of the five ships to do the following:
1.Decrement the ships length (e.g. player->ships[battleshipIdx].length--)
2.If the ships length is equal to 0 (zero)
a.Output that the players ship is sunk (e.g. printf("%s's BATTLESHIP is sunk!", player->name)
3.Call function getchar()
f.Write for loop to loop through the players five ships to do the following
i.If the ships length is 0 (zero)
1.Increment the variable sunkCount by one
g.If sunkCount is equal to the NUM_SHIPS
i.Call function endGame(*player)

11.Write function void endGame(Player player) to do the following
a.Return type void
b.Parameter list struct Player player
c.Output which player HAS LOST THE GAME!
d.Call function getchar()
e.Call function exit(0)

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.