Advertisement
makispaiktis

MY 1ST GAME

Aug 7th, 2018 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <ctime>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. //#include "Fighter.h"
  8. //#include "Swordsman.h"
  9.  
  10. using namespace std;
  11.  
  12.  
  13. // 1. Swordsman.h  (power 5-8)
  14. class Swordsman{
  15.  
  16.     unsigned int power;
  17.  
  18. public:
  19.  
  20.     // Constructors, destructor
  21.     Swordsman(){  power = ( rand()%4 ) + 5; }
  22.     Swordsman(unsigned int power){  this->power = power;  }
  23.     ~Swordsman(){  cout << "Swordsman with power " << power << " has been destroyed." << endl;  }
  24.  
  25.     // Setter, getter
  26.     void setPower(unsigned int i){  power = i;  }
  27.     unsigned int getPower(){  return power;  }
  28.  
  29.     // Methods
  30.     void show(){  cout << "Swordsman's power: " << power << endl;  }
  31.  
  32. };
  33.  
  34.  
  35. // 2. Archer.h  (power 4-6)
  36. class Archer{
  37.  
  38.     unsigned int power;
  39.  
  40. public:
  41.  
  42.     // Constructors, destructor
  43.     Archer(){  power = ( rand()%3 ) + 4; }
  44.     Archer(unsigned int power){  this->power = power;  }
  45.     ~Archer(){  cout << "Archer with power " << power << " has been destroyed." << endl;  }
  46.  
  47.     // Setter, getter
  48.     void setPower(unsigned int i){  power = i;  }
  49.     unsigned int getPower(){  return power;  }
  50.  
  51.     // Methods
  52.     void show(){  cout << "Archer's power: " << power << endl;  }
  53.  
  54. };
  55.  
  56.  
  57. // 3. Trooper.h  (power 10-12)
  58. class Trooper{
  59.  
  60.     unsigned int power;
  61.  
  62. public:
  63.  
  64.     // Constructors, destructor
  65.     Trooper(){  power = ( rand()%3 ) + 10; }
  66.     Trooper(unsigned int power){  this->power = power;  }
  67.     ~Trooper(){  cout << "Trooper with power " << power << " has been destroyed." << endl;  }
  68.  
  69.     // Setter, getter
  70.     void setPower(unsigned int i){  power = i;  }
  71.     unsigned int getPower(){  return power;  }
  72.  
  73.     // Methods
  74.     void show(){  cout << "Trooper's power: " << power << endl;  }
  75.  
  76. };
  77.  
  78.  
  79. // 4. Tank.h  (power 15-20)
  80. class Tank{
  81.  
  82.     unsigned int power;
  83.  
  84. public:
  85.  
  86.     // Constructors, destructor
  87.     Tank(){  power = ( rand()%6 ) + 15; }
  88.     Tank(unsigned int power){  this->power = power;  }
  89.     ~Tank(){  cout << "Tank with power " << power << " has been destroyed." << endl;  }
  90.  
  91.     // Setter, getter
  92.     void setPower(unsigned int i){  power = i;  }
  93.     unsigned int getPower(){  return power;  }
  94.  
  95.     // Methods
  96.     void show(){  cout << "Tank's power: " << power << endl;  }
  97.  
  98. };
  99.  
  100.  
  101. // 5. MythicalHero.h  (power 40-50)
  102. class MythicalHero{
  103.  
  104.     unsigned int power;
  105.  
  106. public:
  107.  
  108.     // Constructors, destructor
  109.     MythicalHero(){  power = ( rand()%11 ) + 40; }
  110.     MythicalHero(unsigned int power){  this->power = power;  }
  111.     ~MythicalHero(){  cout << "MythicalHero with power " << power << " has been destroyed." << endl;  }
  112.  
  113.     // Setter, getter
  114.     void setPower(unsigned int i){  power = i;  }
  115.     unsigned int getPower(){  return power;  }
  116.  
  117.     // Methods
  118.     void show(){  cout << "MythicalHero's power: " << power << endl;  }
  119.  
  120. };
  121.  
  122.  
  123.  
  124. // Auxiliary Function to create numbers which are near a given number
  125. int near(int n){
  126.  
  127.     int prosimo = rand() % 2;
  128.     int apolytiTimi = rand() % 6;
  129.     int apoklisi;
  130.  
  131.     // If prosimo = 0 then apoklisi = + apoltimi (+ because of 0), else if prosimo is 1, apoklisi =  - apoltimi (- because of 1)
  132.     if (prosimo == 0){
  133.  
  134.         apoklisi = apolytiTimi;
  135.     }
  136.  
  137.     else{
  138.  
  139.         apoklisi = -apolytiTimi;
  140.     }
  141.  
  142.     return (n + apoklisi);
  143.  
  144. }
  145.  
  146.  
  147.  
  148.  
  149. int main()
  150. {
  151.     srand(time(NULL));
  152.  
  153.     // For user's team
  154.     cout << "Give me 5 numbers as the numbers of the 5 different types of fighters." << endl;
  155.     cout << "The 1st, 2nd, 3rd, 4th and 5th number will be for: " << endl;
  156.     cout << "1. Swordsmen, 2. Archers, 3. Troopers, 4. Tanks, 5. MythicalHeroes" << endl << endl << endl;
  157.     unsigned int a1, a2, a3, a4, a5;
  158.     cin >> a1;
  159.     cin >> a2;
  160.     cin >> a3;
  161.     cin >> a4;
  162.     cin >> a5;
  163.  
  164.     // Creating the 5 types of fighters - Creating Classes' objects
  165.  
  166.     // 1. For Swordsmen
  167.     Swordsman swo;
  168.     Swordsman v1[a1];
  169.  
  170.     for(int i=0; i<a1; i++){
  171.  
  172.         v1[i] = swo;
  173.     }
  174.  
  175.     // 2. For Archers
  176.     Archer ar;
  177.     Archer v2[a2];
  178.  
  179.     for(int i=0; i<a2; i++){
  180.  
  181.         v2[i] = ar;
  182.     }
  183.  
  184.  
  185.     // 3. For Troopers
  186.     Trooper tr;
  187.     Trooper v3[a3];
  188.  
  189.     for(int i=0; i<a3; i++){
  190.  
  191.         v3[i] = tr;
  192.     }
  193.  
  194.     // 4. For Tanks
  195.     Tank tan;
  196.     Tank v4[a4];
  197.  
  198.     for(int i=0; i<a4; i++){
  199.  
  200.         v4[i] = tan;
  201.     }
  202.  
  203.  
  204.     // 5. For MythicalHeroes
  205.     MythicalHero mh;
  206.     MythicalHero v5[a5];
  207.  
  208.     for(int i=0; i<a5; i++){
  209.  
  210.         v5[i] = mh;
  211.     }
  212.  
  213.     cout << endl << "So, you chose your team, which is: " << endl << endl;
  214.     cout << "   " << a1 << " Swordsmen with power " << v1[0].getPower() * a1 << "  (One swordsman's power = " << v1[0].getPower() << ")" << endl;
  215.     cout << "   " << a2 << "   Archers with power " << v2[0].getPower() * a2 << "  (One    archer's power = " << v2[0].getPower() << ")" << endl;
  216.     cout << "   " << a3 << "  Troopers with power " << v3[0].getPower() * a3 << "  (One   trooper's power = " << v3[0].getPower() << ")" << endl;
  217.     cout << "   " << a4 << "     Tanks with power " << v4[0].getPower() * a4 << "  (One      tank's power = " << v4[0].getPower() << ")" << endl;
  218.     cout << "   " << a5 << " MythicalHeroes with power " << v5[0].getPower() * a5 << "  (One mythicalHero's power = " << v5[0].getPower() << ")" << endl;
  219.  
  220.     int POWER1 = v1[0].getPower() * a1 + v2[0].getPower() * a2 + v3[0].getPower() * a3 + v4[0].getPower() * a4 + v5[0].getPower() * a5;
  221.     cout << endl << "   " << "WHOLE POWER  =  " << POWER1 << endl << endl << endl;
  222.  
  223.  
  224.     // Creation of the enemy team
  225.     int near1 = near(a1);
  226.     int near2 = near(a2);
  227.     int near3 = near(a3);
  228.     int near4 = near(a4);
  229.     int near5 = near(a5);
  230.  
  231.     cout << endl << endl << "Enemy team: " << endl;
  232.  
  233.     cout << "   " << near1 << " Swordsmen with power " << v1[0].getPower() * near1 << "  (One swordsman's power = " << v1[0].getPower() << ")" << endl;
  234.     cout << "   " << near2 << "   Archers with power " << v2[0].getPower() * near2 << "  (One    archer's power = " << v2[0].getPower() << ")" << endl;
  235.     cout << "   " << near3 << "  Troopers with power " << v3[0].getPower() * near3 << "  (One   trooper's power = " << v3[0].getPower() << ")" << endl;
  236.     cout << "   " << near4 << "     Tanks with power " << v4[0].getPower() * near4 << "  (One      tank's power = " << v4[0].getPower() << ")" << endl;
  237.     cout << "   " << near5 << " MythicalHeroes with power " << v5[0].getPower() * near5 << "  (One mythicalHero's power = " << v5[0].getPower() << ")" << endl;
  238.  
  239.     int POWER2 = v1[0].getPower() * near1 + v2[0].getPower() * near2 + v3[0].getPower() * near3 + v4[0].getPower() * near4 + v5[0].getPower() * near5;
  240.     cout << endl << "   " << "WHOLE POWER  =  " << POWER2 << endl << endl;
  241.  
  242.     // Comparison between the 2 teams
  243.     cout << endl << endl << endl;
  244.  
  245.     if (POWER1 > POWER2){
  246.  
  247.         cout << "VICTORY! You won the enemy team!" << endl;
  248.         cout << "Difference in power between the 2 teams: " << POWER1 - POWER2 << endl;
  249.     }
  250.  
  251.     else{
  252.  
  253.         cout << "DEFEAT... You lost form the enemy team..." << endl;
  254.         cout << "Difference in power between the 2 teams: " << POWER2 - POWER1 << endl;
  255.     }
  256.  
  257.     cout << endl  << endl;
  258.  
  259.     return 0;
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement