Hello. I am creating a battleship program and it appears for everything to be working fine, except the shots. For some reason my shots and my locations are not working and I have been working on fixing this a long time. Can somebody please take a look at my code?
//Header File:
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// coordinates (location) of the ship and shots
class location{
public:
location(); // void constructor, assigns -1 to X
void pick(); // picks a random location
void fire(); // asks the user to input coordinates of the next shot
void print() const; // prints location in format "a1"
// returns true if the two locations match
friendbool compare(location, location);
private:
staticconstint fieldSize=5; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (location) and whether is was sunk
class ship{
public:
ship(); // void constructor, sets sunk=false
bool match(location&) const; // returns true if this location matches
// the ship's location
bool isSunk() const {return sunk;} // checks to see if the ship is sunk
void sink(); // sets "sunk" member variable of the ship to true
void setLocation(const location&); // deploys the ship at the specified location
void printShip() const; // prints location and status of the ship
private:
location loc;
bool sunk;
};
// contains the fleet of the deployed ships
class fleet{
public:
void deployFleet(); // deploys the ships in random locations
// of the ocean
bool operational() const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(const location &); // returns true if there was a deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet() const; // prints out locations of ships in fleet
private:
staticconstint fleetSize=5; // number of battleships
int check(const location &); // returns index of the ship
// that matches location
// -1 if none match
ship ships[fleetSize]; // battleships of the fleet
};
#endif /* BATTLESHIP_H_ */
//Test File to run the program:
#include "battleship.h"
#include <iostream>
usingnamespace std;
int main (){
char answer;
fleet myFleet;
myFleet.deployFleet();
cout << "Print position of ships?<y or n>" << endl;
cin >> answer;
if (answer=='y')
myFleet.printFleet();
while (myFleet.operational()==true){
cout << "there are still ships up";
location userShot;
userShot.location();
cout << "Please select next shot(a-e then 1-5) ";
userShot.fire();
if (myFleet.isHitNSink(userShot)==true)
cout << "Shot!";
else
cout << "Missed!";
cout << "Print position of ships? <y or n>";
cin >> answer;
if (answer== 'y')
myFleet.printFleet();
elseif(answer=='n')
break;
}
}
Using the test that I have it has to be in the deploy. I get the same fleet everytime that it deploys but after I print and try to shoot a spot where I know there is a boat it says I missed.