Question

In: Computer Science

I am working on a project for my Computer Science course. I am trying to create...

I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run it. Could someone please help me to get this program working properly? I will paste the code below:

battleship.h:

#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_


// coordinates (location) of the ship and shots
class location {
public:
void locationreset(void); // void constructor, assigns -1 to X
void pick(void); // picks a random location

void fire(void); // asks the user to input the
// coordinates of the next shot
void print(void) const; // prints location in format "a1"

// returns true if the two locations match
friend bool compare(location, location);

private:
static const int 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); // void constructor, sets sunk=false
bool match(location) const; // returns true if this location matches
// the ship's location
bool isSunk(void) const { return(sunk); }; // checks to see if the ship is sunk
void sink(void); // sets "sunk" member variable of the ship to true
void setLocation(location); // deploys the ship at the specified location
void printShip(void) 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(void); // deploys the ships in random locations
// of the ocean
bool operational(void) const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(location); // returns true if there was a deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet(void) const; // prints out locations of ships in fleet

private:
static const int fleetSize = 5; // number of battleships
bool check(location); // returns true if one of the ship's locations
// matches the ship, returns false
// if none-match
ship ships[fleetSize]; // battleships of the fleet
};

#endif /* BATTLESHIP_H_ */

//////////////////////////////////////////////////////////////////////////////////////////////////////////

battleship.cpp:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"

using namespace std;

void location::locationreset(void) {
   x = -1;
   y = 'x';
}

void location::pick(void) {
   x = rand() % fieldSize + 1;
   y = rand() % fieldSize;
   switch (y) {
   case 0: y = 'a'; break;
   case 1: y = 'b'; break;
   case 2: y = 'c'; break;
   case 3: y = 'd'; break;
   case 4: y = 'e'; break;
   }
}

void location::fire(void) {
   cout << "input the coordinates of the next shot "; cin >> y >> x;

}
void location::print() const {
   cout << y << x;
}
bool compare(location loc1, location loc2) {
   return (loc1.x == loc2.x && loc1.y == loc2.y);
}

ship::ship(void) {
   sunk = false;
}

bool ship::match(location shiploc) const {
   return compare(loc, shiploc);
}

void ship::sink(void) {
   sunk = true;
}

void ship::setLocation(location shiploc) {   // deploys the ship at the specified location
   loc = shiploc;
}

void ship::printShip(void) const { // prints location and status of the ship
   loc.print();
   if (sunk == true)
       cout << "sunk";
   else if (sunk == false)
       cout << "not sunk" << endl;
}

void fleet::deployFleet(void) { // deploys the ships in random locations of the ocean
   location loc; loc.locationreset();
   for (int i = 0; i < fleetSize; i++) {
       loc.pick();
       ships[i].setLocation(loc);
   }

}

bool fleet::operational() const { // returns true if at least one ship in the fleet is not sunk
   for (int i = 0; i < fleetSize; ++i)
       if (ships[i].isSunk() == false)
           return true;
   return false;

}

bool fleet::isHitNSink(location shiploc) {   // returns true if there was a deployed
   for (int i = 0; i < fleetSize; ++i) {
       if (ships[i].match(shiploc) == true && ships[i].isSunk() == false)
       {
           ships[i].sink();
           return true;
       }
       else
           return false;
   }
}

void fleet::printFleet(void) const { // prints out locations of ships in fleet
   for (int i = 0; i < fleetSize; ++i)
       ships[i].printShip();
}

bool fleet::check(location shiploc) {
   for (int i = 0; i < fleetSize; ++i)
       if (ships[i].match(shiploc))
       {
           return true;
       }
       else
       {
           return false;
       }
}

////////////////////////////////////////////////////////////////////////////

main.cpp:

#include "battleship.h"
#include <iostream>

using namespace std;

int main() {
   srand(time(NULL));
   char answer;
   fleet myFleet;   //computer's ships
   myFleet.deployFleet(); // fleet is deployed at random locations
   cout << "Do ships' positions and status need to be printed? (y for yes)" << endl;
   cin >> answer;
   if (answer == 'y')
       myFleet.printFleet();
   while (myFleet.operational() == true) {
       cout << "there are still ships up";
       location userShot;
       userShot.locationreset();
       cout << "Input location(a-e 1-5): ";
       userShot.fire();
       if (myFleet.isHitNSink(userShot) == true)
           cout << "hit";
       else
           cout << "miss";
       cout << "Do ships' positions and status need to be printed? (y for yes, q to quit)";
       cin >> answer;
       if (answer == 'y')
           myFleet.printFleet();
       else if (answer == 'q')
           break;
   }
}

Solutions

Expert Solution

If you are compiling below C++ codes in command line then compile it by using below line:

g++ main.cpp battleship.cpp

C++ code:

battleship.h

#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_

// coordinates (location) of the ship and shots
class location
{
public:
    void locationreset(void); // void constructor, assigns -1 to X
    void pick(void);          // picks a random location

    void fire(void); // asks the user to input the
    // coordinates of the next shot
    void print(void) const; // prints location in format "a1"

    // returns true if the two locations match
    friend bool compare(location, location);

private:
    static const int 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);                 // void constructor, sets sunk=false
    bool match(location) const; // returns true if this location matches
    // the ship's location
    bool isSunk(void) const { return (sunk); }; // checks to see if the ship is sunk
    void sink(void);                            // sets "sunk" member variable of the ship to true
    void setLocation(location);                 // deploys the ship at the specified location
    void printShip(void) 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(void); // deploys the ships in random locations
    // of the ocean
    bool operational(void) const; // returns true if at least
    // one ship in the fleet is not sunk
    bool isHitNSink(location); // returns true if there was a deployed
    // ship at this location (hit) and sinks it
    // otherwise returns false (miss)
    void printFleet(void) const; // prints out locations of ships in fleet

