Rock paper scissors using functions

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

Join Date: Nov 2008
Posts: 3
Reputation: evilsithgirl is an unknown quantity at this point 
Solved Threads: 0
evilsithgirl's Avatar
evilsithgirl evilsithgirl is offline Offline
Newbie Poster

Rock paper scissors using functions

 
0
  #1
Nov 10th, 2008
hello everybody. I am trying to write a game of rock paper scissors where the user plays against the computer. The computer picks a random number and the user makes his/her own selection. then the selections are compaired and the winner is selected.

I need to write the following in functions:

get user choice
get computer choice

i was able to get the winner in a function already.

the program works as it stands now but i have no idea how to put the user and computer choice in a function. here is my code

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void winner(int selection, int compSelection);
  7.  
  8. int main()
  9. {
  10. srand((unsigned)time(0));
  11.  
  12. int compSel = (rand()%2)+1;
  13. int userSel;
  14.  
  15. cout << "Welcome to ~ Rock Paper Scissors ~!! I assume you know how to play,";
  16. cout << " so lets begin. You are playing against the computer. Type 0 for";
  17. cout << " rock, 1 for paper, and 2 for scissors\n";
  18. cin >> userSel;
  19.  
  20. cout << "Computer Selects " << compSel << endl;
  21.  
  22. winner(userSel, compSel);
  23.  
  24. system("pause");
  25. return 0;
  26. }
  27.  
  28. */
  29. // ************************************************************************
  30. // Winner
  31. //
  32. // task: Selects the winner
  33. // data in: user choice and computer choice
  34. // data out: Winner
  35. //
  36. // ***********************************************************************
  37.  
  38.  
  39. void winner (int selection, int compSelection)
  40.  
  41. {
  42.  
  43. if (selection == 0)
  44. {
  45. if (compSelection == 0)
  46. cout << "It's a tie!\n\n\n\n";
  47. else if (compSelection == 1)
  48. cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
  49. else if (compSelection == 2)
  50. cout << "Rock beats scissors! You win!\n\n\n\n";
  51. }
  52.  
  53. else if (selection == 1)
  54. {
  55. if (compSelection == 0)
  56. cout << "It's a tie!\n\n\n\n";
  57. else if (compSelection == 1)
  58. cout << "Paper beats rock! You win!\n\n\n\n";
  59. else if (compSelection == 2)
  60. cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
  61. }
  62. else if (selection == 2)
  63. {
  64. if (compSelection == 0)
  65. cout << "It's a tie!\n\n\n\n";
  66. else if (compSelection == 1)
  67. cout << "Scissors beat paper! You win!\n\n\n\n";
  68. else if (compSelection == 2)
  69. cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
  70. }
  71. }

any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Rock paper scissors using functions

 
0
  #2
Nov 10th, 2008
What is the problem?
Write a function!
int userselection()
{
cout...
cin ...
return an int
}
Do the same with the computerselection...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC