Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective :Transform the single combined source code below into the more standar

ID: 3582438 • Letter: O

Question

Objective:Transform the single combined source code below into the more standard separated .h and .cpp source files. Determine which #includes are needed for each file. Find and fix errors where the code can not find symbols because of namespace/scope issues. Get the complete program compiled and running correctly.

Requirements: Work on this project in a new, empty directory/folder. Start with the source code. Break the code into separate source code files. There should be six files when you are done (Type.h, Ship.h, Ship.cpp, Quadrant.h, Quadrant.cpp, and Trek.cpp). Header files are required to include header guards and should not have any code implementation, just class or enum declarations. Header files should #include all the files that the associated implementation file will need to get its tasks done. Source files other than the one with the main function should only #include their associated header file. Try to compile the program using all the .cpp source files.

Correct the errors by doing the following: Add the appropriate #include statements to each file as needed (but no extra #includes), fix any header guard problems that you have in the code, add namespace/scope information to statements as needed so C++ can find the appropriate methods or fields.

Code:

Sample Run:

Explanation / Answer

Ship.h:

#include <string>
#include "Type.h"
#ifndef SHIP_H
#define SHIP_H
using namespace std;
class Ship {
private:
    static int numShips;
    static int typeShips[];
    static bool firstPass;

    static void initRand();
    static void log(string msg);

    Type type;
    string name;
    int energy;
    int shields;
    int torpedos;
    int x, y;
    void init(Type type, int energy, int shields, int torpedos, int x, int y, string name);

public:
    Ship();
    Ship(Type type, string name);
    Ship(Type type, int energy, int shields, int torpedos, int x, int y, string name);
    string toString() const;
    static int getNumberOfShips();
    static int getNumberOfShips(Type type);
    Type getType() const;
    string getTypeName() const;
    string getName() const;
    void destroy();
    int getX() const;
    int getY() const;
    void setX(int x);
    void setY(int y);
    bool operator==(Ship& rhs) const;
};

#endif

Ship.cpp:

#include <iostream>
#include <cstddef>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include "Ship.h"
using namespace std;

bool Ship::firstPass = false;
int Ship::numShips = 0;
int Ship::typeShips[] = { 0, 0 };

void Ship::initRand() {
    if (!firstPass) {
        firstPass = true;
        srand((unsigned int) time(NULL));
    }
}

Ship::Ship() {
    initRand();
    init(rand() % 2 == 0 ? Romulan : Federation, rand() % 2000 + 1000, rand() % 2000 + 1000, rand() % 20 + 10, -1, -1, string(""));
}

Ship::Ship(Type type, string name) {
    initRand();
    init(type, rand() % 2000 + 1000, rand() % 2000 + 1000, rand() % 20 + 10, -1, -1, name);
}

Ship::Ship(Type type, int energy, int shields, int torpedos, int x, int y, string name) {
    initRand();
    init(type, energy, shields, torpedos, x, y, name);
}

void Ship::init(Type type, int energy, int shields, int torpedos, int x, int y, string name) {
    initRand();
    this->type = type;
    this->energy = energy;
    this->shields = shields;
    this->torpedos = torpedos;
    this->x = x;
    this->y = y;
    this->name = name;
    numShips++;
    typeShips[static_cast<int>(type)]++;
}

string Ship::toString() const {
    char buf[500];
    sprintf_s(buf, "%s (%s) [%d,%d]: Energy %d, Shields %d, Torpedos %d",name.c_str(), getTypeName().c_str(), x, y, energy, shields, torpedos);
    return string(buf);
}

int Ship::getNumberOfShips() { return numShips; }
int Ship::getNumberOfShips(Type type) { return typeShips[static_cast<int>(type)]; }

void Ship::destroy() {
    char buf[500];
    this->energy = 0;
    this->shields = 0;
    this->torpedos = 0;
    numShips--;
    typeShips[static_cast<int>(type)]--;
    sprintf_s(buf, "The %s ship %s has been destroyed", getTypeName().c_str(), name.c_str());
    log(string(buf));
}

void Ship::log(string msg) { cout << msg << endl; }
Type Ship::getType() const { return type; }
string Ship::getTypeName() const { return string(static_cast<int>(type) == 0 ? "Federation" : "Romulan"); }
string Ship::getName() const { return name; }
int Ship::getX() const { return x; }
int Ship::getY() const { return y; }
void Ship::setX(int x) { this->x = x; }
void Ship::setY(int y) { this->y = y; }
bool Ship::operator==(Ship& rhs) const { return this == &rhs; }

Quadrant.h:

#include <vector>
#include "Ship.h"
#ifndef QUADRANT_H // prevent the Quadrant.h file from being compiled more than one time
#define QUADRANT_H
using namespace std;
class Quadrant {
private:
    vector<Ship> ships;
    static bool firstPass;
    static void initRand();
    static void log(string msg);
public:
    static const int WIDTH = 30;
    static const int HEIGHT = 20;
    Ship& addShip(Ship& ship);
    Ship& addShip(Ship& ship, int x, int y);
    string toString() const;
    string shipRoster() const;
    vector<Ship> getShipsAt(int x, int y) const;
};
#endif //QUADRANT_H

Quadrant.cpp:

