Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FightModus.h
- #ifndef FIGHTMODUS_H
- #define FIGHTMODUS_H
- #include "Modus.h"
- class FightModus:public Modus
- {
- public:
- FightModus(const FightModus& orig);
- virtual ~FightModus();
- string execute();
- FightModus(Modus* prev);
- void monsterTurn();
- bool checkActiveTurn();
- void selectMonster(Player* player);
- bool checkFightEnd();
- void checkRoundEnd();
- private:
- Player** activePlayer_;
- Player** passivePlayer_;
- bool active_turn_;
- };
- #endif /* FIGHTMODUS_H */
- FightModus.cpp
- /*
- * File: FightModus.cpp
- * Author: dave
- *
- * Created on 19. April 2011, 22:39
- */
- #include "FightModus.h"
- #include "UserInterface.h"
- #include "Database.h"
- #include "ModusManager.h"
- #include "Modus.h"
- #include "Monster.h"
- #include "CommandDex.h"
- #include "CommandUse.h"
- #include "CommandAttack.h"
- #include "CommandStatus.h"
- #include "CommandSwap.h"
- #include "CommandItems.h"
- #include "CommandResign.h"
- #include "Player.h"
- #include <iostream>
- using std::cout;
- using std::endl;
- FightModus::FightModus(Modus* prev_mode,Player** activePlayer,Player** passivePlayer)
- : Modus(prev_mode),activePlayer_(activePlayer),passivePlayer_(passivePlayer)
- {
- string active_player = (*(ModusManager::getActivePlayer()))->getName();
- active_player = " ["+active_player+"]"+" - fight> ";
- prompt_=active_player;
- possible_commands_.push_back(new CommandDex());
- possible_commands_.push_back(new CommandUse());
- possible_commands_.push_back(new CommandAttack(
- ModusManager::getActivePlayer(),ModusManager::getPassivePlayer()));
- possible_commands_.push_back(new CommandStatus());
- possible_commands_.push_back(
- new CommandSwap(ModusManager::getActivePlayer()));
- possible_commands_.push_back(new CommandItems());
- possible_commands_.push_back(new CommandResign(end_flag_));
- }
- FightModus::~FightModus()
- {
- }
- string FightModus::execute()
- {
- //spieleprofil bereitstellen fÃŒr pokemon daten
- //abfragen
- Database* data = *(ModusManager::getActiveData());
- string input;
- Monster* enemy;
- vector<Monster*> monsters = (*(ModusManager::getActivePlayer()))->getMonster();
- unsigned int int_value;
- //endlossschleifen fÃŒr voreinstellungen definieren
- while(true)
- {
- while(true)
- {
- cout << " Monster ID? ";
- input =UserInterface::readString();
- if (UserInterface::stringToUnsignedInt(input, int_value))
- {
- //user Id Eingabe ist Integer
- //wenn ja, ÃŒberprÃŒfen ob id existiert
- if( (enemy = data->searchMonster(int_value)) != NULL)
- break;
- else
- cout << "Error: ID doesn't exist." << endl;
- } else {
- cout << "Error: Please enter a not negative integer." << endl; //fehlercatch "Error: ID doesn't exist."
- }
- }
- //monster level abfragen
- while(true)
- {
- cout << " Monster level? ";
- input = UserInterface::readString() ;
- if (UserInterface::stringToUnsignedInt(input, int_value)) {
- //user Id Eingabe ist Integer
- //wenn ja, ÃŒberprÃŒfen ob id existiert
- if(int_value >=1 && int_value <= 100)
- {
- enemy = new Monster(enemy);
- enemy->setDefense(int_value*enemy->getDefense());//werte multipl. mit level setzen
- enemy->setHealth(int_value*enemy->getHealth());
- enemy->setAttack(int_value*enemy->getAttack());
- enemy->setCurrentHealth(enemy->getHealth());
- enemy->setLvl(int_value);
- break;
- }
- else
- cout <<"Error: Please enter a level between 1 and 100" << endl;
- } else {
- cout << "Error: Please enter a not negative integer."<< endl; //fehlercatch "Error: ID doesn't exist."
- }
- }
- break;
- }
- //fragen ob pokemon mit mehr als 0 HP im Profil ist
- bool found = false;
- for(unsigned int i = 0; i < monsters.size(); i++)
- {
- if(monsters.at(i)->getCurrentHealth() != 0)
- {
- found = true;
- break;
- }
- }
- if(found == false)
- {
- cout << (*(ModusManager::getActivePlayer()))->getName()<<" lost!";
- return;
- ////fight modus beendet????
- }
- cout << (*(ModusManager::getActivePlayer()))->getName() <<" - Select a monster!";
- while(true)
- {
- input =UserInterface::readString();
- if (UserInterface::stringToUnsignedInt(input, int_value))
- {
- if(monsters.at(int_value)!= NULL && (int_value >=1&& int_value<=6) ){
- (*(ModusManager::getActivePlayer()))->setActiveMonster(monsters.at(int_value)->getId());
- }
- else
- cout <<"Error: Monster in this slot is not able to fight" << endl;
- }
- else
- {
- cout << "Error: Please enter a not negative integer." << endl; //fehlercatch "Error: ID doesn't exist."
- }
- }
- end_flag_ = true;
- string command;
- active_turn_= true;
- passivePlayer_->setRound(0);
- ///execute von modus jeweils nur einmal ausfÃŒhren und dann zurÃŒckkehren zu
- //modus
- do
- {
- Monster *used_Monster = activePlayer_->getActiveMonster();
- command = Modus::execute();
- //check if command ends hero turn
- if(command =="use"||command ="swap"||command="attack")
- {
- if (command == "swap"
- && (*used_Monster) != (*((*activePlayer_)->getActiveMonster())))
- active_turn_= false;
- else if (command == "attack")
- active_turn_ = false;
- else if (command == "use")
- active_turn_ = false;
- }
- checkRoundEnd();
- }
- while(true);
- delete enemy;
- return command;
- }
- void FightModus::monsterTurn()
- {
- //(n * Gegner HP * Eigene HP) % (Anzahl der Attacken).
- unsigned int attack_pos = (passivePlayer_->getRound() * passivePlayer_
- ->getActiveMonster()
- ->getCurrentHealth() * activePlayer_->getActiveMonster()
- ->getCurrentHealth() ) % passivePlayer_->getActiveMonster()
- ->getAttacks().size();
- passivePlayer_->getActiveMonster()->attack(attack_pos);
- }
- void FightModus::checkRoundEnd()
- {
- ///alle Werte ausarbeiten nach beendigung der Runde
- ///1.abftagen ob Effekte Monster beschÀdigt
- ///2.abfragen ob eigenes monster tot ist
- ///3.select monster aufrufen
- ///4.abfragen ob Kampf zu ende ist
- if(checkFightEnd())
- {
- cout << (*activePlayer_)->getName()<< " lost!";
- ///preisgeld berechnen
- }
- if(activePlayer_->getActiveMonster()->getCurrentHealth() == 0)
- selectMonster(*(activePlayer_));
- if(active_turn_ == false)
- monsterTurn();
- }
- bool FightModus::checkFightEnd()
- {
- if ((*activePlayer_)->getActiveMonster()->getCurrentHealth() > 0)
- return false;
- vector<Monster*> monsters = (*activePlayer_)->getMonster();
- for (unsigned int counter = 0; counter < monsters.size(); counter++)
- {
- if (monsters.at(counter) != NULL
- && monsters.at(counter)->getCurrentHealth() > 0)
- {
- return false;
- }
- }
- return true;
- }
- void FightModus::selectMonster(Player* player) {
- unsigned int id;
- do {
- cout << player->getName() << " - Select a monster! ";
- if (UserInterface::stringToUnsignedInt(UserInterface::readString(), id)) {
- try {
- id--;
- Monster* monster = (player)->getMonster().at(id);
- if (monster == NULL || monster->getCurrentHealth() == 0)
- throw std::out_of_range(""); //Force error message
- player->setActiveMonster(id);
- break;
- } catch (std::out_of_range) {
- cout << "Error: Monster in this slot is not able to fight." << endl;
- }
- }
- } while (true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement