Number 1
// Rock Paper Scissors
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
enum choice {ROCK, PAPER, SCISSORS};
choice select;
char user;
bool play_again;
string play;
int num_games = 0;
int player_wins = 0;
int computer_wins = 0;
bool tie = false;
int num_ties = 0;
// Ask the user if they would like to play.
cout << "Would you like to play Rock, Paper, Scissors? (y or n): ";
cin >> play;
// If they answer yes, then play, otherwise no.
if(play == "y" || play == "Y"){
play_again = true;
num_games++;
}
// Start playing the actual game if they want to play.
while(play_again != false){
cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
cin >> user;
cout << endl;
// Taking the input and applying it to constant in enum.
if (user == 'r' || user == 'R'){
select = ROCK;
}
else if (user == 'p' || user == 'P'){
select = PAPER;
}
else if (user == 's' || user == 'S'){
select = SCISSORS;
}
else {
cout << "Come on... That's not an option." << endl;
cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
cin >> user;
}
long seed = time(NULL); // gets current time
srand(seed);
// Computer's random selection
int computer = rand();
//DEBUG: cout << computer << endl;
// Computer's random selection
computer = rand() % 3;
//DEBUG: cout << computer << endl << endl;;
//DEBUG: cout << "Here is a test: " << select << endl;
// Applying the computer's selection to the appropriate value in enum.
choice cpu_select;
if (computer == 0){
cpu_select = ROCK;
}
else if (computer == 1){
cpu_select = PAPER;
}
else if (computer == 2){
cpu_select = SCISSORS;
}
// In the even of a tie. [This creates an infinite loop if there is ever a tie for some reason.]
/*
if (cpu_select == select){
tie = true;
}
while(tie == true){
cout << endl << "It's a tie, Pick again please: ";
cin >> user;
if(cpu_select != select)
tie = false;
}
*/
// Displays each respective selection.
cout << endl << endl << "Your selection is " << select << endl;
cout << "The computer selects " << cpu_select << endl;
bool you_win = false;
// Win conditions
if ((select == ROCK && cpu_select == SCISSORS) || (select == PAPER && cpu_select == ROCK) || (select == SCISSORS && cpu_select == PAPER)){
cout << endl << "You win." << endl;
player_wins ++;
}
else if((cpu_select == ROCK && select == SCISSORS) || (cpu_select == PAPER && select == ROCK) || (cpu_select == SCISSORS && select == PAPER)){
cout << endl << "The computer wins." << endl;
computer_wins ++;
}
else if (cpu_select == select){
num_ties++;
}
// Show the results of all the games.
cout << endl << "You have won " << player_wins << " games." << endl;
cout << "The computer has won " << computer_wins << " games." << endl;
cout << endl << "There have been " << num_ties << " ties." << endl;
// Ask if the player would like to play again.
cout << endl << endl << "Would you like to play again?: ";
cin >> play;
// Exit the loop if the player chooses no.
if (play == "n" || play == "N"){
play_again = false;
}
else if (play == "y" || play == "Y"){
num_games++;
}
}
// DEBUG: Shows number of games played.
cout << endl << "You just played " << num_games << " games of Rock Paper Scissors." << endl << endl;
system("pause");
return 0;
}