You are on page 1of 3

// Exercise 10.03.03 // Rock-Paper-Scissors game; // this version similar to previous (10.3.

2) version, but // it periodically switch computer prefs based on player's // choices. // #include <iostream> #include <string> #include <ctime> using namespace std; enum class Choice { rock, paper, scissors }; using namespace Choice; Choice player_choice; Choice computer_choice; // Holds user's move // Holds computer move

Choice comp_first_choice = rock; // These hold computer Choice comp_second_choice = scissors; // preferences. Choice comp_third_choice = paper; int player_wins = 0; int computer_wins = 0; // Track player victories. // Track computer wins.

int player_n_rock = 0; // These vars track player choices. int player_n_scissors = 0; int player_n_paper = 0; string words[3] = {"rock", "paper", "scissors" }; Choice get_computer_choice(); void assign_computer_prefs(); void reset_prefs(); 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. assign_computer_prefs(); string input_str; int c; while (true) { int m = rand0toN1(10); // Reset comp. prefs 10% of time if (m == 0) reset_prefs(); 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; player_n_rock++; } else if (c == 'P' || c == 'p') {

comp_second_choice = paper. . } else { comp_first_choice = paper. player_n_paper++. } else if (c == 'E' || c == 'e') { break. } void assign_computer_prefs() { int n = rand0toN1(3). comp_second_choice = rock. comp_third_choice = rock. cout << "You chose " << words[p]. comp_third_choice = scissors. } } // Reset computer preferences. comp_second_choice = paper. cout << ". } else if (n == 1) { comp_first_choice = scissors." << endl. comp_third_choice = scissors. comp_second_choice = rock. int c = (int) computer_choice. comp_third_choice = paper.player_choice = paper. comp_third_choice = rock. comp_second_choice = scissors. cout << ". cout << "computer wins: " << computer_wins << endl. based on history of // player choices. decide_winner(). } cout << "player wins: " << player_wins << endl. continue. return EXIT_SUCCESS. if (n == 0) { comp_first_choice = rock. player_n_scissors++. // void reset_prefs() { if (player_n_rock > player_n_paper && player_n_rock > player_n_scissors) { comp_first_choice = paper. int p = (int) player_choice. } else { cout << "Invalid input. cout << "I chose " << words[c]." << endl. } else if (player_n_paper > player_n_scissors) { comp_first_choice = scissors. } computer_choice = get_computer_choice(). system("PAUSE")." << endl. } else if (c == 'S' || c == 's') { player_choice = scissors.

} cout << endl. and . } } // Get Computer Choice // Return first. cout << "I WIN!" << endl. second. } string get_msg(Choice winner) { if (winner == rock) return string("Rock smashes scissors. return.} else { comp_first_choice = rock. cout << "YOU WIN!" << endl.. return comp_third_choice..5. else return string("Scissors cuts paper." << endl << endl. } else { cout << get_msg(computer_choice).c == 1 || p . "). else if (n <= 7) return comp_second_choice.. if (n <= 4) return comp_first_choice. comp_third_choice = paper. . computer_wins++. comp_second_choice = scissors. third choices // at . player_wins++.3. if (p .c == -2) { cout << get_msg(player_choice). ")... ").. } int p = (int) player_choice. } int rand0toN1(int n) { return rand() % n. } void decide_winner() { if (player_choice == computer_choice) { cout << "Result is a tie.2 frequency // Choice get_computer_choice() { int n = rand0toN1(10). else if (winner == paper) return string("Paper covers rock. int c = (int) computer_choice. } .