Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ project. Battleship game description. The field (ocean) is a square 5x5 grid. One of the coordinates of the grid is a number (from 1

C++ project.

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.

image text in transcribed

battleships.cpp #include "battleship.h" #include #include #include 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 > location.x; cout > location.y; return location; } void printShip(Ship myShip) { cout 0) { bool pass = false; while (pass == false) { for (int c = 0; c

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_

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago