Question

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

Trek Wars Purpose: classes, inheritance, class diagrams, virtual, testing Description Universal Merriment Development (UMD) iBattle (derived from the Ship class) range is 10, maxHealth =100 ,attack = 10 attributes torpedoes // int initially 10 method

Trek Wars Purpose: classes, inheritance, class diagrams, virtual, testing Description Universal Merriment Development (UMD) is creating an online space battle game called (Trek Wars) You are tasked with creating some classes for this game. Again you have to just write the class and test it. A description of the classes is given below: Figure 1: Graphics from the game. enum Alignment{us, them, chaotic}; Ship (the base class) attributes name string align Alignment xLoc integer yLoc integer range: int currentHealth : integer attackPower: integer maxHealth: integer methods public Ship(name string, x : integer, y integer, align Alignment, maxHealth integer, range integer, attackPower integer ); // currHealth is set to maximum public virtual attack(target: * Ship) : void // private virtual getType () string //'Battleship", "Cruiser", "Corvette", "Repairship" public getX():int// returns the x coordinate public getY()int // returns the y coordinate public getAlign():Alignment // returns the alignment public status () : string// see format below public virtual move () : void // changes position by the amount of that type of ship, // increases health by 1 (until max reached) public changeAlign() :void // changes the alignment. public accessDamage (amt : int) void // changes the health by amt, // (keeping it within bounds [0,maxHealth])
Battle (derived from the Ship class) range is 10, maxHealth =100 ,attack = 10 attributes torpedoes // int initially 10 methods // always moves along the vector (-1, -1) move() attack(target: Ship *) void// attacks and fires torpedo >0 and //does additional 10 damage, 1 less torpedo status ()// also indicates number of torpedoes Cruiser (derived from Ship class) range is 50, maxHealth =50, attack = 5 method // always moves along the vector (1, 2) move() attack(target: Ship *): void //attacks 5 Corvette (derived from Ship class) range is 25, maxHealth -20 method // always moves along the vector (5, 5) move() attack(target: Ship *): void //Everyone loves corvettes so their attack // flips ships in range to its state // (if self is us, turns them to us, // if self is them, turns us to them) Repair (derived from Cruiser class) range is 25, maxHealth = 20 method attack(target: Ship *): void //its attack repairs a ship of own kind to max health Input No input Output For status() print each name: type health: location: (xLoc, yLoc) torpedoes: // only if this is a battleship Except for repair ships, attacks only work if the target of the attack is of the opposite alignment and in range.. By opposite alignment, we mean (us attacks them and them attacks us). Chaotic ships attack everyone. Repair ships only attack (in this case repair), ships of the same alignment.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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";
       }  
};

Add a comment
Know the answer?
Add Answer to:
C++ Trek Wars Project: 100% code coverage unit testing is required for this project. once a ship has reached 0 health it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT