943,558 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1312
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 27th, 2008
0

My simple game: what would i use

Expand Post »
EDIT: I've got a while loop in my code, just now when player 1 goes to have his/her second attack the hp is reset to 99,

c++ Syntax (Toggle Plain Text)
  1. #include <conio.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int p1attack, p2attack, p1hp, p2hp;
  9. char attack;
  10.  
  11. p1hp = 99;
  12. p2hp = 99;
  13.  
  14. system("TITLE Game.");
  15.  
  16. while(p1hp > 0 && p2hp > 0)
  17. {
  18.  
  19. cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
  20. cin >> attack;
  21.  
  22. switch(attack)
  23. {
  24.  
  25. case 'a':
  26. case 'A':
  27. srand ( time(NULL) );
  28. p1attack = rand() % 99 + 1;
  29. cout << "You hit " << p1attack << "!";
  30. break;
  31.  
  32. default:
  33. cout << endl << "INVALID SELECTION!";
  34. break;
  35. }
  36. p2hp -= p1attack;
  37.  
  38. cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
  39. cin >> attack;
  40. switch(attack)
  41. {
  42.  
  43. case 'a':
  44. case 'A':
  45. srand ( time(NULL) );
  46. p2attack = rand() % 99 + 1;
  47. cout << "You hit " << p2attack << "!" << endl;
  48. break;
  49.  
  50. default:
  51. cout << endl << "INVALID SELECTION!";
  52. break;
  53.  
  54. p1hp -= attack;
  55. }
  56.  
  57.  
  58. }
  59. getch();
  60. }
Last edited by Black Magic; Apr 27th, 2008 at 1:28 pm.
Similar Threads
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 27th, 2008
0

Re: My simple game: what would i use

> p1hp -= attack;
This isn't the value you calculated.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 27th, 2008
0

Re: My simple game: what would i use

I've changed it to: p1hp -= p2attack;
But p1hp still stays 99
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 27th, 2008
0

Re: My simple game: what would i use

Hey Black Magic i have solved your problem.

Well just examine That you have typed this code in
C++ Syntax (Toggle Plain Text)
  1. p1hp -= p2attack;

Line 54. Which probarbly doesnt get executed because there are break; statements above it.

So You actually need to paste the code in between Line 46 and line 47 so that the value gets changed.

So here is the total program with the adjustment.

C++ Syntax (Toggle Plain Text)
  1. #include <conio.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int p1attack, p2attack, p1hp, p2hp;
  9. char attack;
  10.  
  11. p1hp = 99;
  12. p2hp = 99;
  13.  
  14. system("TITLE Game.");
  15.  
  16. while(p1hp > 0 && p2hp > 0)
  17. {
  18.  
  19. cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
  20. cin >> attack;
  21.  
  22. switch(attack)
  23. {
  24.  
  25. case 'a':
  26. case 'A':
  27. srand ( time(NULL) );
  28. p1attack = rand() % 99 + 1;
  29. cout << "You hit " << p1attack << "!";
  30. break;
  31.  
  32. default:
  33. cout << endl << "INVALID SELECTION!";
  34. break;
  35. }
  36. p2hp -= p1attack;
  37.  
  38. cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
  39. cin >> attack;
  40. switch(attack)
  41. {
  42.  
  43. case 'a':
  44. case 'A':
  45. srand ( time(NULL) );
  46. p2attack = rand() % 99 + 1;
  47. p1hp -= p2attack;
  48. cout << "You hit " << p2attack << "!" << endl;
  49. break;
  50.  
  51. default:
  52. cout << endl << "INVALID SELECTION!";
  53. break;
  54.  
  55.  
  56. }
  57.  
  58.  
  59. }
  60. getch();
  61. }
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 27th, 2008
0

Re: My simple game: what would i use

Or, if you'd like to keep in consistent with the code for player 1's attack, you could simply put it outside of the switch statement but inside the while loop (like line 57 or 58 in Sky Diploma's program). If you do do that though, remember to take away that same code from line 47.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Apr 27th, 2008
0

Re: My simple game: what would i use

I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right

For example if the user enters something other than "A".
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 27th, 2008
0

Re: My simple game: what would i use

I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right

For example if the user enters something other than "A".
Hmm.... yes you're very correct. In fact, the same thing should be done to the player1 code, because if p2hp -= p1attack; is called and p1attack hasn't been initialized, you'll get not so optimal results.

So either change it, or in the beginning of the loop set p1attack and p2attack to 0.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Apr 27th, 2008
0

Re: My simple game: what would i use

Yes . That is also a good solution Cool Gamer 48

I dint think of that.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 28th, 2008
0

Re: My simple game: what would i use

Thanks guys, now i was wondering now if i should make it so the other player cannot make your hp a negative number, how could this be done? Please reply.

c++ Syntax (Toggle Plain Text)
  1. #include <conio.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int p1attack, p2attack, p1hp, p2hp;
  9. char attack;
  10.  
  11. p1hp = 99;
  12. p2hp = 99;
  13.  
  14. system("TITLE Game.");
  15.  
  16. while(p1hp > 0 && p2hp > 0)
  17. {
  18.  
  19. cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
  20. cin >> attack;
  21.  
  22. switch(attack)
  23. {
  24.  
  25. case 'a':
  26. case 'A':
  27. srand ( time(NULL) );
  28. p1attack = rand() % 50 + 1;
  29. cout << "You hit " << p1attack << "!";
  30. break;
  31.  
  32. default:
  33. cout << endl << "INVALID SELECTION!";
  34. break;
  35. }
  36. p2hp -= p1attack;
  37.  
  38. cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
  39. cin >> attack;
  40. switch(attack)
  41. {
  42.  
  43. case 'a':
  44. case 'A':
  45. srand ( time(NULL) );
  46. p2attack = rand() % 50 + 1;
  47. p1hp -= p2attack;
  48. cout << "You hit " << p2attack << "!" << endl << endl;
  49. break;
  50.  
  51. default:
  52. cout << endl << "INVALID SELECTION!";
  53. break;
  54.  
  55.  
  56. }
  57.  
  58.  
  59. }
  60. getch();
  61. }
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 28th, 2008
0

Re: My simple game: what would i use

Right after p2hp -= p1attack; , add:
C++ Syntax (Toggle Plain Text)
  1. if(p2hp < 0)
  2. p2hp = 0;

You may also try making the health values unsigned, though the above method should work well enough
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 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: game code help
Next Thread in C++ Forum Timeline: Using functions without calling them from any class





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


Follow us on Twitter


© 2011 DaniWeb® LLC