private:
    static const int fleetSize = 5; // number of battleships
    bool check(location);           // returns true if one of the ship's locations
    // matches the ship, returns false
    // if none-match
    ship ships[fleetSize]; // battleships of the fleet
};

#endif /* BATTLESHIP_H_ */

battleship.cpp

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"

using namespace std;

void location::locationreset(void)
{
    x = -1;
    y = 'x';
}

void location::pick(void)
{
    x = rand() % fieldSize + 1;
    y = rand() % fieldSize;
    switch (y)
    {
    case 0:
        y = 'a';
        break;
    case 1:
        y = 'b';
        break;
    case 2:
        y = 'c';
        break;
    case 3:
        y = 'd';
        break;
    case 4:
        y = 'e';
        break;
    }
}

void location::fire(void)
{
    cout << "\ninput the coordinates of the next shot: ";
    cin >> y >> x;
}
void location::print() const
{
    cout << y << x;
}
bool compare(location loc1, location loc2)
{
    return (loc1.x == loc2.x && loc1.y == loc2.y);
}

ship::ship(void)
{
    sunk = false;
}

bool ship::match(location shiploc) const
{
    return compare(loc, shiploc);
}

void ship::sink(void)
{
    sunk = true;
}

void ship::setLocation(location shiploc)
{ // deploys the ship at the specified location
    loc = shiploc;
}

void ship::printShip(void) const
{ // prints location and status of the ship
    loc.print();
    if (sunk == true)
        cout << " sunk" << endl;
    else if (sunk == false)
        cout << " not sunk" << endl;
}

void fleet::deployFleet(void)
{ // deploys the ships in random locations of the ocean
    location loc;
    loc.locationreset();
    for (int i = 0; i < fleetSize; i++)
    {
        loc.pick();
        ships[i].setLocation(loc);
    }
}

bool fleet::operational() const
{ // returns true if at least one ship in the fleet is not sunk
    for (int i = 0; i < fleetSize; ++i)
        if (ships[i].isSunk() == false)
            return true;
    return false;
}

bool fleet::isHitNSink(location shiploc)
{ // returns true if there was a deployed
    for (int i = 0; i < fleetSize; ++i)
    {
        if (ships[i].match(shiploc) == true && ships[i].isSunk() == false)
        {
            ships[i].sink();
            return true;
        }
    }
    return false;
}

void fleet::printFleet(void) const
{ // prints out locations of ships in fleet
    for (int i = 0; i < fleetSize; ++i)
        ships[i].printShip();
}

bool fleet::check(location shiploc)
{
    for (int i = 0; i < fleetSize; ++i)
        if (ships[i].match(shiploc))
            return true;
    return false;
}

main.cpp

#include "battleship.h"
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));
    char answer;
    fleet myFleet;         //computer's ships
    myFleet.deployFleet(); // fleet is deployed at random locations
    cout << "Do ships' positions and status need to be printed? (y for yes): ";
    cin >> answer;
    if (answer == 'y')
        myFleet.printFleet();
    while (myFleet.operational() == true)
    {
        cout << "\nthere are still ships up";
        location userShot;
        userShot.locationreset();
        cout << "\nInput location(a-e 1-5): ";
        userShot.fire();
        if (myFleet.isHitNSink(userShot) == true)
            cout << "hit";
        else
            cout << "miss";
        cout << "\nDo ships' positions and status need to be printed? (y for yes, q to quit): ";
        cin >> answer;
        if (answer == 'y')
            myFleet.printFleet();
        else if (answer == 'q')
            break;
    }
}

Output:


Related Solutions

Needs to be coded in Python. Hello i am working on a project for my computer...
Needs to be coded in Python. Hello i am working on a project for my computer programming course and i am having trouble with one part. The code needs to be able to produce two versions of an output given inputs by the user. for instance. Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or...
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
I am working on a project where I have to create my own organization (stem cell...
I am working on a project where I have to create my own organization (stem cell research) I need to go further in depth about the program or organization in terms of describing the program implementation and the project evaluation...
Professor, In trying to apply my knowledge in the real world, I am trying to create...
Professor, In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 22 and 68, in which I would deposit 25% of my income each year. The income starts at 80,000 with an annual growth rate...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
I am trying to solve this question: "You are working on a school project at the...
I am trying to solve this question: "You are working on a school project at the library when your friend Jane taps you on the shoulder. She cannot seem to connect to a certain website that she needs for her class. Fortunately, you know enough about Windows and networking to help troubleshoot the problem. You open a Windows command prompt and ......" The dots at the end of the story indicates that I need to continue the story. However, I...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
I am trying to create my own doubly linkedlist class and here is what I have...
I am trying to create my own doubly linkedlist class and here is what I have so far. my addfirst is adding elements forever and ever, and I could use some help. This is in C++. Please help me complete my doubly linkedlist class. the addfirst method is what I would like to complete. #ifndef DLLIST_H #define DLLIST_H #include "node.h" #include using namespace std; template class DLList { private:     Node* head = nullptr;     Node* tail = nullptr;    ...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
I am working on a project for my business class to creat a non profit organization!...
I am working on a project for my business class to creat a non profit organization! I came up with offering free WIFI/internet to everyone in the comfort of their own homes (without having to run to starbucks everytime you need to look something up) I am having trouble answering these two questions regarding my non profit so any advice would help! 5. Describe the type of legal structure that would be most appropriate for the non-profit organization. 6. Discuss...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT