You are on page 1of 2

// Exercise 10.03.01 // Rock-Paper-Scissors game; // this version tracks player and computer wins.

// #include <iostream> #include <string> #include <ctime> using namespace std; enum class Choice { rock, paper, scissors }; using namespace Choice; Choice player_choice; Choice computer_choice; int player_wins = 0; int computer_wins = 0; // Holds user's move // Holds computer move // Track player victories. // Track computer wins.

string words[3] = {"rock", "paper", "scissors" }; Choice get_computer_choice(); void decide_winner(); string get_msg(Choice winner); int rand0toN1(int n); int main(int argc, char *argv[]) { srand(time(NULL)); // Set seed for randomization. string input_str; int c; while (true) { cout << "Enter Rock, Paper, Scissors, or Exit: "; getline(cin, input_str); if (input_str.size() < 1) { cout << "Invalid input." << endl; continue; } c = input_str[0]; if (c == 'R' || c == 'r') player_choice = rock; else if (c == 'P' || c == 'p') player_choice = paper; else if (c == 'S' || c == 's') player_choice = scissors; else if (c == 'E' || c == 'e') break; else { cout << "Invalid input." << endl; continue; } computer_choice = get_computer_choice(); int p = (int) player_choice; int c = (int) computer_choice; cout << "You chose " << words[p]; cout << "," << endl; cout << "I chose " << words[c]; cout << "," << endl; decide_winner(); } cout << "player wins: " << player_wins << endl;

else return string("Scissors cuts paper.. ")." << endl << endl. computer_wins++. if (n == 0) return rock. int c = (int) computer_choice. return scissors. player_wins++. cout << "I WIN!" << endl. } string get_msg(Choice winner) { if (winner == rock) return string("Rock smashes scissors. if (n == 1) return paper.. } int p = (int) player_choice. ").. "). } void decide_winner() { if (player_choice == computer_choice) { cout << "Result is a tie. } cout << endl.c == -2) { cout << get_msg(player_choice). if (p . } else { cout << get_msg(computer_choice). } Choice get_computer_choice() { int n = rand0toN1(3).. return.cout << "computer wins: " << computer_wins << endl. else if (winner == paper) return string("Paper covers rock.. } . cout << "YOU WIN!" << endl. } int rand0toN1(int n) { return rand() % n. system("PAUSE").c == 1 || p . return EXIT_SUCCESS..