• Home
  • Blog
  • Build a C program to play a simplified version of Battleship (C Programming)

Build a C program to play a simplified version of Battleship (C Programming)


For this homework assignment, you will edit your Homework 6 to add functionality to play a simplified version of the classic game of Battleship on the screen/console. (I WILL ATTACH MY HOMEWORK 6 PROGRAM: homework6 program.docx )

You shall organize your program into three files:

• prgm.h will hold #includes, #defines, templates of any structure types, and a list of function prototypes.

• main.c will hold the local #include file as well as the main()function for the program.

• func.c will hold the local #include file as well all other functions (except main(), of course) used in the program.

 2. Define a structure that contains the following four members: (1) a character member that holds the row used for the torpedo’s position on the board – note that this character may be a single character in the range ‘A’ – ‘J’; (2) an integer member that holds the column used for the torpedo’s position on the board –note that this integer has a range 1 – 10; (3) an integer member that keeps track of the number of hits on the aircraft carrier – note that an aircraft carrier is sunk 

after 5 hits; and (4) an integer member that keeps track of the number of hits on 

the battleship – note that a battleship is sunk after 4 hits.

3. Update the function to display the game board by adding another parameter used to indicate whether or not to “reveal” the solution with ship positions visible or to display the current, active board. In displaying the board, you will display an ‘X’ for each position where a torpedo shot hit either ship and ‘O’ for each position where a torpedo shot missed a ship. If the “reveal” is not set, you will display a blank space for any position that is open (i.e., no ship is assigned, nor has a torpedo shot been fired) or contains a ship. If the “reveal” is set, you will still display a blank space for any position that is open, but will you display a ‘B’ for each position containing a battleship that has not been hit and an ‘A’ for each position containing an aircraft carrier that has not been hit. You will call this function to display the updated board after every torpedo shot fired by the user. During the game, “reveal” will not be set. However, after the game is won or lost, you will call this function with “reveal” set to display the board.

4. Add a value-returning function to check the status of the shot fired by the user, given the two-dimensional array for the board passed by reference and a pointer to a variable of the structure defined above. This function is called by main()and checks the status of the user-fired torpedo (i.e., hit, miss, duplicate shot,etc.) and updates the board as a result of the shot fired. This function will also update the number of hits on either ship if either an aircraft carrier or battleship was hit and display a message to the screen/console if a particular ship was sunk. And finally, this function will return a value to indicate whether or not the game is won (i.e., both ships are sunk) or lost (i.e., at least one position on at least one ship has not been shot).

5. Inside main(), you will add a loop to play the game until either the game is won (i.e., both ships are sunk) or the user has fired all of his/her allocated torpedo shots without sinking both ships. For each turn, the user will enter a position (e.g., B7) corresponding to the position on the board where the torpedo will be fired. If the user enters an invalid coordinate position, you will indicate that the torpedo cannot be fired at that location and re-prompt the user to enter the coordinates again (without incrementing the number of shots fired). You may assume that the user enters the position correctly as [char][int], though one of both may not be within the valid range defined. You will parse this input and store it in the structure variable that is passed to the function to check the status of the shot fired by the user. Following each turn, you shall display an updated version of the board. If the user has sunk both ships, you will indicate that the user has sunk both ships and how many shots were fired to accomplish this task. If the user was unable to sink both ships in the allocated number of shots, you are to indicate that the user was not able to sink both ships and then display the final updated board with “reveal” set so that the position(s) of the ships are shown.

Your functions should receive as arguments only the minimally needed ones of the appropriate type, and should a value of the appropriate type whenever a return value is needed. 

About the Author

Follow me

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}