In: Computer Science
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.
//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...");
}
}