86.2k views
2 votes
In C++ programming please.

Rock, Paper, Scissors Game.
a) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows:
1. When the program begins, a random number in the range of 1 through 3 is generated (1+rand()%3). If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, them the computer has chosen scissors. (Don't display computer's choice yet).
2. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. (You can use a menu if you prefer).
3. The computer's choice is displayed.
4. A winner is selected according to the rock paper scossors game riles.
Be sure to divide the program into the following functions:
int getUserChoice ();
The getUserChoice function displays a menu allowing the user to select rock, paper, or scissors.
4 to Quit the game
The function then returns 1 for rock, or 2 for paper, or 3 for scissors.
User input validation of the choices
int getComputerSchoice ();
The getComputerChoice function returns the computer's game choice.
It returns 1 for rock (via the ROCK constant), or 2 for paper (via the PAPER constant), or 3 for scissors
determineWinner (int, int);
The determineWinner function accepts the user's game choice and the computer's game choice as arguments and displays the choices, and a message indicating the winner.
void displayChoice (int);
The displayChoice function accepts an integer argument and displays rock, paper, or Scissors.
int main ()
Loop while user choice is not 4 (Quit the game)

User Testuser
by
5.3k points

1 Answer

0 votes

Answer:

A program was written in C++ that allows the user to play the game of Rock, Paper, Scissors against the computer.

Step-by-step explanation:

Solution

C++ CODE:

#include<iostream>

#include <locale>

#include<string>

#include<cstdlib>

#include<time.h>

using namespace std;

int play(int user,int cpu){

return(user - cpu); //here is just substracting and returning the value entered by user and cpu

}

void display(int choice){ //to display the choices entered by user or cpu

if(choice == 1){ //1 for Rock

cout<<"Rock"<<endl;

}else if(choice == 2){ //2 for Paper

cout<<"Paper"<<endl;

}else{ //3 for Scissors

cout<<"Scissors"<<endl;

}

}

void winner(int win){

if(win == 0){ //if user and cpu entered same value

cout<<"A game is a draw."<<endl;

}else if(win == 1){ //if user entered Paper and cpu entered Rock or if user entered Scissors and cpu entered Paper

cout<<"Hurray You won."<<endl;

}else if(win == -1){ //if user entered Rock and cpu entered Paper or if user entered Paper and cpu entered Scissors

cout<<"CPU won."<<endl;

}else if(win == 2){ //if user entered Scissors and cpu entered Rock

cout<<"CPU won."<<endl;

}else if(win == -2){ //if user entered Rock and cpu entered Scissors

cout<<"Hurray You won."<<endl;

}

}

int cpu_choice(){

srand (time(NULL)); //this function is to generate random from time to time

int cpu = 1000;

while(cpu > 3){ //to generate random number between 1 to 3

cpu = (rand() % cpu) +1;

}

return cpu;

}

int main(){

int choice=0,cpu=0,win=0;

locale loc;

string ch;

cout<<"Enter your choice :"<<endl;

cout<<"1. Rock\\2. Paper\\3. Scissors"<<endl<<"\t";

cin>>ch;ch[0] = toupper(ch[0],loc); //converting the first element to char if by misktake the user have entered lower case

if(ch.compare("Rock") == 0 || ch.compare("Paper") == 0 || ch.compare("Scissors") == 0) ch[0] == 'S') choice = 3; //if the user enter scissors or Scissors

cout<<"CPU: ";

cpu = cpu_choice(); //get cpu choice

display(cpu); //display the choice entered by the cpu using the random number

win = play(choice,cpu); //call the function to play the game and returns the winner

winner(win); //display the winner

else{ //if entered choice is wrong

cout<<"wrong choice!"<<endl;

}

return 0;

}

User Lee Oades
by
5.5k points