Question
modify the program to do the following. Design functions InitializeBoard() and DisplayBoard() . Also, provide documentation for the functions. (Description, pre/post conditions.) Design the function
modify the program to do the following.
Design functions InitializeBoard() and DisplayBoard(). Also, provide documentation for the functions. (Description, pre/post conditions.)
Design the function PlaceShip() that determines the position and orientation for a given ship. (Note: You can assume a boolean function IsValid() that checks to see if a given position is valid for a given ship.)
Submit:
Pseudocode for each of the above functions
Commented battleship.h file
Code implementing each of the above functions along with a main() function with test calls.
PROGRAM
Purpose: Builds a board for Battleship that
allows the user to play
*/
DATA TYPES:
const int ROW_SIZE = 10;
const int COL_SIZE = 10;
char BattleBoard[ROW_SIZE][COL_SIZE];
char DisplayBoard[ROW_SIZE][COL_SIZE];
struct Location{
int xPos;
int yPos;
};
struct Ship{
int length;
int hitCount;
bool sunk;
Ships type;
Location postion;
};
enum Ships { Aircraft, Batttlship, Submarine, Cruiser, Destroyer }
enum Letters { A, B, C, D, E, F, G, H, I, J };
Letters rowGuess;
char ship, waves, hit, miss;
int columnGuess (-1 to reflect array position);
//*************** FUNCTIONS BELOW ****************
Generates the board
//Pre: Pass in the ship postions, populate board with waves,
// replace waves with ship marker
//Post: Board has been populated with waves and ships
InitializeBoard()
char WAVES = ~
char SHIP = #
char HIT = H
char MISS = .
//Placing the ships onto the board into random positions
//Pre:
//Post: Populates each ship with a set value [][]
// to be places onto the board
BuildShip()
One 5.size = #####
One 4.size = ####
Two 3.size = ###
One 2.size = ##
//
//Pre: Board has been created with valid ship positions
//Post: Valid player guess has been passed to be tested if a hit or miss
PlayerGuess()
//Checks the user's guess against the board
//Pre: Valid player guess has been passed in
//Post: Tests the players guess against the board, changes hit or miss
checkPOS()
//Checks if the postion is free or not for ship placement
//Pre: Board has been created and passed in
//Post: Ship placements have been confirmed and placed on the board
bool IsVacant()
//Outputs the board with updated results
//Pre: CheckPOS has updated the board with a hit or miss
//Post: New board has been placed on the screen to allow the user to see
OutputBoard()
int main(){
Create ships
Ensure their positions do not overlap
Create and populate board with ships
Display Board
Prompt user for input
Test if valid input (ex. C 7)
Must be A-J and 1-10
Check if input hits or misses a ship
If a hit, replace position with a 'H'
If a miss, replace position with a '.'
Display updated board
return 0;
}
SetBoard()
{
Set for loop to display waves in all array index
}
SetShips()
{
pick random number 0-9
if even set ship to vertical - odd is horz
pick random number 0-9
Set height/length
}
//Checks if the postion is free or not for ship placement
//Pre: Board has been created and passed in
//Post: Ship placements have been confirmed and placed on the board
bool IsVacant()
{
}
//
//Pre: Board has been created with valid ship positions
//Post: Valid player guess has been passed to be tested if a hit or miss
PlayerGuess()
{
}
//Checks the user's guess against the board
//Pre: Valid player guess has been passed in
//Post: Tests the players guess against the board, changes hit or miss
checkPOS()
{
}
//Outputs the board with updated results
//Pre: CheckPOS has updated the board with a hit or miss
//Post: New board has been placed on the screen to allow the user to see
OutputBoard()
{
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started