My fighting Game

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

Join Date: Apr 2007
Posts: 8
Reputation: cjwenigma is an unknown quantity at this point 
Solved Threads: 0
cjwenigma cjwenigma is offline Offline
Newbie Poster

My fighting Game

 
0
  #1
Sep 19th, 2007
OK so here is a fighter game I made..except my fighters power won't decrease and I can't get my fighters names to show up....*sigh*... any idea.. My functions could be wrong.. but I always thought it went above int main()..

  1. <span class="ad_notxt"><code class="inlinecode">
  2. #include<iostream>
  3. using namespace std;
  4. int fighterOne (char fighter1Attack){
  5. string fighter1Name;
  6. // char fighter1Attack;
  7. int fighter2Power=1000;
  8. string fighter2Name;
  9. cout << fighter1Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  10. cin >> fighter1Attack;
  11. if (fighter1Attack == 'P' || fighter1Attack == 'p') {
  12. fighter2Power -= 50;
  13. cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
  14. }
  15. else if (fighter1Attack == 'K' || fighter1Attack == 'k') {
  16. fighter2Power -= 100;
  17. cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
  18. }
  19. else {
  20. cout << "Invalid attack, you lose a turn!" << endl;
  21. }
  22. }
  23. int fighterTwo (char fighter2Attack) {
  24. string fighter2Name;
  25. //char fighter2Attack;
  26. int fighter1Power=1000;
  27. string fighter1Name;
  28. cout << fighter2Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  29. cin >> fighter2Attack;
  30. if (fighter2Attack == 'P' || fighter2Attack == 'p') {
  31. fighter1Power -= 50;
  32. }
  33. else if (fighter2Attack == 'K' || fighter2Attack == 'k') {
  34. fighter1Power -= 100;
  35. cout << fighter1Name << "'s health reduced to " << fighter1Power << endl;
  36. }
  37. else {
  38. cout << "Invalid attack, you lose a turn!" << endl;
  39. }
  40. }
  41. int main() {
  42. string fighter1Name = "Greg";
  43. string fighter2Name = "Casey";
  44. int fighter1Power = 1000;
  45. int fighter2Power = 1000;
  46. char fighter1Attack;
  47. char fighter2Attack;
  48.  
  49. do {
  50. fighterOne (fighter1Attack);
  51. fighterTwo (fighter2Attack);
  52. }while((fighter1Power > 0) && (fighter2Power > 0));
  53. if (fighter1Power > fighter2Power) {
  54. cout << "Fighter 1 is the winner!" << endl;
  55. }
  56. else if (fighter2Power > fighter1Power) {
  57. cout << "Fighter 2 is the winner!" << endl;
  58. }
  59. else {
  60. cout << "The game ended in a draw!" << endl;
  61. }
  62. return 0;
  63. }
  64.  
  65. </code></span>
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: My fighting Game

 
0
  #2
Sep 20th, 2007
This information will help us help you if you follow the suggestions therein.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: My fighting Game

 
0
  #3
Sep 20th, 2007
It looks as badly indented here as it does on other forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1
Reputation: kim165 is an unknown quantity at this point 
Solved Threads: 0
kim165 kim165 is offline Offline
Newbie Poster

Re: My fighting Game

 
0
  #4
Sep 20th, 2007
1. when using string you also need
  1. #include <string>
2. for char values try char name = ' ';
  1. example char fighter1Attack = ' ';
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: My fighting Game

 
0
  #5
Sep 20th, 2007
>cin >> fighter2Attack;
if (fighter2Attack == 'P' || fighter2Attack == 'p')

Seeing as fighter2attack is a string that won't work.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: My fighting Game

 
0
  #6
