Thread: Help with a method

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Help with a method

    I'm trying to create a method called beercanblitz that will double my attacks such as punch() which returns the value of 20. Also when I double my attacks with beercanblitz I lose 25 health. I want this attack beercanblitz to only be able to be used once. Any Ideas how I can do this??

    Code:
    class senior:public teacher{
    
     public:
    
      int headbutt();
      int beercanblitz(punch()));
      void setHealth();
      senior();
      ~senior();
    };
    
    senior::senior(){
    
      health = 100;
      name = "SuperSeniors!";
    }
    
    senior::~senior(){
    
    }
    
    int senior::beercanblitz(senior.punch()){
    
    return senior.punch()*2;
    
    }
    Code:
    else if{(enemyAttack == 5){
            f5.beercanblitz(f5.punch());
            //      f5.kick(f5.beercanblitz());
            //      f5.headbutt(f5.beercanblitz())
          }
          cout<<"Super Human Senior USES BEER CAN BLITZ!!"<<endl;
          cout<<"Your Health: ";
          f1.showHealth();
        }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can have a variable that says "usedBeerCanBlitz" and then use that as a flag to indicate if it's been used or not. Or a "beerCanBlitzBoost" which is set to 2 initially, then downrated to 1 when it's been used, and multiply the "punch" by the beerCanBlitzBoost.

    Code:
      int beercanblitz(punch()));
    This won't compile. It should be a punch argument with the correct type (int?) - the call to beercanblitz could use the result of punch() as an input, but it's not declared this way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    so would I just return 2 for beercanblitz?

    Code:
    int senior::beercanblitz(int){
    
    return 2;
    
    }

    How could I then multiply my attacks?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Make it "int punch" and then return "punch * 2" [or perhaps "force" is a better name than "punch"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    hmm..still not sure.. I've attached my source code for this game. I haven't split the code up so its very large. I'm practicing my inheritance from classes that why there are multiple classes instead of mapped. I'm just trying to get beercanblitz to work..and i'll be happy...haha

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int senior::beercanblitz(int force){
    
    return 2 * force;
    
    }
    And I expect the right call would be:
    Code:
    	f1.reducehealth(f5.beercanblitz(f5.punch()));
    or some such...

    Of course, you still have to figure out how to reduce the "beercanblitz" to 1 when it's been used. I'll let you ponder that one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    thanks for the help!!

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    So I could just decrement beercanblitz?

    Code:
     else if(enemyAttack == 5){
          f1.reduceHealth(f5.beercanblitz(f5.punch()));
          f1.reduceHealth(f5.beercanblitz(f5.kick()));
          f1.reduceHealth(f5.beercanblitz(f5.headbutt()));
          beercanblitz()--;
          cout<<"Super Human Senior USES BEER CAN BLITZ!!"<<endl;
          cout<<"Your Health: ";
          f1.showHealth();
          }

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    Ok so basically the beercanbliltz attack is supposed to double all the senior opponents attack power and can only be used once. I also want seniors health to decrement by 25 when he uses it. I changed my code to have a flag to know when it has been used but I don't know how to double the attacks persay..

    Code:
    class senior:public teacher{
      
    private:
      bool blitz;
      
    public:
      
      int headbutt();
      int beercanblitz(int force);
      bool didbeercanblitz();
      void changebeercanblitz(bool);
      void setHealth();
      senior();
      ~senior();
    };
    
    senior::senior(){
      
      health = 100;
      blitz = false;
      
    }
    
    senior::~senior(){
      
    }
    
    bool senior::didbeercanblitz(){
      
      return blitz;
      
    }
    
    void senior::changebeercanblitz(bool state){
      
      blitz = state;
      
    }
    
    int senior::beercanblitz(int force){
      
      return 2 * force;
      
    }
    
    int senior::headbutt(){
      
      return 20;
    }
    
    void senior::setHealth(){
      
      health = 100;
    }
    In Main....

    Code:
    /* Senior's Attack */
        
        cout<<endl;
        
        if(enemyAttack == 1){
          if(attack == 5){
          	f1.reduceHealth(f1.block(flag * f5.punch()));
          }
          else{
    	f1.reduceHealth(f5.punch());    
    	cout<<"Your Health: ";
    	f1.showHealth();
          }
          cout<<"Super Human Senior Punched You!!"<<endl;
          cout<<"Your Health: ";
          f1.showHealth();  
        }
        else if(enemyAttack == 2){
          if(attack == 5){
    	f1.reduceHealth(f1.block(flag * f5.kick()));
          }
          else{
    	f1.reduceHealth(f5.kick());
    	cout<<"Your Health: ";
    	f1.showHealth();
          }
          cout<<"Super Human Senior Kicked You!!"<<endl;  
          cout<<"Your Health: ";
          f1.showHealth();    
        }
        else if (enemyAttack == 3){
          cout<<"Super Human Senior blocked the attack"<<endl;
        } 
        else if(enemyAttack == 4){
          if(attack == 5){
    	f1.reduceHealth(f1.block(f5.headbutt()));
    	cout<<"Your Health: ";
    	f1.showHealth();      
          }    
          else{
    	f1.reduceHealth(f5.headbutt());
          }
          cout<<"Super Human Senior Headbutts You!!"<<endl;
          cout<<"Your Health: ";
          f1.showHealth();
        }      
        
        
        else if(enemyAttack == 5){
          if (!f5.didbeercanblitz()){
    	f5.changebeercanblitz(true);
    	flag = 2;      
          }
          else {
    	flag = 1;
          }
          /* f1.reduceHealth(f5.beercanblitz(f5.kick()));
    	 f1.reduceHealth(f5.beercanblitz(f5.headbutt()));
    	 cout<<"Super Human Senior USES BEER CAN BLITZ!!"<<endl;
    	 cout<<"Your Health: ";
          */      
          f1.showHealth();
        }      
        
        /*Check for Winner*/
        if(f5.getHealth() <= 0 && f1.getHealth() > 0){
          cout<<endl;
          cout<<"You Killed the Super Human Senior!!"<<endl;
          cout<<"Your Health: ";
          f1.showHealth();
          seniorOpponent--;
          if(seniorOpponent > 0){
    	
    	f5.setHealth();
    	
          }      
        }
      }

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would have something like a message system or so:

    Code:
    class FightingUnit
    {
      public:
        FightingUnit( )
        {
          units_list.push_back(this);
        }
    
        template<class T_TYPE>
        struct Message
        {
          FightingUnit *source, *target;
          std::string action_name;
          int target_data, source_data;
    
          enum ActionType
          {
            AT_OFFENSIVE, AT_DEFENSIVE, AT_SPECIAL
          } action_type;
        };
    
        static void ProcessEvents( )
        {
          while(messages_list.size() > 0)
          {
            const Message msg = messages_list.front();
            switch(msg.action_type)
            {
            case AT_OFFENSIVE:
              source->InflictDamages(target, target_data);
              break;
            case AT_DEFENSIVE:
              if(action_name == "block" || action_name == "unblock")
                source->SetBlockStatus(action_name);
              break;
            case AT_SPECIAL:
              source->ProcessSpecialMove(msg);
              break;
            }
          }
        }
      protected:
        static std::vector<FightingUnit*> units_list;
        static std::vector<Message> messages_list;
    
        int health;
        std::string unit_name;
    
      private:
    };
    
    class Teacher : public FightingUnit
    {
      /* ... */
    };
    
    class Senior : public Teacher
    {
      /* ... */
    };
    This should give you a general idea of what I'd do.

    This is a tad sloppy (and untested) and so I think I will maybe try to take an hour or so and write something much more robust.
    Last edited by Desolation; 11-12-2007 at 12:07 AM.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    Wow looks like some awesome coding thanks for the help. The only thing is I haven't made it that far to templates and pointers. So I can't understand the code all that much. I'm still in the learning process...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM