Assignment 3
Assignment 3
Objective: Create a Battleship game that uses a 2D array to represent a game board. The player
will try to "hit" hidden ships by guessing their locations.
Game Specifications:
You can choose a board bigger than 8x8. The game board of 8x8 can look like
this:
2. Player's Turn:
o The player has 5 chances (or the number you choose) to guess where the ships
are.
o For each guess, the player enters row and column coordinates (e.g., 2 3).
o If the guess is correct (i.e., a ship is at that location), the board should show a X at
that location and print "Hit!".
o If the guess is incorrect, the board should show an O at that location and print
"Miss!".
o After each guess, the game board should be displayed again with the update.
3. Ending the Game:
o The game ends when the player has used all the chances, or certain number of the
ships have been hit (for example, 3 ships have been hit) .
o At the end of the game, the board is displayed with all ships revealed.
1
Detailed Requirements:
1. Array Representation: Use a 2D array to represent the board. Each element of the array
represents a cell that can either be water, a hit, a miss, or a ship.
2. Functions: You must implement at least the following functions:
o void initializeBoard(char board[8][8]);
Initializes the board with ~ (water).
o void placeShips(char board[8][8]);
Randomly places 7 ships (S) on the board.
o void displayBoard(char board[8][8], bool revealShips = false);
Displays the current state of the board. If revealShips is true, all the ships (S)
are displayed.
o bool isHit(char board[8][8], int row, int col);
Returns true if the player guessed the location of a ship.
o void playerTurn(char board[8][8]);
Takes the player's input and marks the board accordingly (hit or miss).
3. You can:
o Add more ships.
o Allow the player to choose a custom grid size.
o Keep track of the number of turns it took the player to win.
Grading Criteria:
o Correct implementation of game rules and logic and Proper handling of edge cases (2
points)
o Code readability, structure, and comments (1 point)
o Proper user input handling/validation and output formatting (1 point)
Submission:
Submit the C++ source file (.cpp) and the screenshot of your program output on Moodle.
Test your program thoroughly and ensure that your code compiles and runs correctly.