Sep 22nd, 2007
Here is a better indented code {i used astyle to do this...}

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int fighterOne (char fighter1Attack)
  6. {
  7.  
  8. string fighter1Name;
  9. // char fighter1Attack;
  10. int fighter2Power=1000;
  11. string fighter2Name;
  12.  
  13. cout << fighter1Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  14. cin >> fighter1Attack;
  15.  
  16. if (fighter1Attack == 'P' || fighter1Attack == 'p')
  17. {
  18. fighter2Power -= 50;
  19. cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
  20. }
  21. else if (fighter1Attack == 'K' || fighter1Attack == 'k')
  22. {
  23. fighter2Power -= 100;
  24. cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
  25. }
  26. else
  27. {
  28. cout << "Invalid attack, you lose a turn!" << endl;
  29. }
  30.  
  31.  
  32. }
  33.  
  34.  
  35. int fighterTwo (char fighter2Attack)
  36. {
  37.  
  38. string fighter2Name;
  39. //char fighter2Attack;
  40. int fighter1Power=1000;
  41. string fighter1Name;
  42.  
  43. cout << fighter2Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  44. cin >> fighter2Attack;
  45.  
  46. if (fighter2Attack == 'P' || fighter2Attack == 'p')
  47. {
  48. fighter1Power -= 50;
  49. }
  50. else if (fighter2Attack == 'K' || fighter2Attack == 'k')
  51. {
  52. fighter1Power -= 100;
  53. cout << fighter1Name << "'s health reduced to " << fighter1Power << endl;
  54. }
  55. else
  56. {
  57. cout << "Invalid attack, you lose a turn!" << endl;
  58. }
  59.  
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65.  
  66.  
  67. string fighter1Name = "Greg";
  68. string fighter2Name = "Casey";
  69. int fighter1Power = 1000;
  70. int fighter2Power = 1000;
  71. char fighter1Attack;
  72. char fighter2Attack;
  73.  
  74. do
  75. {
  76. fighterOne (fighter1Attack);
  77. fighterTwo (fighter2Attack);
  78. }
  79. while ((fighter1Power > 0) && (fighter2Power > 0));
  80. if (fighter1Power > fighter2Power)
  81. {
  82. cout << "Fighter 1 is the winner!" << endl;
  83. }
  84. else if (fighter2Power > fighter1Power)
  85. {
  86. cout << "Fighter 2 is the winner!" << endl;
  87. }
  88. else
  89. {
  90. cout << "The game ended in a draw!" << endl;
  91. }
  92.  
  93.  
  94. return 0;
  95. }
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: My fighting Game

 
0
  #7
Sep 22nd, 2007
here is something that works:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. /*although i didn't understand why you wanted to pass string fighter1Attack as an argument, i passed it as a reference*/
  7. int fighterOne (string& fighter1Attack)
  8. {
  9.  
  10. string fighter1Name = "Subzero"; /*i added the names*/
  11. // char fighter1Attack;
  12. int fighter2Power=1000;
  13. string fighter2Name;
  14.  
  15. cout << fighter1Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  16. cin >> fighter1Attack;
  17.  
  18. /*here i made the comparisons from a_string=='char' to a_string=="another_string"*/
  19. if (fighter1Attack == "P" || fighter1Attack == "p")
  20. {
  21. fighter2Power -= 50;
  22. cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
  23. }
  24. else if (fighter1Attack == "K" || fighter1Attack == "k")
  25. {
  26. fighter2Power -= 100;
  27. cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
  28. }
  29. else
  30. {
  31. cout << "Invalid attack, you lose a turn!" << endl;
  32. }
  33.  
  34.  
  35. }
  36.  
  37.  
  38. int fighterTwo (string& fighter2Attack)
  39. {
  40.  
  41. string fighter2Name = "Scorpio";
  42. //char fighter2Attack;
  43. int fighter1Power=1000;
  44. string fighter1Name;
  45.  
  46. cout << fighter2Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  47. cin >> fighter2Attack;
  48.  
  49. if (fighter2Attack == "P" || fighter2Attack == "p")
  50. {
  51. fighter1Power -= 50;
  52. }
  53. else if (fighter2Attack == "K" || fighter2Attack == "k")
  54. {
  55. fighter1Power -= 100;
  56. cout << fighter1Name << "'s health reduced to " << fighter1Power << endl;
  57. }
  58. else
  59. {
  60. cout << "Invalid attack, you lose a turn!" << endl;
  61. }
  62.  
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68.  
  69.  
  70. string fighter1Name = "Greg"; /*you don't pass the names to the function, so this is why you see subzero and scorpion when the program is running! Hint:: try to this like i did the string fighterXattack, if you can't i 'll help you.*/
  71. string fighter2Name = "Casey";
  72. int fighter1Power = 1000;
  73. int fighter2Power = 1000;
  74. string fighter1Attack; /*i changed this part*/
  75. string fighter2Attack; /*i changed this part */
  76.  
  77. do
  78. {
  79. fighterOne (fighter1Attack);
  80. fighterTwo (fighter2Attack);
  81. }
  82. while ((fighter1Power > 0) && (fighter2Power > 0));
  83. if (fighter1Power > fighter2Power)
  84. {
  85. cout << "Fighter 1 is the winner!" << endl;
  86. }
  87. else if (fighter2Power > fighter1Power)
  88. {
  89. cout << "Fighter 2 is the winner!" << endl;
  90. }
  91. else
  92. {
  93. cout << "The game ended in a draw!" << endl;
  94. }
  95.  
  96.  
  97. return 0;
  98. }

Basically the error that i foud was that you were using c++ string facilities with chars...this is why nothing worked....

Check the comments i made on your code.

Also you are starting object oriented programming maybe you should check about encapsulation
1 and 2

Another thing is we don't you implement the fighters as one class,and just instantiate 2 objects fighter1 and fighter2

Last but not least maybe you should check for a tutorial on strings:: http://www.cppreference.com/cppstring/index.html and http://www.cprogramming.com/tutorial/string.html

PS::it is the first time i help someone else, in this fourm, and i am relative noob so i don't promise that this the best thing you could do!
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC