Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <string>
- using namespace std;
- //This program is a game of rock, paper, scissors.
- void pcAI(int, string &); //Function Prototype
- void Converter(int, string &);
- void winner(string, string, string&);
- int main(){
- char yup = 'y';
- while (yup == 'y' || yup == 'Y'){ //run until user does not want to continue
- cout << "\nLet's Play A Game of Rock, Paper, Scissors! \n\nPick your Weapon:\n";
- cout << "\t(1) Rock.\n\t(2) Paper.\n\t(3) Scissors.\n";
- int usrInput, pcx = 0;
- string menuStr, pcIn; //used to fetch name of choice
- cin >> usrInput;
- Converter(usrInput, menuStr); //send user choice and get equivalent string back
- pcAI(pcx, pcIn); //(garbage, string) from PC
- if (menuStr != pcIn){
- cout << "\t\tYou: <" << menuStr << "> vs Computer: <" << pcIn << ">.\n";
- string winStr;
- winner(menuStr, pcIn, winStr); //Find the winner
- cout << winStr << endl;
- }
- else //catch the same values
- cout << "\t<" << menuStr << "> vs <" << pcIn << "> are the same, redraw.\n";
- cout << "\n\n\t\t\tAnother game (y|Y) ? ";
- cin >> yup; //y=continue, anything else exit loop
- cin.ignore();
- }
- cout << "\nThanks for playing! " << endl;
- cin.ignore();
- return 0;
- }
- void pcAI(int toMenu, string &out)
- {
- srand((int)time(0)); //Generates a number from 1-3.
- toMenu = (rand() % 3 + 1);
- Converter(toMenu, out); //Send number to Converter and fetch/output string
- }
- void Converter(int in, string &out) //Convert number to name
- {
- switch (in){ //####FYI#### Switch can also accept integers
- case 1:
- out = "Rock";
- break;
- case 2:
- out = "Paper";
- break;
- case 3:
- out = "Scissors";
- break;
- }
- }
- void winner(string player, string pc, string &out){ //Decide winner
- if (player == "Rock" && pc == "Scissors")
- out = "\t\tThe rock smashes the scissors. PLAYER won, Congrats!";
- else if (player == "Scissors" && pc == "Paper")
- out = "\t\tScissors cuts paper. PLAYER won, Congrats!";
- else if (player == "Paper" && pc == "Rock")
- out = "\t\tPaper wraps rock. PLAYER won, Congrats!";
- else if (pc == "Rock" && player == "Scissors")
- out = "\t\tThe rock smashes the scissors. COMPUTER won.";
- else if (pc == "Scissors" && player == "Paper")
- out = "\t\tScissors cuts paper. COMPUTER won.";
- else if (pc == "Paper" && player == "Rock")
- out = "\t\tPaper wraps rock. COMPUTER won.";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement