/**************** Thomas Baillie CSE 231 (section 17) Project 3 - build date: 9/13/06 *******************/ #include #include // used to manipulate/create values of type String #include // utilizes the system clock to seed for a semi-random number using namespace std; /* Begin variable declarations*/ string compChoice(), // this function reports the user's selection userChoice(); // this function reports the computer's selection //getPref(); char choice; // this char hold's the users input commands. void playGame(); // initializes the game routine int compWins = 0; // the number of computer wins int userWins = 0; // the number of user wins int scissorsCount = 0; // the number of times the user chooses scissors int paperCount = 0; // the number of times the user chooses paper int rockCount = 0; // the number of times the user chooses rock int tie = 0; // the number of ties int highest = 0; int main() { cout << "Are you ready to play Rock, Paper, Scissors? Your choices are: \n\n"; cout << " r/R for Rock\n"; cout << " p/P for Paper\n"; cout << " s/S for Scissors\n"; cout << " q/Q to Quit\n\n"; cout << "Choose your weapon: "; cin >> choice; // show menu and take in initial user input while (choice != 'Q' && choice != 'q'){ switch (choice){ // if the user doesn't quit, the program loops through this switch forever case 's': case 'S': // if the user chooses scissors, add 1 to scissorsCount and then start the game scissorsCount++; playGame(); cout << "\nChoose your weapon (or q/Q to quit): "; cin >> choice; break; case 'p': case 'P': // if the user chooses paper, add 1 to paperCount and then start the game paperCount++; playGame(); cout << "\nChoose your weapon (or q/Q to quit): "; cin >> choice; break; case 'r': case 'R': // if the user chooses rock, add 1 to rockCount and then start the game rockCount++; playGame(); cout << "\nChoose your weapon (or q/Q to quit): "; cin >> choice; break; default: // if the user enters invalid input, force them to enter something valid cout << "Invalid input! Please try again.\n\n"; cout << "Choose your weapon: "; cin >> choice; break; } } // if the user enters q/Q, the above switch is bypassed and game scores are tallied up cout << "\n\nYou've chosen to end the game. The final results are:\n\n"; cout << "The computer won " << compWins << " times\n"; cout << "You won " << userWins << " times\n"; cout << "We tied " << tie << " times\n"; cout << "You selected Rock " << rockCount <<" times\n"; cout << "You selected Paper " << paperCount << " times\n"; cout << "You selected Scissors " << scissorsCount << " times\n\n"; cout << "Thanks for playing!\n\n"; system("pause"); } // end main() /* The playGame() function evaluates the data returned by the userChoice() and compChoice() functions to determine/output the winner of each round. */ void playGame() { string userPref = "null"; cout << "\n\n[User Choice]: " << userChoice() << "\n"; cout << "[Computer Choice]: " << compChoice() << "\n"; if (userChoice() == "paper" && compChoice() == "rock"){ cout << "You win!\n\n"; userWins++; } else if (userChoice() == "scissors" && compChoice() == "paper"){ cout << "You win!\n\n"; userWins++; } else if (userChoice() == "rock" && compChoice() == "scissors"){ cout << "You win!\n\n"; userWins++; } else if (userChoice() == compChoice()){ cout << "You've tied!\n\n"; tie++; } else { cout << "You lose!\n\n"; compWins++; } // GODDAMNIT, MAINTAIN THE ACTUAL CURRENT PREFERENCE, FUCK! if (rockCount > scissorsCount || rockCount > paperCount){ highest = rockCount; userPref = "rock"; } else if (scissorsCount > rockCount || scissorsCount > paperCount){ highest = scissorsCount; userPref = "scissors"; } else if (paperCount > rockCount || paperCount > scissorsCount){ highest = paperCount; userPref = "paper"; } cout << "lol teh usarpreff: " << userPref; }// end playGame() /* The userChoice() function takes in the char value stored in choice and returns the corresponding string value (rock/paper/scissors, respectively) */ string userChoice() { string userChoice; switch (choice){ case 'r': userChoice = "rock"; break; case 'p': userChoice = "paper"; break; case 's': userChoice = "scissors"; break; default: userChoice = "null"; break; } return userChoice; }// end userChoice() /* compChoice() utilizes the srand() (which seeds the system clock) and rand() functions to generate a random number between 0-2 inclusive and designates 0,1,2 as rock,paper,scissors, respectively. */ string compChoice() { int randVal = 0; srand((unsigned)time(0)); randVal = rand()%3; if (randVal == 0) return "paper"; else if (randVal == 1) return "rock"; else return "scissors"; }//compChoice()