In: Computer Science
C++ questions, Please make sure to divide your program into functions which perform each major task.
The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic:
ROCK defeats SCISSORS (“smashes”)
PAPER defeats ROCK (“wraps”)
SCISSORS defeats PAPER (“slices”)
If both players choose the same selection the game ends in a tie. Write a program that asks the user to select one of the three choices. The computer will randomly choose one of the three options as well. Your program will then display both players' selections as well as the who won the game.
Below is the code:
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
int main() // main function
{
char ch;
int win = 0; //variables declaration
int tie = 0;
int lose = 0;
do
{
int choice;
cout << "Rock, Paper, Scissors Game" << endl;
cout << "=========================="<<endl;
cout << "Enter your choice, Press 1 for Rock, 2 for Paper, 3
for Scissors:" << endl;
cin >> choice;
int ai = rand() % 3 + 1;
cout << "Computer's choice: " << ai <<
endl;
if(choice == 1 && ai == 1)
{
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if(choice ==1 && ai== 2)
{
cout << "Rock is covered by Paper the computer wins!."
<< endl;
lose++;
}
else if(choice == 1 && ai == 3)
{
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if(choice == 2 && ai == 1)
{
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if(choice == 2 && ai == 2)
{
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if(choice == 2 && ai == 3)
{
cout << "Paper is cut by Scissors the computer wins!"
<< endl;
lose++;
}
else if( choice == 3 && ai == 1)
{
cout << "Scissors are crushed by Rock computer wins!"
<< endl;
lose++;
}
else if( choice == 3 && ai == 2)
{
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if(choice == 3 && ai == 3)
{
cout << "Scissors meet Scissors its a tie!" <<
endl;
tie++;
}
else{
cout << "Please select 1, 2 or 3" << endl;
}
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" <<
endl;
cin >> ch;
} while(ch == 'Y' || ch == 'y');
return 0;
}
You have to provide your input among 1, 2, or 3 numbers. It pics a random number as a computer number and then starts the game. Once the play is over, this prompts the user to continue the game or not. If the user enters y or Y then the game continues
Below is the output screenshot:
---Adding the version 2 of the code with the required functions-----
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
int win = 0; //variables declaration
int tie = 0;
int lose = 0;
//reading the choice from user
int readChoice()
{
int choice = 0;
cout << "Enter your choice, Press 1 for Rock, 2 for Paper, 3
for Scissors:" << endl;
cin >> choice;
return choice;
}
//generating the computer's choice:
int getComputersChoice()
{
int computerChoice= rand() % 3 + 1;
cout << "Computer's choice: " << computerChoice
<< endl;
return computerChoice;
}
//starts the game
void playTheGame(int choice, int ai)
{
if(choice == 1 && ai == 1)
{
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if(choice ==1 && ai== 2)
{
cout << "Rock is covered by Paper the computer wins!."
<< endl;
lose++;
}
else if(choice == 1 && ai == 3)
{
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if(choice == 2 && ai == 1)
{
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if(choice == 2 && ai == 2)
{
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if(choice == 2 && ai == 3)
{
cout << "Paper is cut by Scissors the computer wins!"
<< endl;
lose++;
}
else if( choice == 3 && ai == 1)
{
cout << "Scissors are crushed by Rock computer wins!"
<< endl;
lose++;
}
else if( choice == 3 && ai == 2)
{
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if(choice == 3 && ai == 3)
{
cout << "Scissors meet Scissors its a tie!" <<
endl;
tie++;
}
else{
cout << "Please select 1, 2 or 3" << endl;
}
}
int main() // main function
{
char ch;
do
{
int choice, ai;
cout << "Rock, Paper, Scissors Game" << endl;
cout << "=========================="<<endl;
choice = readChoice();
ai = getComputersChoice();
playTheGame(choice, ai);
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" <<
endl;
cin >> ch;
} while(ch == 'Y' || ch == 'y');
return 0;
}