Question

In: Computer Science

ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...

ROCK, PAPER, SCISSORS

Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool.
Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and a non-default constructor which will take in an int used to initialize the strength field. The constructor should also initialize the type field using 'r' for Rock, 'p' for Paper, and 's' for Scissors.
These classes will also need a public function bool fight(Tool) that compares their strengths in the following way:
 Rock's strength is doubled (temporarily) when fighting scissors, but halved (temporarily) when fighting paper.
 In the same way, paper has the advantage against rock, and scissors against paper.
 The strength field shouldn't change in the function, which returns true if the original class wins in strength and false otherwise.
**A design choice that returns a type other than a bool for a win, loss, or tie will not be penalized**
You may also include any extra auxiliary functions and/or fields in any of these classes.
In addition, you will create a class called RPSGame, which allows a human to play the rock, paper, scissors game against the computer. Your RPSGame must have two Tool * for the human and computer tool because you don’t know the new tool they’ll select with each round. The RPSGame should also have three int fields to keep track of the number of human_wins, computer_wins, and ties.
You can choose the strategy for the computer guesses, but it cannot be based on what the human selected for a tool in the current game!!! Example of a novice and veteran computer AI: http://www.nytimes.com/interactive/science/rock-paper-scissors.html?_r=0
After the human selects the tool for the current game, display the computer’s tool, a message describing who won, the current stats for the wins and ties, and then ask the user if he/she wants to play again.
Example Play of Game:
Welcome to Rock, Paper, Scissors! Do you want to choose different strengths for the tools? (y-yes, n-no) n
Choose your tool (r-rock, p-paper, s-scissor, e-exit): r
Computer chose scissor.
You win!!!
Human wins: 1
Computer wins: 0
Ties: 0
Choose your tool (r-rock, p-paper, s-scissor, e-exit): r
Computer chose paper.
Computer wins! :-(
Human wins: 1
Computer wins: 1
Ties: 0
Choose your tool (r-rock, p-paper, s-scissor, e-exit): e
Things to consider:
If you choose to set different strengths for the tools, then you need to prompt the user for his/her specific strength for their tool, and you will need to select a specific strength for the AI choice.
**If you selected one non-default strength for both, then you will not be penalized**
You must have the proper constructors, destructors, and assignment operator overload for the Tool, Rock, Paper, Scissor, and RPSGame classes.
Your member variables in all classes must be private or protected for encapsulation rules.
You must have your class definitions in a .h file and your implemented classes in .cpp files.
You must also have your main function in a play_game.cpp file, separated from the class implementations.​

Solutions

Expert Solution

RPSGame.h


class Tool{

protected:
int strength;
char type;

public:
void setStrength(int s);
int getStrength();
char getType();
virtual bool fight(Tool*);
};

class Rock:public Tool{
public:
   Rock();
   Rock(int s);

   virtual bool fight(Tool *);
};

class Paper:public Tool{
public:
   Paper();
   Paper(int s);

   virtual bool fight(Tool *);
};
class Scissors:public Tool{
public:
   Scissors();
   Scissors(int s);

   virtual bool fight(Tool *);
};

RPSGame.cpp:

#include<iostream>
using namespace std;

#include "RPSGame.h"

void Tool::setStrength(int s){ strength=s;}

int Tool::getStrength(){ return strength;}

char Tool::getType(){ return type;}

bool Tool::fight(Tool *t){

return true;}


Rock::Rock(){
strength=1;
type='r';
}

Rock::Rock(int s){
strength=s;
type='r';
}

Paper::Paper(){
strength=1;
type='p';
}

Paper::Paper(int s){
strength=s;
type='p';
}

Scissors::Scissors(){
strength=1;
type='s';
}

Scissors::Scissors(int s){
strength=1;
type='s';
}

bool Rock::fight(Tool *t){
char type=t->getType();
if(type=='s')
{
   strength*=2;
   return true;
}

return false;
}

bool Paper::fight(Tool *t){
char type=t->getType();
if(type=='r')
{
   strength*=2;
   return true;
}

return false;
}

bool Scissors::fight(Tool *t){
char type=t->getType();
if(type=='p')
{
   strength*=2;
   return true;
}

return false;
}

play_game.cpp

#include<iostream>
using namespace std;

#include<stdlib.h>

#include "RPSGame.h"

int main(void)
{

Tool * tool=new Tool[3];
tool[0]=Rock();
tool[1]=Paper();
tool[1]=Scissors();

char tl[3][8]={"Rock", "Paper", "Scissor"};

char o;

cout<<"Welcome to Rock, Paper, Scissors! Do you want to choose different strengths for the tools? (y-yes, n-no) n"<<endl;
cin>>o;

if(o=='y'|| o=='Y')
{
   cout<<"Enter strength for rock,paper,scissors successively.."<<endl;
   int t1,t2,t3;
   cin>>t1>>t2>>t3;
   tool[0].setStrength(t1);
   tool[1].setStrength(t2);
   tool[2].setStrength(t3);
}

srand(25678);
int human_wins=0,computer_wins=0,ties=0;

while(1){
cout<<"Chose your tool(r-rock, p-paper, s-scissor, e-exit):"<<endl;
cin>>o;

int t;

int i=rand()%3;


switch(o){
   case 'r':
      
       t=0;
       break;
   case 'p':
       t=1;
       break;
   case 's':
       t=2;
       break;
   case 'e':
       delete [] tool;
       return 0;
   }

    cout<<"Computer Chosed "<<tl[i]<<endl;
   if((&tool[t])->fight(&tool[i]))
   {
       cout<<"Human Wins: :)"<<endl;
       human_wins++;
   }
   else
   {
       cout<<"Computer wins: :("<<endl;
       computer_wins++;
   }

   cout<<"Human wins:"<<human_wins<<endl;
   cout<<"Comuter wins:"<<computer_wins<<endl;
   cout<<"Ties:"<<ties<<endl;
  
      
}
return 0;
}

Output:

Welcome to Rock, Paper, Scissors! Do you want to choose different strengths for the tools? (y-yes, n-no) n
n
Chose your tool(r-rock, p-paper, s-scissor, e-exit):
r
Computer Chosed Rock
Human Wins: :)
Human wins:1
Comuter wins:0
Ties:0
Chose your tool(r-rock, p-paper, s-scissor, e-exit):
s
Computer Chosed Rock
Human Wins: :)
Human wins:2
Comuter wins:0
Ties:0
Chose your tool(r-rock, p-paper, s-scissor, e-exit):
p
Computer Chosed Scissor
Human Wins: :)
Human wins:3
Comuter wins:0
Ties:0
Chose your tool(r-rock, p-paper, s-scissor, e-exit):
e


