0% found this document useful (0 votes)
35 views2 pages

Assignment 3

The assignment requires creating a Battleship game using an 8x8 2D array to represent the game board, where players guess the locations of hidden ships. Players have a limited number of chances to guess, with feedback provided for hits and misses, and the game ends when all chances are used or a certain number of ships are hit. The implementation must include specific functions for board initialization, ship placement, board display, hit detection, and player turns, with additional features allowed for customization.

Uploaded by

singh.angad0905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Assignment 3

The assignment requires creating a Battleship game using an 8x8 2D array to represent the game board, where players guess the locations of hidden ships. Players have a limited number of chances to guess, with feedback provided for hits and misses, and the game ends when all chances are used or a certain number of ships are hit. The implementation must include specific functions for board initialization, ship placement, board display, hit detection, and player turns, with additional features allowed for customization.

Uploaded by

singh.angad0905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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:

1. Game Board Setup:


o The game board is a 8x8 grid, represented by a 2D array (char board[8][8]).
o Initially, all elements of the array are set to ~ (representing water).
o You will hide 7 ships on the board, represented by the letter S. Each ship occupies
a single cell.
o The ships should be placed randomly on the board.

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.

You might also like