Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.36 KB | None | 0 0
  1.    
  2. FightModus.h
  3.  
  4. #ifndef FIGHTMODUS_H
  5. #define FIGHTMODUS_H
  6. #include "Modus.h"
  7.  
  8. class FightModus:public Modus
  9. {
  10. public:
  11.   FightModus(const FightModus& orig);
  12.   virtual ~FightModus();
  13.   string execute();
  14.   FightModus(Modus* prev);
  15.   void monsterTurn();
  16.   bool checkActiveTurn();
  17.   void selectMonster(Player* player);
  18.   bool checkFightEnd();
  19.   void checkRoundEnd();
  20. private:
  21.     Player** activePlayer_;
  22.     Player** passivePlayer_;
  23.     bool active_turn_;
  24.  
  25. };
  26.  
  27. #endif  /* FIGHTMODUS_H */
  28.  
  29.  
  30.  
  31. FightModus.cpp
  32.  
  33. /*
  34.  * File:   FightModus.cpp
  35.  * Author: dave
  36.  *
  37.  * Created on 19. April 2011, 22:39
  38.  */
  39.  
  40. #include "FightModus.h"
  41. #include "UserInterface.h"
  42. #include "Database.h"
  43. #include "ModusManager.h"
  44. #include "Modus.h"
  45. #include "Monster.h"
  46. #include "CommandDex.h"
  47. #include "CommandUse.h"
  48. #include "CommandAttack.h"
  49. #include "CommandStatus.h"
  50. #include "CommandSwap.h"
  51. #include "CommandItems.h"
  52. #include "CommandResign.h"
  53. #include "Player.h"
  54.  
  55. #include <iostream>
  56.  
  57. using std::cout;
  58. using std::endl;
  59.  
  60. FightModus::FightModus(Modus* prev_mode,Player** activePlayer,Player** passivePlayer)
  61. : Modus(prev_mode),activePlayer_(activePlayer),passivePlayer_(passivePlayer)
  62. {
  63.   string active_player = (*(ModusManager::getActivePlayer()))->getName();
  64.  
  65.   active_player = "  ["+active_player+"]"+" - fight> ";
  66.   prompt_=active_player;
  67.  
  68.  
  69.   possible_commands_.push_back(new CommandDex());
  70.   possible_commands_.push_back(new CommandUse());
  71.   possible_commands_.push_back(new CommandAttack(
  72.   ModusManager::getActivePlayer(),ModusManager::getPassivePlayer()));
  73.   possible_commands_.push_back(new CommandStatus());
  74.   possible_commands_.push_back(
  75.   new CommandSwap(ModusManager::getActivePlayer()));
  76.   possible_commands_.push_back(new CommandItems());
  77.   possible_commands_.push_back(new CommandResign(end_flag_));
  78. }
  79.  
  80.  
  81.  
  82. FightModus::~FightModus()
  83. {
  84. }
  85.  
  86. string FightModus::execute()
  87. {
  88.     //spieleprofil bereitstellen fÃŒr pokemon daten
  89.     //abfragen
  90.  
  91.     Database* data = *(ModusManager::getActiveData());
  92.     string input;
  93.     Monster* enemy;
  94.     vector<Monster*> monsters = (*(ModusManager::getActivePlayer()))->getMonster();
  95.     unsigned int int_value;
  96.  
  97.    
  98.  
  99.     //endlossschleifen fÃŒr voreinstellungen definieren
  100.  
  101.     while(true)
  102.     {
  103.         while(true)
  104.          {
  105.             cout << "  Monster ID? ";
  106.              input =UserInterface::readString();
  107.             if (UserInterface::stringToUnsignedInt(input, int_value))
  108.             {
  109.                
  110.                 //user Id Eingabe ist Integer
  111.                 //wenn ja, ÃŒberprÃŒfen ob id existiert
  112.                 if( (enemy = data->searchMonster(int_value))  != NULL)
  113.                     break;
  114.                 else
  115.                     cout <<  "Error: ID doesn't exist." << endl;
  116.  
  117.             } else {
  118.                 cout << "Error: Please enter a not negative integer." << endl;  //fehlercatch "Error: ID doesn't exist."
  119.             }
  120.          
  121.  
  122.         }
  123.  
  124.         //monster level abfragen
  125.         while(true)
  126.          {
  127.         cout << "  Monster level? ";
  128.         input = UserInterface::readString() ;
  129.          if (UserInterface::stringToUnsignedInt(input, int_value)) {
  130.  
  131.                 //user Id Eingabe ist Integer
  132.                 //wenn ja, ÃŒberprÃŒfen ob id existiert
  133.                 if(int_value >=1 && int_value <= 100)
  134.                 {
  135.                     enemy = new Monster(enemy);
  136.                     enemy->setDefense(int_value*enemy->getDefense());//werte multipl. mit level setzen
  137.                     enemy->setHealth(int_value*enemy->getHealth());
  138.                     enemy->setAttack(int_value*enemy->getAttack());
  139.                     enemy->setCurrentHealth(enemy->getHealth());
  140.                     enemy->setLvl(int_value);
  141.                     break;
  142.                 }
  143.                 else
  144.                     cout <<"Error: Please enter a level between 1 and 100" << endl;
  145.  
  146.             } else {
  147.                 cout << "Error: Please enter a not negative integer."<< endl;  //fehlercatch "Error: ID doesn't exist."
  148.             }
  149.  
  150.    
  151.          }
  152.         break;
  153.  
  154.     }
  155.     //fragen ob pokemon mit mehr als 0 HP im Profil ist
  156.  
  157.     bool found = false;
  158.     for(unsigned int i = 0; i < monsters.size(); i++)
  159.     {
  160.         if(monsters.at(i)->getCurrentHealth() != 0)
  161.         {
  162.             found = true;
  163.             break;
  164.         }    
  165.        
  166.     }
  167.     if(found == false)
  168.     {
  169.         cout <<  (*(ModusManager::getActivePlayer()))->getName()<<" lost!";
  170.         return;
  171.     ////fight modus beendet????
  172.     }
  173.     cout << (*(ModusManager::getActivePlayer()))->getName() <<"  - Select a monster!";
  174.     while(true)
  175.     {
  176.     input =UserInterface::readString();
  177.             if (UserInterface::stringToUnsignedInt(input, int_value))
  178.             {
  179.                 if(monsters.at(int_value)!= NULL && (int_value >=1&& int_value<=6) ){
  180.                 (*(ModusManager::getActivePlayer()))->setActiveMonster(monsters.at(int_value)->getId());
  181.  
  182.                
  183.                 }
  184.                 else
  185.                     cout <<"Error: Monster in this slot is not able to fight" << endl;
  186.              
  187.  
  188.             }
  189.             else
  190.             {
  191.                 cout << "Error: Please enter a not negative integer." << endl;  //fehlercatch "Error: ID doesn't exist."
  192.             }
  193.     }
  194.     end_flag_ = true;
  195.     string command;
  196.     active_turn_= true;
  197.     passivePlayer_->setRound(0);
  198.     ///execute von modus jeweils nur einmal ausfÃŒhren und dann zurÃŒckkehren zu
  199.     //modus
  200.     do
  201.     {
  202.         Monster *used_Monster = activePlayer_->getActiveMonster();
  203.       command =  Modus::execute();
  204.       //check if command ends hero turn
  205.       if(command =="use"||command ="swap"||command="attack")
  206.       {
  207.            if (command == "swap"
  208.           && (*used_Monster) != (*((*activePlayer_)->getActiveMonster())))
  209.                active_turn_= false;
  210.       else if (command == "attack")
  211.           active_turn_ = false;
  212.       else if (command == "use")
  213.           active_turn_ = false;
  214.       }
  215.      
  216.       checkRoundEnd();
  217.     }
  218.      while(true);
  219.  
  220.    
  221.    delete enemy;
  222.  
  223.    return command;
  224. }
  225.  
  226.  
  227.  
  228. void FightModus::monsterTurn()
  229. {
  230.     //(n * Gegner HP * Eigene HP) % (Anzahl der Attacken).
  231.   unsigned int attack_pos = (passivePlayer_->getRound() * passivePlayer_
  232.             ->getActiveMonster()
  233.             ->getCurrentHealth() * activePlayer_->getActiveMonster()
  234.             ->getCurrentHealth() ) % passivePlayer_->getActiveMonster()
  235.             ->getAttacks().size();
  236.   passivePlayer_->getActiveMonster()->attack(attack_pos);
  237.            
  238. }
  239.  
  240. void FightModus::checkRoundEnd()
  241. {
  242.     ///alle Werte ausarbeiten nach beendigung der Runde
  243.     ///1.abftagen ob Effekte Monster beschÀdigt
  244.     ///2.abfragen ob eigenes monster tot ist
  245.     ///3.select monster aufrufen
  246.     ///4.abfragen ob Kampf zu ende ist
  247.     if(checkFightEnd())
  248.     {
  249.         cout << (*activePlayer_)->getName()<< " lost!";
  250.         ///preisgeld berechnen
  251.  
  252.     }
  253.     if(activePlayer_->getActiveMonster()->getCurrentHealth() == 0)
  254.           selectMonster(*(activePlayer_));
  255.       if(active_turn_ == false)
  256.           monsterTurn();
  257.  
  258. }
  259. bool FightModus::checkFightEnd()
  260. {
  261.   if ((*activePlayer_)->getActiveMonster()->getCurrentHealth() > 0)
  262.     return false;
  263.  
  264.   vector<Monster*> monsters = (*activePlayer_)->getMonster();
  265.  
  266.   for (unsigned int counter = 0; counter < monsters.size(); counter++)
  267.   {
  268.     if (monsters.at(counter) != NULL
  269.         && monsters.at(counter)->getCurrentHealth() > 0)
  270.     {
  271.       return false;
  272.     }
  273.  
  274.   }
  275.  
  276.  
  277.   return true;
  278. }
  279.  
  280.  
  281.   void FightModus::selectMonster(Player* player) {
  282.         unsigned int id;
  283.  
  284.  
  285.         do {
  286.             cout << player->getName() << " - Select a monster! ";
  287.             if (UserInterface::stringToUnsignedInt(UserInterface::readString(), id)) {
  288.                 try {
  289.                     id--;
  290.                     Monster* monster = (player)->getMonster().at(id);
  291.                     if (monster == NULL || monster->getCurrentHealth() == 0)
  292.                         throw std::out_of_range(""); //Force error message
  293.  
  294.                     player->setActiveMonster(id);
  295.                     break;
  296.  
  297.                 } catch (std::out_of_range) {
  298.                     cout << "Error: Monster in this slot is not able to fight." << endl;
  299.                 }
  300.             }
  301.  
  302.         } while (true);
  303.  
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement