/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/cppFiles/file.cc to edit this template */ #include using namespace std; int computerWins = 0; //update it every time the computer wins int playerWins = 0; ////update it every time the human wins /**The computer returns * R/r rock, P/p paper, Ss scissors. * The static var keeps track of how many times this methods was called; * @return R, P S */ char computerPlay() { static int numberOfTimesPlayed = 0; numberOfTimesPlayed++; switch (rand() % 3) { case 0: return 'r'; break; case 1: return 'p'; break; case 2: return 's'; break; } } /**The human( user) is prompted to type * R/r rock, P/p paper, Ss scissors. * or Q/q to stop playing the game. * @return R, P, S, Q */ char humanPlay() { bool validInput = false; char userInput; do { printf("Let's play Rock Paper Scissors!\n"); printf("Please enter r for rock, p for paper, s for scissors (case insensitive): "); cin >> userInput; userInput = std::tolower(userInput, std::locale()); printf("Player entered: %c\n", userInput); if (userInput == 'r' | userInput == 'p' | userInput == 's') { validInput = true; } else { validInput = false; printf("Invalid input. Please enter r, p, or s (case insensitive): "); } } while (!validInput); return userInput; } /**Displays wins and losses for human and computer. * */ void displayStats() { printf("CPU: %i\n", computerWins); printf("Player: %i\n", playerWins); } std::string rps(char input) { std::string output; switch (input) { case 'r': output = "rock"; break; case 'p': output = "paper"; break; case 's': output = "scissors"; break; } return output; } std::string determineWinner(char cpu, char player) { std::string output; if ((cpu == 'r' && player == 'r') | (cpu == 'p' && player == 'p') | (cpu == 's' && player == 's')) { // ties std::string beginningString = "You both chose "; std::string endingString = ". It is a tie."; std::string result = rps(cpu); computerWins++; playerWins++; return beginningString.append(result).append(endingString); } if ((cpu == 'r' && player == 's') | (cpu == 'p' && player == 'r') | (cpu == 's' && player == 'p')) { // CPU Wins std::string beginningString = "You chose"; std::string middle = "The computer chose"; std::string endingString = "The computer wins."; std::string cpuResult = " " + rps(cpu) + ". "; std::string playerResult = " " + rps(player) + ". "; computerWins++; return beginningString.append(playerResult).append(middle).append(cpuResult).append(endingString); } if ((player == 'r' && cpu == 's') | (player == 'p' && cpu == 'r') | (player == 's' && cpu == 'p')) { // Player Wins std::string beginningString = "You chose "; std::string middle = "The computer chose"; std::string endingString = "You win!"; std::string cpuResult = " " + rps(cpu) + ". "; std::string playerResult = " " + rps(player) + ". "; playerWins++; return beginningString.append(playerResult).append(middle).append(cpuResult).append(endingString); } } void gameLoop() { char userInput; bool continueLoop = true; bool playGame = true; do { if (playGame) { printf("%s\n", determineWinner(computerPlay(), humanPlay()).c_str()); displayStats(); } printf("Play again? Y/N (case insensitive):"); cin >> userInput; userInput = std::tolower(userInput, std::locale()); if (!(userInput == 'y' | userInput == 'n')) { printf("invalid input! please enter Y/N...\n"); playGame = false; } if (userInput == 'y') { playGame = true; continueLoop = true; } if (userInput == 'n') { playGame = false; continueLoop = false; } } while (continueLoop); }