#include <iostream>
#include <cstddef>
#include <ctime>
#include <string>
#include <cstdio>
#include <stdlib.h>
#include <vector>
#include "Quadrant.h"
#include "Ship.cpp"
using namespace std;
bool Quadrant::firstPass = false;
void Quadrant::initRand() {
    if (!firstPass) {
        firstPass = true;
        srand((unsigned int) time(NULL));
    }
}
Ship& Quadrant::addShip(Ship& ship) {
    initRand();
    int x, y;

    if (ship.getX() == -1 || ship.getY() == -1) {
        do {
            x = rand() % WIDTH;
            y = rand() % HEIGHT;
        } while (getShipsAt(x, y).size() > 0);
    }
    else {
        x = ship.getX();
        y = ship.getY();
    }
    addShip(ship, x, y);
    return ship;
}

Ship& Quadrant::addShip(Ship& ship, int x, int y) {
    char buf[500];
    vector<Ship> lst = getShipsAt(x, y);
    if (lst.size() != 0) {
        // collision(s)
        for (unsigned int i = 0; i<lst.size(); i++) {
            sprintf_s(buf, "The %s ship %s has collided with the %s ship %s",
                ship.getTypeName().c_str(), ship.getName().c_str(),
                lst[i].getTypeName().c_str(), lst[i].getName().c_str());
            log(string(buf));
            for (unsigned int j = 0; j < ships.size(); j++) {
                if (ships[i] == lst[i]) ships.erase(ships.begin() + j);
            }
            lst[i].destroy();
        }
        ship.destroy();
    }
    else {
        ship.setX(x);
        ship.setY(y);
        ships.push_back(ship);
    }
    return ship;
}

void Quadrant::log(string msg) {
    cout << msg << endl;
}

string Quadrant::toString() const {
    string s;
    char buf[10];
    char quadrant[HEIGHT][WIDTH];
    for (int r = 0; r<HEIGHT; r++) {
        for (int c = 0; c<WIDTH; c++) {
            quadrant[r][c] = ' ';
        }
    }

    for (unsigned int i = 0; i<ships.size(); i++) {
        int x = ships[i].getX();
        int y = ships[i].getY();
        if (quadrant[y][x] != ' ') quadrant[y][x] = '*';
        else quadrant[y][x] = ships[i].getTypeName().at(0);
    }

    for (int r = 0; r<HEIGHT; r++) {
        for (int c = 0; c<WIDTH; c++) {
            sprintf_s(buf, "%c ", quadrant[r][c]);
            s += string(buf);
        }
        s += " ";
    }
    return s;
}

string Quadrant::shipRoster() const {
    string s;
    char buf1[10], buf2[10], buf3[10];
    for (unsigned int i = 0; i<ships.size(); i++) s += ships[i].toString() + " ";
    sprintf_s(buf1, "%d", getNumberOfShips());
    sprintf_s(buf2, "%d", getNumberOfShips(Romulan));
    sprintf_s(buf3, "%d", getNumberOfShips(Federation));

    s += string("Total ships: ") + string(buf1) + string(" ");
    s += string("Romulan ships: ") + string(buf2) + string(" ");
    s += string("Federation ships: ") + string(buf3) + string(" ");
    return s;
}

vector<Ship> Quadrant::getShipsAt(int x, int y) const {
    vector<Ship> lst;
    for (unsigned int i = 0; i<ships.size(); i++) if (ships[i].getX() == x && ships[i].getY() == y) lst.push_back(ships[i]);
    return lst;
}

Type.h:

#ifndef TYPE_H
#define TYPE_H
enum Type { Federation, Romulan };
#endif //TYPE_H

Trek.cpp:

#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <cstdio>
#include "Type.h"
#include "Ship.cpp"
#include "Quadrant.cpp"
using namespace std;
string getUnusedName(vector<string>& names);

int main(int argc, char* argv[]) {
    srand((unsigned int) time(NULL));
    Quadrant quad;

    vector<string> namesRom;
    vector<string> namesFed;
    string romnm[] = { "Valdore", "Hathas", "Vas Hatham", "Scimitar",
        "Aelignne", "Firebat", "Klamath", "Caladan", "Vreedex", "vas'Kalabam",
        "Galan Stelri", "Takara Morlatta", "Vithrel", "Comilius", "Reemea" };
    string fednm[] = { "Adelphi", "Zhukov", "Drake", "Horatio",
        "Zapata", "Bradbury", "Fleming", "Surak", "Appalachia", "Bozeman", "Sentinel",
        "President", "Tempest", "Olympic", "Houston" };
    for (int i = 0; i<15; i++) {
        namesRom.push_back(romnm[i]);
        namesFed.push_back(fednm[i]);
    }

    Type type;
    string name;
    Ship unlucky;

    for (int i = 0; i<20; i++) {
        if (rand() % 2 == 0) {
            type = Federation;
            name = getUnusedName(namesFed);
        }
        else {
            type = Romulan;
            name = getUnusedName(namesRom);
        }
        Ship ship(type, name);
        quad.addShip(ship);
        if (i == 0 || (rand() % 5 == 0 && type == Romulan)) unlucky = ship;
    }


    Ship destroyer(Romulan, 1000, 1000, 1000,
        unlucky.getX(), unlucky.getY(), "Reaper");
    quad.addShip(destroyer);

    cout << quad.toString() << endl;
    cout << quad.shipRoster() << endl;

    system("pause");
    return 0;
}

string getUnusedName(vector<string>& names) {
    string s;
    if (names.size() == 0) {
        s = "Unknown";
    }
    else {
        int i = rand() % names.size();
        s = names[i];
        names.erase(names.begin() + i);
    }
    return s;
}