C++ Trek Wars Project:
100% code coverage unit testing is required for this project.
once a ship has reached 0 health it is considered blown up
-throw an exception if the move method is called on it.
-repair ships cannot repair ships at 0 health
There will be no chaotic corvette ships
Ship :
#include <iostream>
using namespace std;
enum Alignment{us,them,chaotic};
class Ship{
protected:
string name;
Alignment align;
int xLoc;
int yLoc;
int range;
int currentHealth;
int attackPower;
int maxHealth;
public:
Ship(string name,int x,int
y,Alignment align,int maxHealth,int range,int attackPower){
this->name =
name;
xLoc = x;
yLoc = y;
this->align =
align;
this->maxHealth = maxHealth;
this->range =
range;
this->attackPower = attackPower;
currentHealth =
maxHealth;
}
virtual void attack(Ship
target){
}
virtual string getType(){
return
"Ship";
}
virtual void move(){
}
int getX(){
return
xLoc;
}
int getY(){
return
yLoc;
}
Alignment getAlign(){
return
align;
}
string status(){
cout<<"Name : "<<name<<endl;
cout<<"Type : "<<getType()<<endl;
cout<<"Health : "<<currentHealth<<endl;
cout<<"Location :
"<<"("<<xLoc<<","<<yLoc<<")"<<endl;
}
void changeAlign(){
if(align ==
us){
align = them;
}
else if (align
== them){
align = us;
}
}
void accessDamage(int amt){
if(amt > 0
&& amt < currentHealth){
currentHealth = currentHealth - amt;
}
else if
(amt>=currentHealth){
currentHealth = 0;
}
}
void repair(){
if(currentHealth==0){
throw "Ship is destroyed";
}
currentHealth =
maxHealth;
}
};
Battle :
#include "Ship.cpp"
#include <cmath>
class Battle : public Ship{
private:
int torpedoes;
public:
Battle(string name,int x,int
y,Alignment align):Ship(name,x,y,align,100,10,10){
torpedoes =
10;
}
void attack(Ship &
target){
if(align !=
target.getAlign() && torpedoes>0 &&
(sqrt(((target.getX() - getX())*(target.getX() - getX())) +
((target.getY() - getY())*(target.getY() - getY()))) <=
range)){
target.accessDamage(10);
torpedoes--;
}
}
string getType(){
return
"Battle";
}
void move(){
if(currentHealth==0){
throw "Ship is destroyed";
}
xLoc--;
yLoc--;
if(currentHealth<maxHealth){
currentHealth++;
}
}
string status(){
Ship::status();
cout<<"Torpedoes : "<<torpedoes<<endl;
}
};
Cruiser :
#include "Ship.cpp"
#include <cmath>
class Cruiser : public Ship{
public:
Cruiser(string name,int x,int
y,Alignment align):Ship(name,x,y,align,50,50,5){
}
void attack(Ship &
target){
if(align !=
target.getAlign() && (sqrt(((target.getX() -
getX())*(target.getX() - getX())) + ((target.getY() -
getY())*(target.getY() - getY()))) <= range)){
target.accessDamage(5);
}
}
string getType(){
return
"Cruiser";
}
void move(){
if(currentHealth==0){
throw "Ship is destroyed";
}
xLoc++;
yLoc++;
yLoc++;
if(currentHealth<maxHealth){
currentHealth++;
}
}
};
Corvette :
#include "Ship.cpp"
#include <cmath>
class Corvette : public Ship{
public:
Corvette(string name,int x,int
y,Alignment align):Ship(name,x,y,align,20,25,0){
}
void attack(Ship &
target){
if(align !=
target.getAlign() && (sqrt(((target.getX() -
getX())*(target.getX() - getX())) + ((target.getY() -
getY())*(target.getY() - getY()))) <= range)){
target.changeAlign();
}
}
string getType(){
return
"Corvette";
}
void move(){
if(currentHealth==0){
throw "Ship is destroyed";
}
xLoc = xLoc +
5;
yLoc = yLoc +
5;
if(currentHealth<maxHealth){
currentHealth++;
}
}
};
Repair :
#include "Cruiser.cpp"
#include <cmath>
class Repair : public Cruiser{
public:
Repair(string name,int x,int
y,Alignment align):Cruiser(name,x,y,align){
range =
25;
maxHealth =
20;
}
void attack(Ship &
target){
if(align ==
target.getAlign() && (sqrt(((target.getX() -
getX())*(target.getX() - getX())) + ((target.getY() -
getY())*(target.getY() - getY()))) <= range)){
target.repair();
}
}
string getType(){
return
"Repair";
}
};
C++ Trek Wars Project: 100% code coverage unit testing is required for this project. once a ship has reached 0 health it...