943,965 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2516
  • C++ RSS
Apr 23rd, 2007
0

Rock Paper Scissors fun-ction

Expand Post »
Hello everyone, I am having trouble fine tuning my code. I like to have all the bulky parts of my main in functions, and I can not seem to figure out how to write this code as a function. Anyone tell me where to start?

c++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int player = 0;
  4. int computer = 0;
  5. int tie = 0;
  6. do {
  7. char userChoice;
  8. char compChoice;
  9. compChoice = getCompChoice();
  10. userChoice = getUserChoice();
  11.  
  12. cout << "User picked: " << userChoice << endl;
  13. cout << "Computer picked: " << compChoice << endl;
  14.  
  15.  
  16. if (compChoice == userChoice) {
  17. cout << "It's a tie.\n";
  18. tie += 1;
  19. } else if ((compChoice == 'R') && (userChoice == 'S')) {
  20. cout << "Rock smashes scissors. You Lose.\n";
  21. computer += 1;
  22. } else if ((compChoice == 'P') && (userChoice == 'R')) {
  23. cout << "Paper covors rock. You Lose.\n";
  24. computer += 1;
  25. } else if ((compChoice == 'S') && (userChoice == 'P')) {
  26. cout << "Scissors cut paper. You Lose.\n";
  27. computer += 1;
  28. } else if ((compChoice == 'R') && (userChoice == 'P')) {
  29. cout << "Paper covors rock. You Win.\n";
  30. player += 1;
  31. } else if ((compChoice == 'P') && (userChoice == 'S')) {
  32. cout << "Scissors cut paper. You Win.\n";
  33. player += 1;
  34. } else if ((compChoice == 'S') && (userChoice == 'R')) {
  35. cout << "Rock smashes scissors. You Win.\n";
  36. player += 1;
  37. }
  38. cout << "User: " << player << endl
  39. << "Computer: " << computer << endl
  40. << "Ties: " << tie << endl;
  41. } while (doItAgain());
  42. return (0);
  43. }
Thats my main this is the code I would like to be a function.
c++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3.  
  4. if (compChoice == userChoice) {
  5. cout << "It's a tie.\n";
  6. tie += 1;
  7. } else if ((compChoice == 'R') && (userChoice == 'S')) {
  8. cout << "Rock smashes scissors. You Lose.\n";
  9. computer += 1;
  10. } else if ((compChoice == 'P') && (userChoice == 'R')) {
  11. cout << "Paper covors rock. You Lose.\n";
  12. computer += 1;
  13. } else if ((compChoice == 'S') && (userChoice == 'P')) {
  14. cout << "Scissors cut paper. You Lose.\n";
  15. computer += 1;
  16. } else if ((compChoice == 'R') && (userChoice == 'P')) {
  17. cout << "Paper covors rock. You Win.\n";
  18. player += 1;
  19. } else if ((compChoice == 'P') && (userChoice == 'S')) {
  20. cout << "Scissors cut paper. You Win.\n";
  21. player += 1;
  22. } else if ((compChoice == 'S') && (userChoice == 'R')) {
  23. cout << "Rock smashes scissors. You Win.\n";
  24. player += 1;
  25. }
Last edited by kylcrow; Apr 23rd, 2007 at 10:04 pm.
Similar Threads
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Rock Paper Scissors fun-ction

create the function and pass the three variables (tie, player and computer) by reference to it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Rock Paper Scissors fun-ction

ok that is what I was wondering. But is it pssible to pass chars and int&

c++ Syntax (Toggle Plain Text)
  1. char result(char compChoice, char userChoice, int& player, int& computer, int& tie) {
  2.  
  3. if (compChoice == userChoice) {
  4. tie += 1;
  5. return ("It's a tie.\n")
  6. } else if ((compChoice == 'R') && (userChoice == 'S')) {
  7. computer += 1;
  8. return ("Rock smashes scissors. You Lose.\n")
  9. } else if ((compChoice == 'P') && (userChoice == 'R')) {
  10. computer += 1;
  11. return ("Paper covors rock. You Lose.\n")
  12. } else if ((compChoice == 'S') && (userChoice == 'P')) {
  13. computer += 1;
  14. return ("Scissors cut paper. You Lose.\n")
  15. } else if ((compChoice == 'R') && (userChoice == 'P')) {
  16. player += 1;
  17. return ("Paper covors rock. You Win.\n")
  18. } else if ((compChoice == 'P') && (userChoice == 'S')) {
  19. player += 1;
  20. return ("Scissors cut paper. You Win.\n")
  21. } else if ((compChoice == 'S') && (userChoice == 'R')) {
  22. player += 1;
  23. return ("Rock smashes scissors. You Win.\n")
  24. }
  25. }

I am honestly not trying to get you to write my code, just a lil help where to go from here.
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Rock Paper Scissors fun-ction

looks ok to me except the function should return char* not char. See lines 13 and 14 below.

The rest is simple
  1. int main()
  2. {
  3. int player = 0;
  4. int computer = 0;
  5. int tie = 0;
  6. do {
  7. char userChoice;
  8. char compChoice;
  9. const char* msg;
  10. compChoice = getCompChoice();
  11. userChoice = getUserChoice();
  12.  
  13. msg = result(compChoice, userChoice, player, computer, tie);
  14. cout << msg << "\n";
  15.  
  16. cout << "User picked: " << userChoice << endl;
  17. cout << "Computer picked: " << compChoice << endl;
  18.  
  19.  
  20. cout << "User: " << player << endl
  21. << "Computer: " << computer << endl
  22. << "Ties: " << tie << endl;
  23. } while (doItAgain());
  24. return (0);
  25. }
Last edited by Ancient Dragon; Apr 23rd, 2007 at 10:40 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Rock Paper Scissors fun-ction

What does that mean? return a void not a char, does that mean it should be void result(char, char, int&, int&, int&) ?
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Rock Paper Scissors fun-ction

Click to Expand / Collapse  Quote originally posted by kylcrow ...
What does that mean? return a void not a char, does that mean it should be void result(char, char, int&, int&, int&) ?
Please re-read my post -- I realized void was not right and made appropriate correction. Sorry if I confused you.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Rock Paper Scissors fun-ction

Still got a bunch of errors, but I think I can take it from here. Thanks for all your help
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 25th, 2007
0

Re: Rock Paper Scissors fun-ction

It is much better if you use a switch rather that if statement.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Madzts is offline Offline
7 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Visual C++ Installer problems
Next Thread in C++ Forum Timeline: for-loop help please





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


Follow us on Twitter


© 2011 DaniWeb® LLC