Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function called rps that will simulate playing rock, paper, scissors wit

ID: 642786 • Letter: W

Question

Write a function called rps that will simulate playing rock, paper, scissors with the computer. Each time the function is called, the game is played once. The function rps returns NO value, but does take in two values as parameters:

Two char parameters (NO MORE, NO LESS) will be passed to this function. The first one represents the user's choice of rock, paper, or scissors. The second char parameter represents the result of the game, either win, loss, or tie. ***For this second parameter, you should allow the function to change this parameter as you'll check to see what it is back in main() after this function has been called*** (We'll learn this on Thurs, 2/26, reference parameters.)

Inside the function, first get the computer's choice. Remember the computer's choice of rock, paper, or scissors involves randomness. You can accomplish this using rand(), restricting the range of values rand returns to 3. (you can do the seeding in main() ) (see online notes).

Print out the computer's choice and based on it, print out what the user chose, and whether they won, lost, or tied (see sample runs below). Then set the second parameter representing the result of the game to 'w', 'l', or 't' appropriately. Remember, this function should **NOT** return a value.

Write a function called printResults that will be called upon the user quitting the game. The function will take in as parameters three integers (NO MORE, NO LESS) representing wins, losses, and ties, and reutrn a bool (true if the number of wins is greater than the number of losses, and false otherwise). The table you see in the sample runs below is printed from this function. The table should consist of number of wins, losses, and ties, as well as the percentage for each (printed to two decimal places). At the end of the table you should print the total number of games played. Then the function should return it's true or false value.

Along with these functions write a main() routine that should do the following:

Welcome the user

Enter a loop that allows the user to play the game, and prompt for their choice of rock ('r'), paper ('p'), or scissors ('s') or quit ('q'). You do not have to allow capital letters, but you must ***force*** the user to enter a valid option.

When the user chooses to play the game by entering 'r', 'p', or 's', call your rps function, passing the user's selected value as well as another character variable that will store the result of the game (remember, the function will change this value, and the changes to it will be reflected back in main() ) You should initialize this variable representing the game result in the beginning of main to '' the NULL character.

After you call your rps function, check the result variable to see if the game was a win ('w'), tie ('t'), or loss ('l'), and keep count of each appropriately.

Play over and over until the user enters 'q' to Quit. Before the program ends, call your printResults function. If printResults returns true meaning the user had more wins than losses, print "You played great!". If not, print "Meh...".

Remember your output should match mine exactly for the most points. See the following sample runs to see how your program should behave:

Explanation / Answer

Program Code:

// RockPaperScissors-Functions.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <time.h>

#include <iomanip>

using namespace std;

void rps(char userchoice, char &status);

void printUserChoice(char userch);

bool printResults(int won, int loose, int tie);

int main()

{

     srand(time(NULL));

     char ch='';

     int win=0;

     int lose=0;

     int ties=0;

     bool flag=false;

     char status;

     cout<<endl;

     cout<<"Welcome to Rock Paper Scissors Game!"<<endl;

     do

     {

          cout<<endl;

          cout<<"Rock(r), Paper(p), Scissors(s) ?"<<endl;

          cout<<"or quit(q). --> ";

          cin>>ch;

          if(ch!='r'&& ch!='p'&& ch!='s' && ch!='q')

          {

              cout<<"Please enter r, p, s >> ";

              cin>>ch;

          }

     if(ch=='q')

              break;

          rps(ch, status);

          if(status=='w')

          {

              ++win;

              cout<<"w "<<win<<endl;

          }

          else if(status=='l')

          {

              ++lose;

              cout<<"l "<<lose<<endl;

          }

          else if(status=='t')

          {

              ++ties;

              cout<<"t "<<ties<<endl;

          }

     }while(ch!='q');

     flag=printResults(win, lose, ties);

     if(flag)

          cout<<"Your played great!"<<endl;

     else

          cout<<"Meh..."<<endl;

     cout<<"Thanks for playing!"<<endl;

     system("pause");

     return 0;

}

void rps(char userchoice, char &status)

{

     double random=rand()%3+1;

     char computer;

     if(random==1)

     {

          computer='r';

          cout<<"Computer choose... ROCK!"<<endl;

     }

     else if(random==2)

     {

          computer='p';

          cout<<"Computer choose... PAPER!"<<endl;

     }

     else if(random==3)

     {

          computer='s';

          cout<<"Computer choose... SCISSORS!"<<endl;

     }

     cout<<"";

     if(computer==userchoice)

     {

          printUserChoice(userchoice);

          cout<<"You TIED!"<<endl;

          status='t';

     }

     else if(userchoice=='r')

     {

          printUserChoice(userchoice);

          if(computer=='s')

          {            

              status='w';

              cout<<"You WIN!"<<endl;

          }

          if(computer=='p')

          {

              status=='l';

              cout<<"You LOSE!"<<endl;

          }

     }

     else if(userchoice=='p')

     {

          printUserChoice(userchoice);

          if(computer=='r')

          {

              status='w';

              cout<<"You WIN!"<<endl;

          }        

          if(computer=='s')

          {

              status=='l';

              cout<<"You LOSE!"<<endl;

          }

     }

     else if(userchoice=='s')

     {

          printUserChoice(userchoice);

          if(computer=='p')

          {

              status='w';

              cout<<"You WIN!"<<endl;

          }

          if(computer=='r')

          {

              status=='l';

              cout<<"You LOSE!"<<endl;

          }

     }

}

void printUserChoice(char userch)

{

     if(userch=='r')

          cout<<"You choose ROCK!"<<endl;

     if(userch=='p')

          cout<<"You choose PAPER!"<<endl;

     if(userch=='s')

          cout<<"You choose SCISSORS!"<<endl;

}

bool printResults(int won, int loose, int tie)

{

     bool f=false;

     double winper=0.00;

     double losper=0.00;

     double tieper=0.00;

     if(won>loose)

          f=true;

     else

          f= false;

     double total=won+loose+tie;

     if(total!=0)

     {

     winper=(won/total)*100;

     losper=(loose/total)*100;

     tieper=(tie/total)*100;

     }

     else

     {

          winper=0.00;

          losper=0.00;

          tieper=0.00;

     }

     cout<<""<<setw(20)<<"Totals"<<setw(20)<<"Percentage"<<endl;

     cout<<"------------------------------------------------"<<endl;

     cout<<"Wins"<<setw(15)<<won<<setw(20)<<setprecision(2)<<winper<<"%"<<endl;

     cout<<"Losses"<<setw(13)<<loose<<setw(20)<<setprecision(2)<<losper<<"%"<<endl;

     cout<<"Ties"<<setw(15)<<tie<<setw(20)<<setprecision(2)<<tieper<<"%"<<endl;

     cout<<"------------------------------------------------"<<endl;

     cout<<"Total games played: "<<(won+loose+tie)<<endl;

     return f;

}

Sample output:

Welcome to Rock Paper Scissors Game!

Rock(r), Paper(p), Scissors(s) ?

or quit(q). --> r

Computer choose... SCISSORS!

You choose ROCK!

You WIN!

Rock(r), Paper(p), Scissors(s) ?

or quit(q). --> p

Computer choose... ROCK!

You choose PAPER!

You WIN!

Rock(r), Paper(p), Scissors(s) ?

or quit(q). --> s

Computer choose... PAPER!

You choose SCISSORS!

You WIN!

Rock(r), Paper(p), Scissors(s) ?

or quit(q). --> q

              Totals          Percentage

------------------------------------------------

Wins              3                 100%

Losses            0                   0%

Ties              0                   0%

------------------------------------------------

Total games played: 3

Your played great!

Thanks for playing!

Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote