Programming a fighting game?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 5
Reputation: gamerprog is an unknown quantity at this point 
Solved Threads: 0
gamerprog gamerprog is offline Offline
Newbie Poster

Programming a fighting game?

 
0
  #1
Dec 25th, 2008
I'm programming this RPG, and I can't get past the first battle. I can't figure out how to get the computer to break the loop when then enemy's health (enhp) is less than 1. I tried using an if statement, but then it didn't loop at all. Could you guys help me out?
  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int main (void)
  6. {
  7. int num, random_integer, hp= 100, enhp= 50;
  8. while (hp >=1 or enhp >=1)
  9. {
  10. {srand((unsigned)time(0));
  11. for(int index=0; index<20; index++)
  12. {random_integer = (rand()%10)+1;
  13. hp = hp-random_integer;
  14. if (hp>=1)
  15. {cout<<"The enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health.";
  16. cout<<"\n1)Attack!\n 2)I've had enough- run away!";
  17. cin>>num;}
  18. if(num ==1)
  19. {srand((unsigned)time(0));
  20. for(int index=0; index<20; index++)
  21. {random_integer = (rand()%10)+1;}
  22. enhp = enhp-random_integer;
  23. cout<<"You have done "<<random_integer<<" damage." ;}
  24. else
  25. cout<<"You have fled";
  26. }
  27. system("PAUSE");
  28. return 0;
  29. }
  30. }
  31. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Programming a fighting game?

 
0
  #2
Dec 25th, 2008
  1. //...
  2. if (enhp < 1)
  3. break;
  4.  
  5. enhp = enhp-random_integer;
  6. //...
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 640
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Programming a fighting game?

 
0
  #3
Dec 25th, 2008
Did you mean to loop until both were dead, or until one was dead?

  1. while (hp >=1 or enhp >=1)

Reads (in english) "While the player health is at least one or the enemy health is at least one, keep fighting"

I think you wanted to stop if either one died:

  1. while (hp >=1 and enhp >=1)

Reads (in english) "While the player health is at least one AND the enemy health is at least one, keep fighting"

So this loop will stop when either one is dead.

(But the break would work too.)
Last edited by Murtan; Dec 25th, 2008 at 7:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: gamerprog is an unknown quantity at this point 
Solved Threads: 0
gamerprog gamerprog is offline Offline
Newbie Poster

Re: Programming a fighting game?

 
0
  #4
Dec 29th, 2008
thanks, your posts have been very helpful. Unfortunately, I've noticed another problem. My else that lets the player exit the battle only works in the initialization. Do you know why that might be the case?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 640
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Programming a fighting game?

 
0
  #5
Dec 29th, 2008
Comments on the original code (that's all I have to look at):

You're really only supposed to call srand() once per run you don't have to call it everytime you want a random number.

In the original code, the 'I want to quit' option prints "You have fled" but it is still inside the for loop so it keeps fighting.

The outer while loop that we spent the time talking about is actually never tested. When the for loop terminates, the main() returns before the while gets a chance to be tested.

If your code has changed significantly, please re-post it.

Please use language specific code tags:
[code=c++]
//your code here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: gamerprog is an unknown quantity at this point 
Solved Threads: 0
gamerprog gamerprog is offline Offline
Newbie Poster

Re: Programming a fighting game?

 
0
  #6
Dec 29th, 2008
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main (void)
  5. {
  6. int num, random_integer, hp= 100, enhp= 50;
  7. while (hp >=1 and enhp >=1)
  8. {srand((unsigned)time(0));
  9. for(int index=5; index<10; index++)
  10. {random_integer = (rand()%10)+1;
  11. hp = hp-random_integer;
  12. if (hp>=1) //remember, multiline if statements require brackets to function
  13. {cout<<"\nThe enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health.";
  14. cout<<"\n1)Attack!\n 2)I've had enough- run away!";
  15. cin>>num;}
  16. else
  17. {cout<<"You have died, better luck next time,";
  18. system("Pause");
  19. return 0;}
  20. {if(num ==1)
  21. {srand((unsigned)time(0));
  22. for(int index=10; index<20; index++)
  23. {random_integer = (rand()%10)+1;}
  24. enhp = enhp-random_integer;
  25. {if(enhp< 1) //Victory condition. Breaks loop after enemy death
  26. {cout<<"The masked man falls to the ground, defeated.";
  27. break;}}
  28. cout<<"You have done "<<random_integer<<" damage." ;}
  29. else
  30. {cout<<"You have fled"; break;}}}}}}
  31. cout<<"Terrified, you run through the streets. Where is you father? If anybody'll know what to do, it'll be him.";
  32. system("PAUSE");
  33. return 0;
  34. }
