Thread: Battle sequence easter egg not working!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    Battle sequence easter egg not working!

    I'm woking on tehbatlle sequnce for a game and an easter egg isn't working.
    Here's the code for one of the weapons.
    Code:
    int unarmed()
    {
        while (hp>0 && opphp>0)
        {
              cout<<"HP:"<<hp<<"\nOpponent's HP:"<<opphp<<"\n";
              cout<<"Weapon: Unarmed\n1. Karate Chop\n2. Punch\n3. Kick\n";
              unarmed:
              cin>>attack;
              switch (attack)
              {
                     case 1:
                          dmg=1;
                          break;
                     case 2:
                          dmg=(rand()%2)+1;
                          break;
                     case 3:
                          dmg=(rand()%3)+1;
                          break;
                     case 7:
                          dmg=opphp;
                          hit=(rand()%100);
                     default:
                             goto unarmed;
                             break;
              }
              if (attack != 7) hit=(rand()%2);
              if (hit==1)
              {
                         opphp=opphp-dmg;
                         cout<<"Opponent's HP-"<<dmg<<"!\n";
              }
              else
              {
                  cout<<"Miss!\n";
              }
              if (opphp<=0) break;
              oppdmg=(rand()%oppmaxhit)+1;
              opphit=(rand()%5);
              if (opphit==1)
              {
                            hp=hp-oppdmg;
                            cout<<"HP-"<<oppdmg<<"!\n";
              }
              else
              {
                  cout<<"Opponent missed!\n";
              }
        }
        if (opphp<=0) cout<<"Opponent's HP is 0! You Win!\n";
        if (hp<=0) cout<<"Your HP is 0... You lose...\n";
    }
    Please help, I don't know what's wrong. I think it's in the switch statement.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Oops, I'm stupid. I figured out what went wrong. I forgot the break statement at the easter egg case statment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Just so you know for next time...

    Place yourself in our position (i.e. not knowing of your project) and read this:

    "Please help, I don't know what's wrong. I think it's in the switch statement."

    Now tell us if you have any idea of what could be wrong with the program. Errors ? If yes, compiler errors or linker errors ? No errors ? Then what happens ? And so on.. We can't figure that out.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Consider calling srand() to seed the random number generator, unless you want the game to be exactly the same every time you play it. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    I fixed it, as I said in my previous post, but the case 7: statement was falling through to the default: statement. I call srand() in my main() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. Every other scanf not working
    By Rob20050 in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 12:48 AM
  4. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM