C++ battleship program. Structures Introduction You are to program a simplified
ID: 3685559 • Letter: C
Question
C++ battleship program.
Structures Introduction You are to program a simplified version of the Battleship guessing game. The assignment is broken into two parts. The below game and header file description applies to both parts Battleship game description. The field (ocean) is a square 5x5 grid. One of the coordinates of the grid is a number (from 1 to 5) and the other -- a letter (from 'a' to 'e'). Your program should randomly place a fleet of five ships in the ocean. Each ship takes up exactly one location in the ocean. Multiple ships cannot be placed in the same location. The ships, however, can be placed in adjacent locations. The user fires on the ships by specifying the coordinates of the shot. The program reports whether each shot was a hit or a miss. If the shot was a hit, the ship is sunk. The game continues until all ships are sunk. The program does not keep track of the locations of the previously fired shots. Data structures and function description. The data structures and functions needed to implement the game are declared in this header shown below Figure 1. The header file defines two structures: location stores the number and letter coordinates of a ship or a shot; ship stores the coordinates of the ship in location substructure and a boolean variable signifying whether the ship was sunk. The fleet of the deployed ships should stored in the array where each element is a structure variable of ship. This array is first initialized and then used in the game. . · The functions are separated into three groups: Initialization functions that place the fleet of battleships in the ocean. The major function among the initialization functions is deploy () that accepts an array of ships by reference and initializes their locations. It uses the other two initialization functions: pick() and check () The pseudocode for deploy () is as follows: . declare a variable that stores the number of the deployed ships, initially zero this variable is to be used as an index ln the array of ships loop until all ships are deployed invoke pick) to get a new random location in the ocean invoke check) to verify whether this location is occupiedExplanation / Answer
testShips.cpp
#include "battleship.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main(){
// create location
location mySpot;
mySpot.x=5;
mySpot.y='e';
// print location
cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;
mySpot = pick(); // assign randomly generated location to mySpot
cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;
mySpot = fire(); // assign user input location to mySpot
cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;
// checking ship functions
ship myShip;
myShip.loc = pick(); // assign random location to myShip
myShip.sunk = false; // myShip is not sunk
printShip(myShip); // print myShip information
if(match(myShip,mySpot))
cout << "myShip is at mySpot location ";
else
cout << "myShip is not at mySpot location ";
sink(myShip); // sinking myShip
printShip(myShip); // print sunk ship
// checking fleet functions
ship myFleet[FLEET_SIZE];
initialize(myFleet); //assigning -1 to all ship's locations in myFleet
printFleet(myFleet);
deploy(myFleet); // deploying ships at random locations
printFleet(myFleet);
if(check(myFleet,mySpot) != -1)
cout << "myFleet has a ship at mySpot ";
else
cout << "myFleet does not have a ship at mySpot ";
if(operational(myFleet))
cout << "at least one ship is not sunk ";
else
cout << "all ships are sunk ";
}
battleship.h
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// data structures definitions
const int FLEET_SIZE=5; // number of battleships
const int FIELD_SIZE=5; // the field (ocean) is FIELD_SIZExFIELD_SIZE
// coordinates (location) of the ship and shots
struct location{
int x; // 1 through FIELD_SIZE
char y; // 'a' through FIELD_SIZE
};
// contains ship's coordinates (location) and whether is was sunk
struct ship{
location loc;
bool sunk;
};
// initialization functions
void initialize(ship[]); // places all ships in -1 X location to signify
// that the ship is not deployed
location pick(); // generates a random location
bool match(ship, location); // returns true if this location matches
// the location of the ship
// returns false otherwise
int check(const ship[], location); // returns the index of element of the array
// that matches the location
// returns -1 if none do
// uses match()
void deploy(ship[]); // places an array of battleships in
// random locations in the ocean
// display functions
void printShip(ship); // prints the location and status (sunk or not)
// of a single ship
void printFleet(const ship[]); // prints the locations of all the ships and
// whether they are sunk
// battle functions
bool operational(const ship[]); // returns true if at least one ship in the array
// is not sunk
location fire(); // asks the user to input the coordinates of the next
// shot
// note that check() is also used in the battle
void sink(ship&); // sets "sunk" member variable of the ship to true
#endif /* BATTLESHIP_H_ */
battleship.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using std::cout; using std::cin; using std::endl;
bool match(ship myShip, location mySpot){
// returns true if this location matches
// the location of the ship
// returns false otherwise
if (myShip.loc.x == mySpot.x && myShip.loc.y == mySpot.y)
return true;
else
return false;
}
int check(const ship myFleet[], location mySpot){
// returns the index of element of the array
// that matches the location
// returns -1 if none do
for (int i = 0; i < FLEET_SIZE; ++i)
if (match(myFleet[i], mySpot)) {
return i;
}
return -1;
}
void initialize(ship myFleet[]) {
// places all ships in -1 X location to signify
// that the ship is not deployed
for (int i=0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
myFleet[i].loc.y = 'X';
}
}
location pick() {
// generates a random location
location mySpot;
srand(int(time(0)));
mySpot.x = rand() % 5 + 1;
int y = rand() % 5 + 1;
switch (y) {
case 1:
mySpot.y = 'a';
break;
case 2:
mySpot.y = 'b';
break;
case 3:
mySpot.y = 'c';
break;
case 4:
mySpot.y = 'd';
break;
case 5:
mySpot.y = 'e';
break;
}
return mySpot;
}
void deploy(ship myFleet[]) {
// places an array of battleships in
// random locations in the ocean
int i = 0;
while (i < FLEET_SIZE) {
location mySpot = pick();
int tmp = check(myFleet, mySpot);
if (tmp == -1) {
myFleet[i].loc = mySpot;
myFleet[i].sunk = false;
++i;
}
}
}
// DISPLAY FUNCTIONS
void printShip(ship myShip) {
// prints the location and status (sunk or not) of a single ship
cout << "The ship is at: " << myShip.loc.x << myShip.loc.y << endl;
cout << "Status of ship: ";
if (myShip.sunk == false)
cout << "The ship is still alive!" << endl;
else
cout << "The ship was sunk!" << endl;
}
void printFleet(ship const myFleet[]) {
// prints the locations of all the ships and whether they are sunk
for (int i = 0; i < FLEET_SIZE; ++i) {
printShip(myFleet[i]);
}
}
// BATTLE FUNCTIONS
bool operational(ship const myFleet[]) {
// returns true if at least one ship in the array is not sunk
for (int i = 0; i < FLEET_SIZE; ++i) {
if (myFleet[i].loc.x != -1 && myFleet[i].loc.y != -1)
return true;
}
return false;
}
location fire() {
// asks the user to input the coordinates of the next shot
location mySpot;
cout << "Where will you take your next shot? (Coordinates 1-5 a-e separated by a space): ";
cin >> mySpot.x >> mySpot.y;
return mySpot;
}
void sink(ship& myShip) {
// sets "sunk" member variable of the ship to true
myShip.sunk = true;
}
sample output
mySpot.x = 5 mySpot.y = e
mySpot.x = 5 mySpot.y = b
Where will you take your next shot? (Coordinates 1-5 a-e separated by a space): 4 a
mySpot.x = 4 mySpot.y = a
The ship is at: 2a
Status of ship: The ship is still alive!
myShip is not at mySpot location
The ship is at: 2a
Status of ship: The ship was sunk!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: 2a
Status of ship: The ship is still alive!
The ship is at: 1c
Status of ship: The ship is still alive!
The ship is at: 4e
Status of ship: The ship is still alive!
The ship is at: 2e
Status of ship: The ship is still alive!
The ship is at: 3c
Status of ship: The ship is still alive!
myFleet does not have a ship at mySpot
at least one ship is not sunk
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.