My simple game: what would i use

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

My simple game: what would i use

 
0
  #1
Apr 27th, 2008
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,

  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.
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: My simple game: what would i use

 
0
  #2
Apr 27th, 2008
> p1hp -= attack;
This isn't the value you calculated.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: My simple game: what would i use

 
0
  #3
Apr 27th, 2008
I've changed it to: p1hp -= p2attack;
But p1hp still stays 99
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: My simple game: what would i use

 
0
  #4
Apr 27th, 2008
Hey Black Magic i have solved your problem.

Well just examine That you have typed this code in
  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.

  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. }
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: My simple game: what would i use

 
0
  #5
Apr 27th, 2008
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: My simple game: what would i use

 
0
  #6
Apr 27th, 2008
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".
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: My simple game: what would i use

 
0
  #7
Apr 27th, 2008
Originally Posted by Sky Diploma View Post
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: My simple game: what would i use

 
0
  #8
Apr 27th, 2008
Yes . That is also a good solution Cool Gamer 48

I dint think of that.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: My simple game: what would i use

 
0
  #9
Apr 28th, 2008
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.

  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. }
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: My simple game: what would i use

 
0
  #10
Apr 28th, 2008
Right after p2hp -= p1attack; , add:
  1. if(p2hp < 0)
  2. p2hp = 0;

You may also try making the health values unsigned, though the above method should work well enough
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC