0% found this document useful (0 votes)
621 views3 pages

Rock Paper Scissor

This C++ program implements a rock-paper-scissors game between a player and computer. The computer periodically switches its preferences for rock, paper, or scissors based on the player's previous choices. It tracks wins for both the player and computer, and resets the computer's preferences 10% of the time to change its strategy.

Uploaded by

Tripletsp3
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
621 views3 pages

Rock Paper Scissor

This C++ program implements a rock-paper-scissors game between a player and computer. The computer periodically switches its preferences for rock, paper, or scissors based on the player's previous choices. It tracks wins for both the player and computer, and resets the computer's preferences 10% of the time to change its strategy.

Uploaded by

Tripletsp3
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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') {

player_choice = paper; player_n_paper++; } else if (c == 'S' || c == 's') { player_choice = scissors; player_n_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; cout << "computer wins: " << computer_wins << endl; system("PAUSE"); return EXIT_SUCCESS; } void assign_computer_prefs() { int n = rand0toN1(3); if (n == 0) { comp_first_choice = rock; comp_second_choice = scissors; comp_third_choice = paper; } else if (n == 1) { comp_first_choice = scissors; comp_second_choice = paper; comp_third_choice = rock; } else { comp_first_choice = paper; comp_second_choice = rock; comp_third_choice = scissors; } } // Reset computer preferences, based on history of // player choices. // void reset_prefs() { if (player_n_rock > player_n_paper && player_n_rock > player_n_scissors) { comp_first_choice = paper; comp_second_choice = rock; comp_third_choice = scissors; } else if (player_n_paper > player_n_scissors) { comp_first_choice = scissors; comp_second_choice = paper; comp_third_choice = rock;

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

You might also like