Programming Assignment #4: Game of Battleship
Due on July 13th, 11:59pm
For this assignment you will write a program that simulates the game of Battleship. This will help you in mastering 2-dimensional arrays, pointers and loops. This assignment can be considered the signature assignment for all CptS 121 courses at WSU. Hence, I’m quoting the question from Gina Sprint’s homework assignments for her class last year, and it should resemble (in all or partially) the homework assignment by Andy O’Fallon.
Note that you are asked to create at least one .h file and a .c file associated with it. Your program should successfully build. The most points an assignment will receive if it does not build successfully is 65.
Problem:
(starting the quotation)
Write a program that simulates the game of Battleship. Battleship is a two player Navy game. The objective of the game is to sink all ships in your enemy’s fleet. The Player to sink his/her enemy’s fleet first wins. Both players’ fleets consist of 5 ships that are hidden from the enemy. Each ship may be differentiated by its “size” (besides the Cruiser and Submarine) or number of cells it expands on the game board:
1. Carrier has 5 cells
2. Battleship has 4 cells
3. Cruiser has 3 cells
4. Submarine has 3 cells
5. Destroyer has 2 cells
The program should be built such that the user is Player1 and the computer is Player2. Two boards (2-dimensional arrays) exist within the game. Each 2- dimensional array should be 10 X 10. One represents Player1’s board and one represents Player2’s board. At the beginning of the game each Players’ game board should be initialized to all ‘-‘ indicating that no ships have been placed on either board.
Before the game starts, Player1 should have the option to either manually place each of the 5 ships in his/her fleet or to have them randomly placed on the board. If Player1 decides to place the ships manually, then he/she should be prompted to
place the Carrier first, Battleship second, Cruiser third, Submarine fourth, and the Destroyer last. Note that ships cannot be placed diagonally on the board, they can only be placed vertically or horizontally (you don’t need to check for this); however, you do need to check if the user tries to place a ship outside the boundaries of the board or on top of a ship that has already been placed.
Each cell on the board that contains part of the ship must be indicated by ‘c’ for Carrier, ‘b’ for Battleship, ‘r’ for Cruiser, ‘s’ for Submarine, or ‘d’ for Destroyer. For example, if the Carrier was placed then the board should contain 5 ‘c’ s for each cell on the board that has a piece of the Carrier, etc. Once Player1’s ships have been placed, Player2’s ships must be randomly placed. Note that the placement of Player2’s ships must be unknown to the user. Thus, Player2’s board will only display ‘-‘ in each cell after the placement of each ship. The program should randomly select Player1 or Player2 to go first.
Once it has been decided on which player goes first, the game starts. Whenever it’s Player1’s turn, a prompt should be displayed asking for a position to target (specifying where to “shoot”) on the enemy’s (Player2’s) board (2-dimensional array). The position should be specified in terms of a row and a column on the board. The row and column should always be displayed along with the board.
If the position specified happens to hit a ship, then a ‘*’ should replace the ‘-‘ on Player2’s board. If the hit sinks a ship (Armen: i.e. all cells associated with that ship are hit), replace the ‘*’s on Player2’s board with the characters representing the sunk ship (i.e. if the destroyer is sunk, replace the 2 ‘*’s with ‘d’s). If the position specified misses any one of the ships in the fleet, then a ‘m’ should replace the ‘-‘ on Player2’s board. Note that from turn-to-turn each player should NOT be allowed to enter the same position. Also, between turns clear the screen (system(“cls”)). (Armen: writing system(“cls”); command at any stage of your program will clear the screen when the run arrives in that line.)
In one turn, a player can only take one shot at the enemy’s (other player’s) fleet. When Player2 takes a shot at Player1’s board, each hit should be specified with a ‘*’ and each miss with a ‘m’ on Player1’s board. The game is over when Player1 or Player2 has sunk all of the ships in the fleet of the enemy.
For each move made by Player1 and Player2, the results should be echoed to a file called battleship.log. In this file, you should log the targeted position by each player on each move and whether it was a hit on one of the ships in the fleet. Also, if one of the ships happens to sink, then note this in the log file. For more information about the rules of Battleship visit: (Armen: Rules of Battleship).
At the end of the game, Player1’s and Player2’s statistics should be written to battleship.log. The stats include:
Total number of hits
Total number of misses
Total number of shots
Hits to misses ratio (as a percentage)
Won or lost the game
Once the game has ended you should write each player’s stats to the battleship.log file.
BONUS – 5 pts (Armen: you will learn more about the structs on July 11th and 12th sessions.)
Keep track of the statistics in a structure called Stats. You will need two variables of type Stats, one for Player1 and one for Player2. When the game ends write the contents of each struct variable to the battleship.log file.
Functional Decomposition
The first step is to draw a structure chart to help you understand the decomposition of functions for this program. (Armen: you are not required to submit this chart! Structure chart is similar to the one we drew for GPA program, showing each function with a box, connecting these boxes to each other and specifying the input and output of each box. See slide 16, Lecture 8). Remember to start with the overall problem and break it down into inputs, computations, and outputs. One possible functional decomposition includes the following (Note: you are NOT required to apply all these functions in your program!):
Create a function welcome_screen() that displays an initial program welcome message along with the rules of Battleship.
Create a function initialize_board() that sets each cell in a game board to ‘-‘.
Create a function select_who_starts_first() that determines if Player1 or Player2 goes first in the game.
Create a function manually_place_ships_on_board() that allows the user to place each of the 5 types of ships on his/her game board.
Create a function randomly_place_ships_on_board() that randomly places the 5 types of ships on a given board.
Create a function check_shot() that determines if the shot taken was a hit or a miss.
Create a function is_winner() that determines if a winner exists.
Create a function update_board() that updates the board every time a
shot is taken. ‘*’ indicates a hit and ‘m’ indicates a miss.
Create a function display_board() that displays a board to the screen. Note that Player1’s board should be displayed differently than Player2’s board (see above).
o Hint: pass in a flag (int) that stores whether or not you just passed in Player1’s or Player2’s board.
o Then perform the different logic for Player1’s board versus Player2’s board.
Create a function output_current_move() that writes the position of the shot taken by the current player to the log file.
o It also writes whether or not it was a hit, miss, and if the ship was sunk.
Create a function check_if_sunk_ship() that determines if a ship was
sunk.
Create a function output_stats() that writes the statistics collected on each player to the log file.
Other functions that you think are necessary!
A main function that does the following:
o Opens an output file battleship.log for writing; o Simulates the game of Battleship
o Outputs data to the log file
o Outputs stats to the log file
o Closes the log file
Sample Execution
The following sample session demonstrates how your program should work (user input is shown in bold).
***** Welcome to Battleship! *****
Rules of the Game:
1. This is a two player game.
2. Player1 is you and Player2 is the computer.
3. Etc. (You need to list the rest of the rules here.)
Hit enter to start the game!
Enter
(clear the screen)
Please select from the following menu:
1. Enter positions of ships manually.
2. Allow the program to randomly select positions of ships. 1
Please enter the five cells to place the Carrier across: 0203040506
Please enter the four cells to place the Battleship across: 34445464
Etc……
Player2 (Computer’s) board has been generated.
Player1 has been randomly selected to go first.
Player1’s Board: 0123456789
0–ccccc— 1dd——– 2———s 3—-b—-s 4—-b—-s 5—-b—– 6—-b—– 7—rrr—- 8———- 9———-
Player2’s Board: 0123456789
0———- 1———- 2———- 3———- 4———- 5———- 6———- 7———- 8———- 9———-
Enter a target: 2 3 (clear screen)
2,3 is a hit!
Player1’s Board: 0123456789
0–ccccc— 1dd——– 2———s 3—-b—-s 4—-b—-s 5—-b—– 6—-b—– 7—rrr—- 8———- 9———-
Player2’s Board: 0123456789
0———- 1———- 2—*—— 3———- 4———- 5———- 6———- 7———- 8———- 9———-
Player selects: 9 9
9,9 is a miss!
Hit enter to continue!
Enter
(clear screen)
Player1’s Board: 0123456789
0–ccccc— 1dd——– 2———s 3—-b—-s 4—-b—-s 5—-b—– 6—-b—– 7—rrr—- 8———- 9———m
Player2’s Board: 0123456789
0———- 1———- 2—*—— 3———- 4———- 5———- 6———- 7———- 8———- 9———-
Etc…
Player1 Wins!
Statistics outputted to logfile successfully!
Submission Guidelines
1. Your project must have at least one header file (a .h file), two C source files (which must be .c files), and project workspace. Delete the debug folder before you zip the project folder.
2. Your project must build properly. The most points an assignment can receive if it does not build properly is 65 out of 100.
(Armen: first guideline is deleted since it does not apply to our class, our submissions being through OSBLE+).
Grading Guidelines
This assignment is worth 100 points. Your assignment will be evaluated based on a successful compilation and adherence to the program requirements. We will grade according to the following criteria:
95 pts for adherence to the instructions stated above (see above)
5 pts for appropriate top-down design of functions and good style
BONUS: Up to 5 pts for keeping track of the statistics in a Stats struct.
o Note: If you attempt the extra credit, state so explicitly in the comment
block in your main source file. (end of quotation)
Given the complexity of this assignment, the upcoming 4th of July holiday and your midterm exam, rather than the one week submission time of the original version of this homework is extended to 12 days for this homework for our class (due on Wednesday, July 13, 2016, 11:59pm). However we will have a new assignment (PA5) handed out on July 7th and due on July 15th, therefore I recommend starting working on this early!