943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1218
  • C++ RSS
Nov 2nd, 2009
0

Problems With Simple C++ Battle System; Need Help

Expand Post »
Okay, what I'm trying to do here is set up a some-what simple battle system for a text based game... I'm having some trouble getting it to work correctly though. I want the Player's attack and the Monster's defense to be random each time the program loops (which is also not working), but it keeps giving the same numbers.

I don't have much experience, so any help you can offer would be appreciated, thanks.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. // Variables
  8. int charLevel, Floor;
  9. int Hp, Atk, Def;
  10. int monLevel, monHp, monAtk, monDef;
  11. string action, charName, monName;
  12. int damageDealt;
  13.  
  14. // Prototypes
  15. void battlePhase();
  16.  
  17. int main()
  18. {
  19. // Defining the character
  20. cout << "What is your name? ";
  21. cin >> charName;
  22. cout << "\nWhat level are you? ";
  23. cin >> charLevel;
  24. cout << "\nWhat floor are you on? ";
  25. cin >> Floor;
  26. cout << "\nWhat monster do you wish to fight? ";
  27. cin >> monName;
  28.  
  29. // Player Stats
  30. // charLevel defined earlier
  31. srand((unsigned)time(0));
  32. Hp = (15 + ( charLevel * 5 ));
  33. Atk = (((rand() % 6) + 0) + (charLevel - 1));
  34. Def = (((rand() % 4) + 0) + (charLevel - 1));
  35.  
  36. // Monster Stats
  37. monLevel = (1/2) * Floor + (charLevel - 2);
  38. if ( monLevel < 0 )
  39. {
  40. monLevel == 1;
  41. }
  42.  
  43. monHp = (10 + (monLevel * 2));
  44. monAtk = (((rand() % 4) + 1) + (monLevel - 1));
  45. monDef = (((rand() % 3) + 1) + (monLevel - 1));
  46.  
  47. while (Hp > 0 || monHp > 0)
  48. {
  49. battlePhase();
  50. }
  51.  
  52. return 0;
  53.  
  54. }
  55.  
  56. void battlePhase()
  57. {
  58. // Actual Battle
  59. cout << endl << "\nWhat do you want to do? ";
  60. cin >> action;
  61.  
  62. if ( action == "Attack" || action == "attack" )
  63. {
  64. cout << endl << charName << " attacked " << monName << " for " << Atk << endl;
  65. cout << monName << " defended " << charName << " for " << monDef << endl;
  66. damageDealt = Atk - monDef;
  67.  
  68. if ( damageDealt < 0 )
  69. {
  70. damageDealt = 0;
  71. }
  72.  
  73. if ( damageDealt == 0 )
  74. {
  75. cout << "\nNo damage!";
  76. }
  77.  
  78. if ( damageDealt > 0 )
  79. {
  80. cout << monName << " took " << damageDealt << " damage!";
  81. monHp = monHp - damageDealt;
  82. }
  83. }
  84. else
  85. {
  86. cout << "\nThat isn't an action! Try typing 'attack'.";
  87. battlePhase();
  88. }
  89.  
  90. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djnstuff is offline Offline
3 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Problems With Simple C++ Battle System; Need Help
If you want the Atk, Def, monAtk, and monDef variables to randomize for each iteration of the loop, you will have to calculate them inside the loop. Currently, you calculate them before the loop starts and you have no way to get back to them to re-calculate them.
Last edited by Fbody; Nov 2nd, 2009 at 5:32 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Nov 2nd, 2009
0
Re: Problems With Simple C++ Battle System; Need Help
Thanks for the feedback... Hm, I tried what you said and moved:

C++ Syntax (Toggle Plain Text)
  1. Atk = (((rand() % 6) + 0) + (charLevel - 1));
  2. Def = (((rand() % 4) + 0) + (charLevel - 1));
  3.  
  4. monAtk = (((rand() % 4) + 1) + (monLevel - 1));
  5. monDef = (((rand() % 3) + 1) + (monLevel - 1));

Into the if statement to make:

C++ Syntax (Toggle Plain Text)
  1. void battlePhase()
  2. {
  3. // Actual Battle
  4. cout << endl << "\nWhat do you want to do? ";
  5. cin >> action;
  6.  
  7. if ( action == "Attack" || action == "attack" )
  8. {
  9. // Randomizing the attack/defend values for player and monster
  10. Atk = (((rand() % 6) + 0) + (charLevel - 1));
  11. Def = (((rand() % 4) + 0) + (charLevel - 1));
  12.  
  13. monAtk = (((rand() % 4) + 1) + (monLevel - 1));
  14. monDef = (((rand() % 3) + 1) + (monLevel - 1));
  15.  
  16. cout << endl << charName << " attacked " << monName << " for " << Atk << endl;
  17. cout << monName << " defended " << charName << " for " << monDef << endl;
  18. damageDealt = Atk - monDef;
  19.  
  20. if ( damageDealt < 0 )
  21. {
  22. damageDealt = 0;
  23. }
  24.  
  25. if ( damageDealt == 0 )
  26. {
  27. cout << "\nNo damage!";
  28. }
  29.  
  30. if ( damageDealt > 0 )
  31. {
  32. cout << monName << " took " << damageDealt << " damage!";
  33. monHp = monHp - damageDealt;
  34. }
  35. }
  36. else
  37. {
  38. cout << "\nThat isn't an action! Try typing 'attack'.";
  39. battlePhase();
  40. }
  41.  
  42. }

It still doesn't seem to work however, and when I tried this the first result for the monDef was -1... Which shouldn't be able to happen.

I'm rather stumped.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djnstuff is offline Offline
3 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Problems With Simple C++ Battle System; Need Help
You need to use srand, with integrated time, so the random integer will change(seed).

Example:
C++ Syntax (Toggle Plain Text)
  1. srand ((unsigned)time(0));
  2. random3 = (rand() % 4) + 1;

Edit: Also include the header: #include<time.h> if you do not already have it.

Also, if you don't mind, please send me the source code once you are done so I can check it out. I love checking out others' RPGs that they make.
Last edited by restrictment; Nov 2nd, 2009 at 6:24 pm.
Reputation Points: 102
Solved Threads: 17
Posting Whiz in Training
restrictment is offline Offline
228 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Problems With Simple C++ Battle System; Need Help
Thanks, but it still doesn't work correctly... Hmm...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djnstuff is offline Offline
3 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Problems With Simple C++ Battle System; Need Help
I didn't see it before...

In your original post, Line 40, change that line (whatever line number it is now) from the equality comparison operator to the assignment operator.

monLevel == 1;
needs to be:
monLevel = 1;
Last edited by Fbody; Nov 3rd, 2009 at 4:16 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Problems With Simple C++ Battle System; Need Help
Apparently using "goto x;" is some kind of ungodly error that i seem to have picked up. But I have been able to create some really successful programs with it.

You can create different type of modules within your program using goto.

For instance:
C++ Syntax (Toggle Plain Text)
  1. {
  2. HPSTASIS:
  3. hptot = Health - EnDamage;
  4. Health = hptot;
  5. if (Health <= 0) {cout<<"You have died.";system("PAUSE");goto endgame;}
  6. else if (Health > 0){
  7. goto CHECKPOINT;
  8. }
  9. }
In which after each attack against you, it makes sure your health is correct and checks to see if you are dead. In which the game would end, otherwise if you are not dead, it goes to the checkpoint module that continues the battle.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Function trouble (beginner)
Next Thread in C++ Forum Timeline: Beginner C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC