c++ battleship game. Battleship game description. The field (ocean) is a square
ID: 3687449 • Letter: C
Question
c++ battleship game.
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 in 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
Lab8_Test_testShips.cpp
#include "battleship.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
//
// checking Location functions
//
// 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 ";
}
Lab8_Test_battleships.cpp
#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::cin; using std::endl;
Location pick() {
Location location;
srand(time(nullptr));
location.x = (rand() % 4) + 1;
int tmp = (rand() % 4) + 1;
switch (tmp) {
case 1: location.y = 'a'; break;
case 2: location.y = 'b'; break;
case 3: location.y = 'c'; break;
case 4: location.y = 'd'; break;
case 5: location.y = 'e'; break;
}
return location;
}
Location fire() {
Location location;
cout << "Input x-axis coordinate (1-5)" << endl;
cin >> location.x;
cout << endl << "Input y-axis coordinate (a-e)" << endl;
cin >> location.y;
return location;
}
void printShip(Ship myShip) {
cout << "Sunk? "<< myShip.sunk << endl
<< "My ship x coord " << myShip.loc.x << endl
<< "My ship y coord " << myShip.loc.y << endl;
}
bool match(Ship myShip, Location mySpot) {
if ((myShip.loc.x == mySpot.x ) &&
(myShip.loc.y == mySpot.y))
return true;
else
return false;
}
void sink(Ship& myShip) {
myShip.sunk = 1;
}
void initialize(Ship myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
}
}
void deploy(Ship myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
myFleet[i].sunk = false;
myFleet[i].loc = pick();
if (i > 0) {
bool pass = false;
while (pass == false) {
for (int c = 0; c < i; ++c) {
if (myFleet[i].loc.x != myFleet[c].loc.x)
pass = true;
else {
myFleet[i].loc = pick();
c = 0;
}
}
}
}
}
}
void printFleet(const Ship myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
cout << "Ship " << i + 1 << " x: " << myFleet[i].loc.x << " "
<< "Ship " << i + 1 << " y: " << myFleet[i].loc.y << endl;
}
}
int check(const Ship myFleet[], Location mySpot) {
for (int i = 0; i < FLEET_SIZE; ++i)
if ((myFleet[i].loc.x == mySpot.x) &&
myFleet[i].loc.y == mySpot.y)
return 1;
else
return -1;
}
bool operational(const Ship myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
if (myFleet[1].sunk == false) {
return true;
}
}
return false;
}
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 every Ship in a Location where x-coordinate is -1 and y-coordinate is '*' (a star) 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_
sample output
mySpot.x = 5 mySpot.y = e
mySpot.x = 4 mySpot.y = a
Input x-axis coordinate (1-5)
2
Input y-axis coordinate (a-e)
b
mySpot.x = 2 mySpot.y = b
Sunk? 0
My ship x coord 4
My ship y coord c
myShip is not at mySpot Location
Sunk? 1
My ship x coord 4
My ship y coord c
Ship 1 x: -1 Ship 1 y:
Ship 1 x: -1 Ship 1 y:
Ship 2 x: -1 Ship 2 y: p
Ship 3 x: -1 Ship 3 y:
Ship 4 x: -1 Ship 4 y: ;
Ship 5 x: -1 Ship 5 y:
b5
Ship 1 x: 4 Ship 1 y: c
Ship 2 x: 1 Ship 2 y: a
Ship 3 x: 2 Ship 3 y: d
Ship 4 x: 4 Ship 4 y: c
Ship 5 x: 3 Ship 5 y: c
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.