Code://----------------------------------------------------------------------- // File-----------rock paper scissors // This program plays a game of Rock-Paper-Scissors and displays some // statistics concerning the results. //----------------------------------------------------------------------- #include <iostream> #include <cstdlib> char getInput(); int decideWinner(char, char, int, int); void stats(int&, int&, int&, int&, char&, char&); //----------------------------------------------------------------------- // main //----------------------------------------------------------------------- int main() { char continued=='y'; char input1, input2; int winner; int totalGames, userWins, computerWins, numR, numS, numP=0; while (continued == 'y' || continued == 'Y'); { do //Get person 1's input input1 = getInput( ); //Get computer's input input2 = randomInput( ); //Decide who won winner = decideWinner(input2, input1, computerWins, userWins); //Keep statistics stats(totalGames, numR, numS, numP, 'P', 'R'); //Decide if want to continue cout << "Play again (Y or N)? "; cin >> continued; cout << endl; } do{ cout << totalGames << " games were played and the user won " << userWins << " games." << endl; cout << "The computer won " << computerWins << " games." << endl << endl; cout << "The % of R's is " << numR/(2.0*totalGames) * 100.0 << endl; cout << "The % of P's is " << numP/(2.0*totalGames) * 100.0 << endl; cout << "The % of S's is " << numS/(2.0*totalGames) * 100.0 << endl; return 0; } //----------------------------------------------------------------------- // getInput //----------------------------------------------------------------------- char getInput(char choice) { cout << "Enter R, P, or S: " ; cin >> choice; while (choice != 'R' && choice != 'P' && choice != 'S') { cout << "Enter R, P, or S: " ; cin >> choice; } } //----------------------------------------------------------------------- // randomInput //----------------------------------------------------------------------- char randomInput() { int choiceInt; char choiceChar; choiceInt = rand()%3; if (choiceInt == 0) choiceChar= 'P'; else if (choiceInt == 1) choiceChar = 'R'; else choiceChar ='S'; cout << "Computer chooses: " << choiceChar << endl; return choiceChar; } //----------------------------------------------------------------------- // decideWinner //----------------------------------------------------------------------- int decideWinner(char choice1, char choice2, int &userTotal,int &computerTotal, int winner) { if (choice1 == 'P') { else (choice2 == 'S') winner = 2; computerTotal++; else if (choice2 == 'R') winner = 1; userTotal++; else if (choice2 == 'P') winner = 0; } else if (choice1 == 'S') { if (choice2 == 'S') winner = 0; else if (choice2 == 'R') winner = 2; computerTotal++; else if (choice2 == 'P') winner = 1; userTotal++; } else if (choice1 == 'R') { if (choice2 == 'S') winner = 1; userTotal++; else if (choice2 == 'R') winner = 0; else if (choice2 == 'P') winner = 2; computerTotal++; } if (winner == 1) cout << endl << "You win!" << endl << endl; else if (winner == 2) cout << endl << "Computer wins!" << endl << endl; else cout << endl << "No one wins! " << endl << endl; return winner; } //----------------------------------------------------------------------- // getInput //----------------------------------------------------------------------- void stats(int &totalGames, int &numR, int &numS, int &numP, char input1, char input2) { totalGames++; if (input1 == 'R') numR++; if (input2 == 'R') numR++; if (input1 == 'S') numS++; if (input2 == 'S') numS++; if (input1 == 'P') numP++; if (input2 == 'P') numP++; return; }