Question

In: Computer Science

Code the game of Rock, Paper, Scissors between a human player and the computer. You can...

Code the game of Rock, Paper, Scissors between a human player and the computer. You can check out the game on Wikipedia if you are not familiar with it. Create a 4 option menu with the human player choices, plus the option of exiting the game. Randomly determine the computer’s choice (although a great deal of AI research has gone in to determining the best computer move).

• Loop the game until the human player exits.

• Count the number of times the computer and the human each won.

When you’re done, display the total number of hands played, the number of times the human won and the number of times the computer won. Declare the highest total of winning hands the game winner.

Solutions

Expert Solution

//C++ program

/*
(1) program gets input from user by displaying options ,with getUserChoice() function.
and Dispaly user choice in text using function displayChoice().

(2) program gets input from computer choice by random number generator function ,with getComputerChoice() function.
and Dispaly computer choice in text using function displayChoice().

(3)now it compare User choice and computer choice according to rule given .uses function determineWinner();

(4)if return value of function determineWinner() is true ,then User is winner and it displays Rules according to which user wins
and Loop Breaks.

(5)if return value of function determineWinner() is false,then Match Tie. And program keeps reapting steps 1-3;

*/
#include<iostream>
#include<stdlib.h>
using namespace std;

int getUserChoice(){
   int choice;
   cout<<"1:Rock \n2:Paper \n3:Scissors \n4:Lizard \n5:Spock\n";
   cout<<"Enter your choice : ";
   cin>>choice;
   return choice;
}


int getComputerChoice(){
   return rand()%5+1;
}


bool determineWinner(int User_choice, int Computer_choice){
   return (User_choice == 3 && Computer_choice == 2) || (User_choice == 2 && Computer_choice == 1) ||
           (User_choice == 1 && Computer_choice == 4) || (User_choice == 4 && Computer_choice == 5) ||
           (User_choice == 5 && Computer_choice == 3) || (User_choice == 3 && Computer_choice == 4) ||
           (User_choice == 4 && Computer_choice == 2) || (User_choice == 2 && Computer_choice == 5) ||
           (User_choice == 5 && Computer_choice == 1) || (User_choice == 1 && Computer_choice == 3);
}


void displayChoice(int choice){
   string option[]={
           "","Rock","Paper","Scissors","Lizard","Spock"
   };
   cout<<option[choice]<<"\n";
  
}

int main(){
   int User_choice , Computer_choice;
   int total_game=0,user_won=0,computer_won=0,tie=0;
   char exit;
  
   string Rule[]={
           "Scissors cut paper","Paper covers rock","Rock crushes lizard","Lizard poisons Spock",
           "Spock smashes (or melts) scissors","Scissors decapitate lizard","Lizard eats paper",
           "Paper disproves Spock","Spock vaporizes rock","Rock breaks scissors"
   };
  
   do{
       total_game++;
       User_choice = getUserChoice();
       cout<<"Your choice : "; displayChoice(User_choice);
       Computer_choice = getComputerChoice();
       cout<<"Computer choice : "; displayChoice(Computer_choice);
       if(determineWinner (User_choice , Computer_choice) ){
           user_won++;
           cout<<"//////You win/////////\nRule : ";
           if(User_choice == 3 && Computer_choice == 2) cout<<Rule[0]<<"\n";
           else if(User_choice == 2 && Computer_choice == 1) cout<<Rule[1]<<"\n";
           else if(User_choice == 1 && Computer_choice == 4) cout<<Rule[2]<<"\n";
           else if(User_choice == 4 && Computer_choice == 5) cout<<Rule[3]<<"\n";
           else if(User_choice == 5 && Computer_choice == 3) cout<<Rule[4]<<"\n";
           else if(User_choice == 3 && Computer_choice == 4) cout<<Rule[5]<<"\n";
           else if(User_choice == 4 && Computer_choice == 2) cout<<Rule[6]<<"\n";
           else if(User_choice == 2 && Computer_choice == 5) cout<<Rule[7]<<"\n";
           else if(User_choice == 5 && Computer_choice == 1) cout<<Rule[8]<<"\n";
           else if(User_choice == 1 && Computer_choice == 3) cout<<Rule[9]<<"\n";
           cout << "\n";
      
       }
       else if(determineWinner (Computer_choice , User_choice) )computer_won++;
       else {
           cout<<"///////Tie////////\n";
           tie++;
       }
      
       cout<<"\nexit (y/n) : ";
       cin>>exit;
   }while(exit!='y');
   cout<<"Total games : "<<total_game;
   cout<<"\nUser won : "<<user_won;
   cout<<"\nComputer won : "<<computer_won;
   cout<<"\ntie : "<<tie<<"\n";
  
   return 0;
}

//sample output


Related Solutions

Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player...
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneously chooses either a rock or a paper or scissors (usually with an outstretched hand). The rule of the game is simple: rock crushes scissors, scissors cut paper, and paper wraps rock. If both the players choose the same object, then it ends in a tie. Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the...
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You...
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the following rewards each time you play the game:  You get $5 if you win  You get $2 if there is a tie  You get $-1 if you lose The computer randomly chooses rock, paper, or scissors in each game. Rather than deciding what to play (rock, paper or scissors) for each individual game, you decide to use the following...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
Consider now the following two-player simultaneous-move game, called the rock-paper-scissors-lizard game. R stands for rock, P...
Consider now the following two-player simultaneous-move game, called the rock-paper-scissors-lizard game. R stands for rock, P for paper, S for scissors, and L for lizard. R beats S but loses against P and L; P beats R but loses against S and L; S beats P and L but loses against R; L beats R and P but loses against S. The payoffs for winning is 1 and that for losing is -1; when both players choose the same strategy...
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When...
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then prompt for the user’s selection. At that point, the program...
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then seek for the user’s selection (using your choice of an object...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT