Answer to Question #173121 in C++ for Lisa

Question #173121

Write a rock paper scissors game


1
Expert's answer
2021-03-18T20:08:24-0400
#include <iostream>
#include <time.h>


using namespace std;


const char AVAILABLE_CHOICES[3] = {'R','P','S'};


int main()
{
    char playAgain;
    char userChoice, computerChoice;


    do
    {
        cout << "Your choice(R,P,S)? ";
        cin >> userChoice;


        computerChoice = AVAILABLE_CHOICES[rand()%3];
        cout << "Computer choice: " << computerChoice << endl;


        if(userChoice == computerChoice)
            cout << "It's a tie" << endl;
        else
        {
            if(userChoice == 'R')
            {
                if(computerChoice == 'S')
                    cout << "Congratulations! You won!!!" << endl;
                else
                    cout << "You lost!" << endl;
            }
            else if(userChoice == 'S')
            {
                if(computerChoice == 'P')
                    cout << "Congratulations! You won!!!" << endl;
                else
                    cout << "You lost!" << endl;
            }
            else
            {
                if(computerChoice == 'R')
                    cout << "Congratulations! You won!!!" << endl;
                else
                    cout << "You lost!" << endl;
            }
        }


        cout << endl << "Do you want to play again?[Y/N] ";
        cin >> playAgain;


    }while(playAgain=='Y');


    cout << endl << "Thanks for playing!!!" << endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS