Rock Paper Scissors fun-ction

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 114
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Rock Paper Scissors fun-ction

 
0
  #1
Apr 23rd, 2007
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?

  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,570
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Rock Paper Scissors fun-ction

 
0
  #2
Apr 23rd, 2007
create the function and pass the three variables (tie, player and computer) by reference to it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 114
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: Rock Paper Scissors fun-ction

 
0
  #3
Apr 23rd, 2007
ok that is what I was wondering. But is it pssible to pass chars and int&

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,570
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Rock Paper Scissors fun-ction

 
0
  #4
Apr 23rd, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 114
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: Rock Paper Scissors fun-ction

 
0
  #5
Apr 23rd, 2007
What does that mean? return a void not a char, does that mean it should be void result(char, char, int&, int&, int&) ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,570
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Rock Paper Scissors fun-ction

 
0
  #6
Apr 23rd, 2007
Originally Posted by kylcrow View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 114
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: Rock Paper Scissors fun-ction

 
0
  #7
Apr 23rd, 2007
Still got a bunch of errors, but I think I can take it from here. Thanks for all your help
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 7
Reputation: Madzts is an unknown quantity at this point 
Solved Threads: 1
Madzts Madzts is offline Offline
Newbie Poster

Re: Rock Paper Scissors fun-ction

 
0
  #8
Apr 25th, 2007
It is much better if you use a switch rather that if statement.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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