This code won't work. All I did was copy and paste it from the larger file, and it said that it expected a declaration before the curly brace on line 30.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Programming a fighting game?

 
0
  #7
Dec 29th, 2008
You're still calling srand() more than once.

Also try indenting your code and you might see your problem! Or at least stand a chance, thats too painful to try and read tbh.

Chris
Last edited by Freaky_Chris; Dec 29th, 2008 at 1:14 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 640
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Programming a fighting game?

 
0
  #8
Dec 29th, 2008
Ok...I will do this once, feel free to re-format afterwards, but here is your code. I put each brace '{' or '}' on its own line and then begged my editor to try to make it look better.

(I made no other changes, even though it needed them.)

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main (void)
  5. {
  6. int num, random_integer, hp= 100, enhp= 50;
  7. while (hp >=1 and enhp >=1)
  8. {
  9. srand((unsigned)time(0));
  10. for(int index=5; index<10; index++)
  11. {
  12. random_integer = (rand()%10)+1;
  13. hp = hp-random_integer;
  14. if (hp>=1) //remember, multiline if statements require brackets to function
  15. {
  16. cout<<"\nThe enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health.";
  17. cout<<"\n1)Attack!\n 2)I've had enough- run away!";
  18. cin>>num;
  19. }
  20. else
  21. {
  22. cout<<"You have died, better luck next time,";
  23. system("Pause");
  24. return 0;
  25. }
  26. {
  27. if(num ==1)
  28. {
  29. srand((unsigned)time(0));
  30. for(int index=10; index<20; index++)
  31. {
  32. random_integer = (rand()%10)+1;
  33. }
  34. enhp = enhp-random_integer;
  35. {
  36. if(enhp< 1) //Victory condition. Breaks loop after enemy death
  37. {
  38. cout<<"The masked man falls to the ground, defeated.";
  39. break;
  40. }
  41. }
  42. cout<<"You have done "<<random_integer<<" damage." ;
  43. }
  44. else
  45. {
  46. cout<<"You have fled"; break;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. cout<<"Terrified, you run through the streets. Where is you father? If anybody'll know what to do, it'll be him.";
  54. system("PAUSE");
  55. return 0;
  56. }

As noted by the previous poster, you're still calling srand() from multiple locations and from within a loop.

I would put one call to srand() between line 6 and line 7 and then get rid of the rest.

Line 7 should read while (hp >=1 && enhp >=1)
You don't need the for loop that starts on line 10 above, the while loop on line 7 will keep you looping until someone is dead.

If you get rid of the srand() on line 29, you can replace the for loop on lines 30-33 with the one call on line 32.

You appear to have too many closing braces '}'

Try that and see what you have.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: gamerprog is an unknown quantity at this point 
Solved Threads: 0
gamerprog gamerprog is offline Offline
Newbie Poster

Re: Programming a fighting game?

 
0
  #9
Dec 30th, 2008
I have made the suggested changes in code, and now the only error that shows up is "expected primary expression before else on the final else." Additionally I apologize for my cluttered code, I sometimes forget that I'm the only one on earth who likes reading it.
  1. {if(num ==1)
  2.  
  3. {random_integer = (rand()%10)+1;}
  4. enhp = enhp-random_integer;
  5. {
  6. if (enhp < 1) //Victory condition. Breaks loop after enemy death
  7. {
  8. cout<<"The masked man falls to the ground, defeated.";
  9. break;
  10. }
  11. else
  12. cout<<"You have done "<<random_integer<<" damage." ;
  13. }
  14. else
  15. {
  16. cout<<"You have fled";
  17. break;
  18. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Programming a fighting game?

 
0
  #10
Dec 30th, 2008
Your last else has no if to go with it.....

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1117 | Replies: 16
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC