Problems With Simple C++ Battle System; Need Help

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

Join Date: Nov 2009
Posts: 3
Reputation: djnstuff is an unknown quantity at this point 
Solved Threads: 0
djnstuff djnstuff is offline Offline
Newbie Poster

Problems With Simple C++ Battle System; Need Help

 
0
  #1
Nov 2nd, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 54
Reputation: Fbody is an unknown quantity at this point 
Solved Threads: 6
Fbody Fbody is offline Offline
Junior Poster in Training
 
0
  #2
Nov 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: djnstuff is an unknown quantity at this point 
Solved Threads: 0
djnstuff djnstuff is offline Offline
Newbie Poster
 
0
  #3
Nov 2nd, 2009
Thanks for the feedback... Hm, I tried what you said and moved:

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 140
Reputation: restrictment is on a distinguished road 
Solved Threads: 10
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #4
Nov 2nd, 2009
You need to use srand, with integrated time, so the random integer will change(seed).

Example:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: djnstuff is an unknown quantity at this point 
Solved Threads: 0
djnstuff djnstuff is offline Offline
Newbie Poster
 
0
  #5
Nov 3rd, 2009
Thanks, but it still doesn't work correctly... Hmm...
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 54
Reputation: Fbody is an unknown quantity at this point 
Solved Threads: 6
Fbody Fbody is offline Offline
Junior Poster in Training
 
0
  #6
Nov 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Shinedevil is an unknown quantity at this point 
Solved Threads: 0
Shinedevil Shinedevil is offline Offline
Light Poster
 
0
  #7
Nov 3rd, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Reply

Tags
c++, system

Message:




Views: 528 | Replies: 6
Thread Tools Search this Thread



Tag cloud for c++, system
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC