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

Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The...
Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The user inputs the number of rounds which should be positive and odd. This is a free form assignment but structure your code similar to the test cases provided. USING MATLAB ONLY!!! Program Inputs • How many rounds would you like to play (odd rounds only)?: XXX – XXX should be positive and odd. Restart the game if the input is not positive or odd....
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...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
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...
Create a rock paper scissors game against the computer using bunch of methods. -No two dimensional...
Create a rock paper scissors game against the computer using bunch of methods. -No two dimensional arrays -No java.util.random -No ragged arrays -Methods only, and one dimensional arrays
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT