Creating Retrievable Enemies

ok so please don't laugh at my code I have been writing C++ for about 3 weeks so I know its awful lol. I want to create class' of enemies that I can call on for battles with the player. I have successfully created and called a class but I seem to have to put the hit points for my enemy in the int main which surely means I am going to have to write out the hit points act for every type of enemy? any way I can create a class with integers in it so I just have to call the member of the class?

my source code is below..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
#include <cstdlib>
#include <iostream>

using namespace std;
       
    class enship
    {
          public:
          
          int earm;
          int espeed;
          int eweap;
          int emav;
          
    
    };
    enship pf;

int main()
{
    
     int arm = 30000; // input starting player stats arm= Armour, Speed = Speed, Weap = Weapon Power mav = Manouverability
     int speed = 30000;
     int weap = 30000;
     int mav = 30000;
  
  
  
 
  cout << "Welcome to your Starship Captain!.... \n We have to ship out immdiatley sir I am afraid the war marches on! \n";
  cout << "The RIS StarTrooper is unfortunatlety not the most powerful in the fleet but she is a Destroyer class so shes a good solid ship! \n";
  
 system("PAUSE");
 
   cout << "welcome to the bridge captain! \n We will be patrolling the supply lines to the front lines first... \n";
   cout << " There has been some pirate activity we belive is linked to the serpratists .. \n";
   cout << " It should be a good prooving ground for our new ship!  \n";
   cout << " Oh and one more thing... The Admiral will only send upgrades our way \n if we can prove ourselves in battle\n";
   
   
   system("PAUSE");
   
   cout << "Enemy ship has appeared and engaged us!\n";
   cout << emav << "\n"; 
   cout<< eweap << "\n";  
   cout<< espeed << "\n"; 
   cout << earm << "\n";
   
   enship pf;
    if ((earm + espeed + eweap + emav) < (arm + speed + weap + mav)) cout << "WE WON!\n";
    
   else if ((earm + espeed + eweap + emav) > (arm + speed + weap + mav)) cout << "We are going to have to retreat Captain!!\n";
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

Create a constructor for your class which will create a ship with needed paremeters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
enum ShipType {SHIP_INTERCEPTOR, SHIP_FRIGATE};

class Ship
{
public:
    Ship(int arm_, int speed_, int weap_, int mav_): //Parameter constructor
    arm(arm_), speed(speed_), weap(weap_), mav(mav_)
    {}

    explicit Ship(ShipType ship) //Enum constructor
    {
        switch(ship)
        {
        case SHIP_INTERCEPTOR:
            arm = 10000;
            speed = 40000;
            weap = 15000;
            mav = 40000;
            break;
        case SHIP_FRIGATE:
            arm = 30000;
            speed = 25000;
            weap = 40000;
            mav = 15000;
            break;
        }
    }

    int getPower()
    {
        return arm + speed + weap + mav;
    }

private:
    int arm;
    int speed;
    int weap;
    int mav;
};

int main()
{
    Ship playerShip(30000, 30000, 30000, 30000); //Constructing ship using explicit parameters
    Ship enemyShip(SHIP_FRIGATE); //Construct ship using enum constructor
    if (playerShip.getPower() > enemyShip.getPower())
        std::cout << "You win";
}

Last edited on
Topic archived. No new replies allowed.