Question: Battleship! . Write a program that simulates the game of Battleship. Battleship is a two player Navy game. The objective of the game is to
Battleship! . 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. The game will be completely text-based Both players' fleets consist of 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. The Carrier has 5 cells Battleship has 4 cells Cruiser has 3 cells Submarine has 3 cells, and the Destroyer has 2 cells. 5 ,,, The program should be built such that the user is Player1 and the computer is Player2. Two boards exist within the game.
Each board should be implemented as a 2-dimensional array. 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: ships cannot be placed diagonally on the board, they can only be placed vertically or horizontally. Your program must check to see 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 (computer's) ships must be randomly placed. Note: the placement of Player2's ships must be unknown.
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. 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 'X' should replace the '-' on Player2's board. If the positioned 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. Homework #5 Due March 29th, 2018 Between turns, clear the screen ( system("cls"); along with the rules of Battleship. first in the game. ). 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 an 'X' and each miss with an 'M' on Player1's board. The game is over win 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: 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), and won or lost the game. Example Functions: NOT Note: you are required to apply these functions in your program! Create a function welcome_screen() that displays an initial program welcome message Create a function initialize_game_board() that sets each cell in a game board to '-'. Create a function that determines if Player1 or Player2 goes select_who_starts_first() 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 that randomly places the 5 types randomly_place_ships_on_board() of ships on a given board. Create a function check_shot() that determines if the shot taken was a hit or a miss. () that determines if a winner exists. Create a function is_winner Create a function update_board() that updates the board every time a shot is taken. '*' indicates a hit and 'm' indicates a miss.
Homework #5 Due March 29th, 2018 that determines if a ship was sunk. Grading: 8 pts for appropriate top-down design of functions, commenting, and good style TOTAL: 100 pts 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. Hint: pass in a flag (int) that stores whether or not you just passed in Player1's or Player2's board. 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. It also writes whether or not it was a hit, miss, and if the ship was sunk. Create a function check_if_sunk_ship() Create a function output_stats() that writes the statistics collected on each player to the log file. 92 pts for adherence to the instructions stated above 3 pts - for displaying a welcome screen 6 pts (3 pts/board) - for initializing player 1s and 2s boards 2 pts for alternating players 5 pts for displaying the game boards accurately 2 pts - for randomly selecting which player goes first 12 pts - for the manual placement of ships on the game board; must not allow overlapping of ships 15 pts for the random placement of ships on the game board; must not allow overlapping of ships 3 pts - for determining if a shot is a hit or miss 5 pts for determining if a ship is sunk 3 pts - for updating the board to indicate hits and misses 4 pts for not allowing the same coordinates for a shot to be used each turn 2 pts for opening the logfile 15 pts for writing moves by each player to a file; also for writing if a ship was hit, missed, and sunk 2 pts - for closing the logfile 8 pts (4 pts/player) for computing and outputting stats to the logfile 5 pts for determining the winner
code must be in C
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
