Battleship game code in C program

SoccerBoss
Category:
Programming
Price: $20 USD

Question description

Battleship game in C program (Code)

You need to build the software infrastructure that will allow you to build a simplified version of the classic game of Battleship that will display on the screen/console. In this program, the user will attempt to find a randomly placed aircraft carrier and battleship on a 10 by 10 board implemented as a two-dimensional array. You should define this SIZE as a constant in case you want to expand or shrink the board later.

Your program will then ask the user to enter the difficulty level of the game as a string, where “easy” means the user has 30 attempts to sink both the aircraft carrier and battleship, “normal” means the user has 25 attempts to do this, and “hard” means the user has only 20 attempts to sink the two ships. If the user does not enter a valid response, you will keep repeating this process until a valid response is entered. To start the game, you will display an introductory message, giving details about the game, including the number of attempts the user gets based on their chosen level of difficulty and the size of the board. This shall be implemented by a function. You shall declare and initialize a two-dimensional array using dynamic memory allocation in main() to represent a 10 x 10 board. This means that you will have to pass the array by reference when needed to the functions that use it. Each element on this 10 x 10 data structure can be visualized as a single location that may host one unit length of an aircraft carrier or battleship.

You shall write a function to assign a ship to a location on the board, given the data structure for the board passed by reference and another parameter providing some identifying factor, such as the length or type of the ship. This function is called by main() and is to only assign (or attempt to assign) one ship that is passed to it. You shall randomly generate the orientation (i.e., either vertical or horizontal) of the ship. You shall also randomly generate the row and column position for the start of the ship. If the ship does not fit on the board at this starting location, you can either return information indicating that the ship could not be assigned (and so it will be up to main() to keep calling this function until it is successfully assigned) or keep randomly generating row and column positions for the ship’s starting position until one is found that can be used to successfully assign the ship. A ship may be assigned anywhere on the board (as long as it fits), but it may not be assigned on top of another ship that exists at that location. This must be a single function designed to handle all ships.


You will also write a function to display the game board, giving the data structure for the board passed by reference. It is called by main() and is invoked anytime the board has been updated (for this homework, it is invoked after the aircraft carrier and battleship have been placed on the board). This function will display the row and column headers for the board as well as the board itself (see sample output). Open space on the board (i.e., where no ships have been assigned) shall be indicated by a blank space on the board. The aircraft carrier shall be represented by an ‘A’ for each of its 5 unit length, while the battleship shall be represented by a ‘B’ for each of its 4 unit length.


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UC Berkeley
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1830 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors