Question

In: Computer Science

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 strategy:  Play (i) scissors – (ii) rock – (iii) paper sequentially and repeatedly, over and over Example:  Round 1: Play scissors  Round 2: Play rock  Round 3: Play paper  Round 4: Play scissors … Write a program to play the rock-paper-scissors game against the computer for 100 times using the above strategy. You are required to calculate the total reward that you accumulate after playing the game 100 times.

Solutions

Expert Solution

//Since you havn't mentioned in which language program should be
// i have written it in cpp and java
//below is the code and outputs.

//Language cpp
//--------- rockScissors.cpp --------------
#include<iostream>
#include<ctime>
using namespace std;
/*
* Integer equivalents.
* Scissors - 0
* Rock - 1
* Paper - 2
*
*/
//get winner takes two choices of participants.
//and returns 1 winner is user or 0 for computer and -1 for draw
int getWinner(int user, int comp)
{
   if(user == comp)
   {
       return -1;
   }
   else if( (user == 0 && comp == 2 ) || (user == 1 && comp == 0) || (user == 2 && comp == 1))
   {
       return 1;
   }
   else
   {
       return 0;
   }
}
//returns string equivalent for given choice in integer type
string getStringMapping(int choice)
{
   if(choice == 0)
   {
       return "scissors";
   }
   else if(choice == 1 )
   {
       return "rock";
   }
   else if(choice == 2 )
   {
       return "paper";
   }
   else
   {
       return "invalid";
   }
}
int main()
{
   //set seed of random numbers to current time of execution
  
   srand(time(NULL));
   //user and computer wins
   int userWins = 0,compWins = 0,draws = 0;
   int userMoney = 0;
   int userCh = 0 , compCh;
   int res;
  
   for(int i = 0; i < 100; i++)
   {
       //cout<<"\nGame - "<<i<<"\n";
       compCh = rand() % 3;
       //cout<<"\nUser Choice : "<<getStringMapping(userCh)<<endl;
       //cout<<"Computer Choice : "<<getStringMapping(compCh)<<endl;
       res = getWinner(userCh,compCh);
       if(res == 1)
       {
           userMoney += 5;
           userWins ++;
           //cout<<"\nWinner: User"<<endl;;
       }
       else if(res == 0)
       {
           userMoney--;
           compWins++;
           //cout<<"\nWinner: Computer"<<endl;;
       }
       else
       {
           userMoney += 2;
           draws ++;
           //cout<<"\nWinner: Draw"<<endl;;
       }
       //cout<<"User Money : "<<userMoney<<endl;
       userCh = (userCh + 1) % 3;
   }
   cout<<"\nReport: \n\n";
   cout<<"Total Rounds : 100\n";
   cout<<"Rounds Won by user : "<<userWins<<endl;
   cout<<"Rounds Won by Computer : "<<compWins<<endl;
   cout<<"Total Draws : "<<draws<<endl;
   cout<<"Final Money of User : "<<userMoney<<endl;
   cout<<"\nExiting...";
   return 0;
}

//-------------- OUTPUT -------------

//Language : java
//--------- RockScissors.java --------------

/*
* Integer equivalents.
* Scissors - 0
* Rock - 1
* Paper - 2
*
*/
import java.util.Random;
class RockScissors
{  
   //get winner takes two choices of participants.
   //and returns 1 winner is user or 0 for computer and -1 for draw
   public static int getWinner(int user, int comp)
   {
       if(user == comp)
       {
           return -1;
       }
       else if( (user == 0 && comp == 2 ) || (user == 1 && comp == 0) || (user == 2 && comp == 1))
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }
   //returns string equivalent for given choice in integer type
   public static String getStringMapping(int choice)
   {
       if(choice == 0)
       {
           return "scissors";
       }
       else if(choice == 1 )
       {
           return "rock";
       }
       else if(choice == 2 )
       {
           return "paper";
       }
       else
       {
           return "invalid";
       }
   }
   public static void main(String[] args)
   {
       //set seed of random numbers to current time of execution
      
       Random rand = new Random();
       //user and computer wins
       int userWins = 0,compWins = 0,draws = 0;
       int userMoney = 0;
       int userCh = 0 , compCh;
       int res;
      
       for(int i = 0; i < 100; i++)
       {
           //System.out.println("\nGame - "+i+"\n";
           compCh = rand.nextInt(3);
           //System.out.println("\nUser Choice : "+getStringMapping(userCh));
           //System.out.println("Computer Choice : "+getStringMapping(compCh));
           res = getWinner(userCh,compCh);
           if(res == 1)
           {
               userMoney += 5;
               userWins ++;
               //System.out.println("\nWinner: User");;
           }
           else if(res == 0)
           {
               userMoney--;
               compWins++;
               //System.out.println("\nWinner: Computer");;
           }
           else
           {
               userMoney += 2;
               draws ++;
               //System.out.println("\nWinner: Draw");;
           }
           //System.out.println("User Money : "+userMoney);
           userCh = (userCh + 1) % 3;
       }
       System.out.println("\nReport: \n");
       System.out.println("Total Rounds : 100");
       System.out.println("Rounds Won by user : "+userWins);
       System.out.println("Rounds Won by Computer : "+compWins);
       System.out.println("Total Draws : "+draws);
       System.out.println("Final Money of User : "+userMoney);
       System.out.println("\nExiting...");
      
   }
}


Related Solutions

Suppose you play rock paper scissors against a computer: *you gain $1 for each win *you...
Suppose you play rock paper scissors against a computer: *you gain $1 for each win *you lose $1 for each lost if it is a tie, nothing happens you choose rock 50% of the time and the others 25% of the time let x be a random variable that represents the amount of money you earn after one game. (a) find the probability distribution of X (b) what is your average payoff after 20 games? (c) what is the standard...
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...
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...
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...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
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.
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's 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. 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, then the computer has chosen...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT