Objective :Transform the single combined source code below into the more standar
ID: 3581108 • 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 C++ 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
// Type.h
#ifndef Type_H
#define Type_H
enum Type { Federation, Romulan };
#endif
// Ship.h
#ifndef Ship_H
#define Ship_H
#include<string>
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<Ship.h>
#include<Type.h>
#include<cstdlib>
#include<cstdio>
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
#ifndef Quadrant_H
#define Quadrant_H
#include<vector>
#include<Ship.h>
#include<string>
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.cpp
#include<Quadrant.h>
#include<cstdlib>
#include<ctime>
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;
}
//Trek.cpp
#include<vector>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<string>
#include<cstdio>
#include<Ship.h>
#include<Type.h>
#include<Quadrant.h>
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.