1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
//Name: get user choice
//Parameter: NONE
//Return type: string - paper, rock, or scissors, based on whatever the user enters
//Body: Prompts the user for paper, rock, or sciccors.
string get_user_choice()
{
string user_input;
cout << "Enter paper, rock, or scissors" << endl;
cin >> user_input;
return user_input;
}
//Name: get cpu_choice
//Parameter: NONE
//Return type: string - paper, rock, or scissors, (randomly generated)
//Body: randomly generate paper, rock, or sciccors.
string get_cpu_choice()
{
//generate 0,1,2
int num = rand() % 3;
if (num == 0)
{
return "paper";
}
else if (num == 1)
{
return "scissors";
}
else
{
return "rock";
}
}
//Name: show_winner
//Parameters: user- the user selection
// cpu- the cpu selection
//Return type: void
//Body: displays who won the game
void show_winner(string user, string cpu)
{
//list all win configurations
if (user == "paper" && cpu == "rock"
|| user == "rock" && cpu == "scissors"
|| user == "scissors" && cpu == "paper")
{
cout << "You win!" << endl;
}
//if the selections matched....
else if (user == cpu)
{
cout << "Tie. Nobody wins." << endl;
}
//only other parameter tie
else
{
cout << "You lost!" << endl;
}
}
int main()
{
//seed the rbd;
//if we didnt do this, then the cpu would select the same thing every time
srand(time(NULL));
string response, user_choice, cpu_choice;
cout << "Paper-Rock-Scissors v1.0" << endl;
do
{
//get user selection
user_choice = get_user_choice();
//get cpu selection
cpu_choice = get_cpu_choice();
//determine who won
show_winner(user_choice, cpu_choice);
cout << "Play again?" << endl;
cin >> response;
} while (response == "yes");
return 0;
}
|