Advertisement
axyd

taf12apr16

Apr 12th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5. using namespace std;
  6.  
  7. //This program is a game of rock, paper, scissors.
  8.  
  9. void pcAI(int, string &); //Function Prototype
  10. void Converter(int, string &);
  11. void winner(string, string, string&);
  12.  
  13. int main(){
  14.     char yup = 'y';
  15.     while (yup == 'y' || yup == 'Y'){ //run until user does not want to continue
  16.         cout << "\nLet's Play A Game of Rock, Paper, Scissors! \n\nPick your Weapon:\n";
  17.         cout << "\t(1) Rock.\n\t(2) Paper.\n\t(3) Scissors.\n";
  18.  
  19.         int usrInput, pcx = 0;
  20.         string menuStr, pcIn; //used to fetch name of choice
  21.         cin >> usrInput;
  22.  
  23.         Converter(usrInput, menuStr); //send user choice and get equivalent string back
  24.         pcAI(pcx, pcIn); //(garbage, string) from PC
  25.  
  26.         if (menuStr != pcIn){
  27.             cout << "\t\tYou: <" << menuStr << "> vs Computer: <" << pcIn << ">.\n";
  28.             string winStr;
  29.             winner(menuStr, pcIn, winStr); //Find the winner
  30.             cout << winStr << endl;
  31.         }
  32.         else //catch the same values
  33.             cout << "\t<" << menuStr << "> vs <" << pcIn << "> are the same, redraw.\n";
  34.  
  35.         cout << "\n\n\t\t\tAnother game (y|Y) ? ";
  36.         cin >> yup; //y=continue, anything else exit loop
  37.         cin.ignore();
  38.     }      
  39.     cout << "\nThanks for playing! " << endl;
  40.     cin.ignore();
  41.     return 0;
  42. }
  43.  
  44. void pcAI(int toMenu, string &out)
  45. {
  46.     srand((int)time(0)); //Generates a number from 1-3.
  47.     toMenu = (rand() % 3 + 1);
  48.     Converter(toMenu, out); //Send number to Converter and fetch/output string
  49. }
  50.  
  51. void Converter(int in, string &out) //Convert number to name
  52. {
  53.     switch (in){ //####FYI####  Switch can also accept integers
  54.     case 1:
  55.         out = "Rock";
  56.         break;
  57.     case 2:
  58.         out = "Paper";
  59.         break;
  60.     case 3:
  61.         out = "Scissors";
  62.         break;
  63.     }
  64. }
  65.  
  66. void winner(string player, string pc, string &out){ //Decide winner
  67.     if (player == "Rock" && pc == "Scissors")
  68.         out = "\t\tThe rock smashes the scissors. PLAYER won, Congrats!";
  69.     else if (player == "Scissors" && pc == "Paper")
  70.         out = "\t\tScissors cuts paper. PLAYER won, Congrats!";
  71.     else if (player == "Paper" && pc == "Rock")
  72.         out = "\t\tPaper wraps rock. PLAYER won, Congrats!";
  73.     else if (pc == "Rock" && player == "Scissors")
  74.         out = "\t\tThe rock smashes the scissors. COMPUTER won.";
  75.     else if (pc == "Scissors" && player == "Paper")
  76.         out = "\t\tScissors cuts paper. COMPUTER won.";
  77.     else if (pc == "Paper" && player == "Rock")
  78.         out = "\t\tPaper wraps rock. COMPUTER won.";
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement