Write a rock paper scissors game
#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;
}
Comments
Leave a comment