Related Solutions

Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming 1) Develop a function that prompts the user to enter their choice (1=Rock 2=Paper 3=Scissors) Return either a 1, 2, or 3 depending on the value the user has entered Do not continue the program until the user has entered a valid choice of 1, 2, 3 2) Develop a function that generates the computer player's choice Return either a 1, 2, or 3...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
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...
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...
1. Have you ever played rock-paper-scissors (or Rochambeau)? It’s considered a “fair game” in that the...
1. Have you ever played rock-paper-scissors (or Rochambeau)? It’s considered a “fair game” in that the two players are equally likely to win (like a coin toss). Both players simultaneously display one of three hand gestures (rock, paper, or scissors), and the objective is to display a gesture that defeats that of your opponent. The main gist is that rocks break scissors, scissors cut paper, and paper covers rock. We investigated some results of the game rock-paper-scissors, where the researchers...
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...
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...
You are playing rock, paper, scissors (RPS) with a friend. Because you are good at predicting...
You are playing rock, paper, scissors (RPS) with a friend. Because you are good at predicting your friend’s strategy, there is a 60% chance each time that you play her, that you win. You play 7 games of rock, paper scissors with your friend and would like to know how many of them you win. Use this information to answer the following questions. 1. What is the random variable in this example? a. X = number of times your friend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT