| | |
Programming a fighting game?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 5
Reputation:
Solved Threads: 0
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?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main (void) { int num, random_integer, hp= 100, enhp= 50; while (hp >=1 or enhp >=1) { {srand((unsigned)time(0)); for(int index=0; index<20; index++) {random_integer = (rand()%10)+1; hp = hp-random_integer; if (hp>=1) {cout<<"The enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health."; cout<<"\n1)Attack!\n 2)I've had enough- run away!"; cin>>num;} if(num ==1) {srand((unsigned)time(0)); for(int index=0; index<20; index++) {random_integer = (rand()%10)+1;} enhp = enhp-random_integer; cout<<"You have done "<<random_integer<<" damage." ;} else cout<<"You have fled"; } system("PAUSE"); return 0; } } }
•
•
Join Date: May 2008
Posts: 640
Reputation:
Solved Threads: 104
Did you mean to loop until both were dead, or until one was dead?
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:
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.)
c++ Syntax (Toggle Plain Text)
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:
c++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: May 2008
Posts: 640
Reputation:
Solved Threads: 104
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]
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]
•
•
Join Date: Dec 2008
Posts: 5
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main (void) { int num, random_integer, hp= 100, enhp= 50; while (hp >=1 and enhp >=1) {srand((unsigned)time(0)); for(int index=5; index<10; index++) {random_integer = (rand()%10)+1; hp = hp-random_integer; if (hp>=1) //remember, multiline if statements require brackets to function {cout<<"\nThe enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health."; cout<<"\n1)Attack!\n 2)I've had enough- run away!"; cin>>num;} else {cout<<"You have died, better luck next time,"; system("Pause"); return 0;} {if(num ==1) {srand((unsigned)time(0)); for(int index=10; index<20; index++) {random_integer = (rand()%10)+1;} enhp = enhp-random_integer; {if(enhp< 1) //Victory condition. Breaks loop after enemy death {cout<<"The masked man falls to the ground, defeated."; break;}} cout<<"You have done "<<random_integer<<" damage." ;} else {cout<<"You have fled"; break;}}}}}} cout<<"Terrified, you run through the streets. Where is you father? If anybody'll know what to do, it'll be him."; system("PAUSE"); return 0; }
•
•
Join Date: May 2008
Posts: 640
Reputation:
Solved Threads: 104
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.)
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
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.
(I made no other changes, even though it needed them.)
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main (void) { int num, random_integer, hp= 100, enhp= 50; while (hp >=1 and enhp >=1) { srand((unsigned)time(0)); for(int index=5; index<10; index++) { random_integer = (rand()%10)+1; hp = hp-random_integer; if (hp>=1) //remember, multiline if statements require brackets to function { cout<<"\nThe enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health."; cout<<"\n1)Attack!\n 2)I've had enough- run away!"; cin>>num; } else { cout<<"You have died, better luck next time,"; system("Pause"); return 0; } { if(num ==1) { srand((unsigned)time(0)); for(int index=10; index<20; index++) { random_integer = (rand()%10)+1; } enhp = enhp-random_integer; { if(enhp< 1) //Victory condition. Breaks loop after enemy death { cout<<"The masked man falls to the ground, defeated."; break; } } cout<<"You have done "<<random_integer<<" damage." ; } else { cout<<"You have fled"; break; } } } } } } cout<<"Terrified, you run through the streets. Where is you father? If anybody'll know what to do, it'll be him."; system("PAUSE"); return 0; }
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.
•
•
Join Date: Dec 2008
Posts: 5
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
{if(num ==1) {random_integer = (rand()%10)+1;} enhp = enhp-random_integer; { if (enhp < 1) //Victory condition. Breaks loop after enemy death { cout<<"The masked man falls to the ground, defeated."; break; } else cout<<"You have done "<<random_integer<<" damage." ; } else { cout<<"You have fled"; break; }
![]() |
Similar Threads
- Tool Optimization (Game Development)
- My fighting Game (C++)
- pleas help me to make low level game (C)
- text adventure programming help (Python)
- Text Adventure -- Classes (Python)
- Half Life 2 SDK; Coding help wanted (C++)
- Tell us about yourself! (Community Introductions)
- rpg phping, fighting sparring please look (PHP)
Other Threads in the C++ Forum
- Previous Thread: For statement in flow chart
- Next Thread: Creating Headers and utilizing #define
Views: 1117 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





