Guest User

CGBS

a guest
Jan 16th, 2010
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. //Simple rock, paper scissors game
  8.  
  9.  
  10. //Introduction to the program
  11. void introduction()
  12. {
  13.     cout << "****Rock, Paper, Scissors****" << endl << endl;
  14. }
  15.  
  16. //Determine user's choice (rock, paper, or scissors)
  17. int userChoice()
  18. {
  19.     int choice;
  20.     string input = "";
  21.  
  22.     //ensures that response is an integer (1, 2, or 3)
  23.     while (true)
  24.     {
  25.         cout << "(1) Rock" << endl << "(2) Paper" << endl << "(3) Scissors" << endl << endl << "Choose 1, 2, or 3: ";
  26.         getline(cin, input);
  27.         stringstream myStream(input);
  28.         if ((myStream >> choice) && (choice >0) && (choice <4))
  29.             break;
  30.         cout << endl << "Invalid choice. Please try again." << endl << endl;
  31.     }
  32.  
  33.     return choice;
  34. }
  35.  
  36. //Determine computer's 'choice'
  37. int cpuChoice()
  38. {
  39.     //Assigns randomChoice a random number (1, 2, or 3)
  40.     int randomChoice = (rand() % (3 - 0 + 1)) + 0;
  41.     return randomChoice;
  42. }
  43.  
  44. //Determine who wins
  45. char winner(int userChoice, int cpuChoice)
  46. {
  47.     // t = tie, c = computer wins, u = user wins
  48.     if (userChoice == cpuChoice)
  49.         return 't';
  50.     else if ((userChoice == 1) && (cpuChoice == 2))
  51.         return 'c';
  52.     else if ((userChoice == 1) && (cpuChoice == 3))
  53.         return 'u';
  54.     else if ((userChoice == 2) && (cpuChoice == 1))
  55.         return 'u';
  56.     else if ((userChoice == 2) && (cpuChoice == 3))
  57.         return 'c';
  58.     else if ((userChoice == 3) && (cpuChoice == 1))
  59.         return 'c';
  60.     else if ((userChoice == 3) && (cpuChoice == 2))
  61.         return 'u';
  62. }
  63.  
  64. //Ouput who won
  65. void outputWinner(char w, int c)
  66. {
  67.     if (w=='t')
  68.         cout << endl << "The game is a tie. ";
  69.     else if (w== 'u')
  70.         cout << endl << "Congratulations, you win! ";
  71.     else
  72.         cout << endl << "Sorry, you lost. ";
  73.  
  74.     if (c == 1)
  75.         cout << "The CPU chose rock. " << endl;
  76.     else if (c==2)
  77.         cout << "The CPU chose paper. " << endl;
  78.     else
  79.         cout << "The CPU chose scissors. " << endl;
  80. }
  81.  
  82. //Ask if user would like to try again
  83. char tryAgain()
  84. {
  85.     string input = "";
  86.     char response;
  87.  
  88.     //ensures response is a char and is either y, Y, n, or N
  89.     while (true)
  90.     {
  91.         cout << endl;
  92.         cout << "Would you like to try again? (Y/N): ";
  93.         getline(cin, input);
  94.         stringstream myStream(input);
  95.         if (input.length() == 1)
  96.         {
  97.             response = input[0];
  98.             if ((response == 'y') || (response == 'Y') || (response == 'n') || (response == 'N'))
  99.                 break;
  100.         }
  101.         cout << endl << "Please enter 'Y' or 'N'" << endl;
  102.     }
  103.  
  104.     cout << endl;
  105.     return response;
  106. }
  107.  
  108. //int main()
  109. int _tmain(int argc, _TCHAR* argv[])
  110. {
  111.     char response = 'y';
  112.     do
  113.     {
  114.         introduction();
  115.         int choice = userChoice();
  116.         int randomChoice = cpuChoice();
  117.         char w = winner(choice,randomChoice);
  118.         outputWinner(w, randomChoice);
  119.         response = tryAgain();
  120.     }   while ((response == 'y') || (response == 'Y'));
  121.  
  122.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment