Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ Inheritance programming:Rock,paper,Scissors You will implement a class calle

ID: 657408 • Letter: C

Question

c++ Inheritance programming:Rock,paper,Scissors

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. 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. 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: 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. Create a Makefile for you project.

Explanation / Answer

//Header File
#ifndef __GAME_H__
#define __GAME_H__
#include <iostream>
#include <stdlib.h>
using namespace std;
class Tool
{
    int strength;
    char type;

    public:
        void setStrength(int st);
        int getStrength();
        char getType();
        void setType(char ch);
        void operator=(Tool right);
        virtual bool fight(Tool*){};
};
class Rock: public Tool
{
public:
    Rock();
    Rock(int st);
    bool fight(Tool opponent);
};
class Paper: public Tool
{
public:
    Paper();
  
    Paper(int st);
    bool fight(Tool opponent);
};
class Scissors: public Tool
{
public:
    Scissors();
    Scissors(int st);
    bool fight(Tool opponent);
};
class RSPGame
{
    int human_wins,computer_wins,ties;
    Tool computer,human;
public:
    RSPGame();
    void playOneGame(Tool*,Tool*);
    void printScores();
};

#endif

//CPP File
#include "Game.h"
void Tool::setStrength(int st)
{
    strength=st;
}
void Tool::setType(char ch)
{
    type=ch;
}
int Tool::getStrength()
{
   return strength;
}
char Tool::getType()
{
   return type;
}
void Tool::operator=(Tool right)
{
    strength=right.strength;
    type=right.type;
}
Rock::Rock()
{
    setStrength(1);
    setType('r');
}
Rock::Rock(int st)
{
    setStrength(st);
    setType('r');
}
bool Rock::fight(Tool opponent)
{
   cout<<opponent.getType()<<" Rocks ";
    if(opponent.getType()=='s')
        return 2*this->getStrength()>opponent.getStrength();
    if(opponent.getType()=='p')
        return 0.5*this->getStrength()>opponent.getStrength();
}
Paper::Paper()
{
    setStrength(1);
    setType('p');
}
Paper::Paper(int st)
{
    setStrength(st);
    setType('p');
}
bool Paper::fight(Tool opponent)
{
   cout<<opponent.getType()<<" Paper ";

   if(opponent.getType()=='r')
        return 2*this->getStrength()>opponent.getStrength();
    if(opponent.getType()=='s')
        return 0.5*this->getStrength()>opponent.getStrength();
}
Scissors::Scissors()
{
    setStrength(1);
    setType('s');
}
Scissors::Scissors(int st)
{
    setStrength(st);
    setType('s');
}
bool Scissors::fight(Tool opponent)
{
   cout<<opponent.getType()<<" Scissors ";
    if(opponent.getType()=='p')
        return 2*this->getStrength()>opponent.getStrength();
    if(opponent.getType()=='r')
        return 0.5*this->getStrength()>opponent.getStrength();
}
RSPGame::RSPGame()
{
    human_wins=0;
    computer_wins=0;
    ties=0;
}
void RSPGame::playOneGame(Tool *human,Tool *computer)
{
    if(human->getType()==computer->getType())
    {
        ties++;
    }
    else
    {
        if(human->fight(computer))
        {
            human_wins++;
            cout<<"You Win!!! ";
        }
        else
        {
            cout<<"Computer wins! :-( ";
            computer_wins++;
        }
    }
    printScores();
}
void RSPGame::printScores()
{
    cout<<"Human wins: "<<human_wins<<endl;
    cout<<"Computer wins: "<<computer_wins<<endl;
    cout<<"Ties: "<<ties<<endl;
}

//Main File

#include "Game.h"
int main()
{
    cout<<"Welcome to Rock, Paper, Scissors! Do you want to choose different stregngths for the tools? (y-yes, n-no) :";
    char ch;
    cin>>ch;
    int rockStrength=1,paperStrength=1,scissorsStrength=1;
    if(ch=='y')
    {
        cout<<"Enter strength of Rock :";
        cin>>rockStrength;
        cout<<"Enter strength of Paper :";
        cin>>paperStrength;
        cout<<"Enter strength of Scissors :";
        cin>>scissorsStrength;
    }
    Tool *computer;
    Tool *human;
    Rock temp4,temp1;
    Scissors temp3,temp6;
    Paper temp5,temp2;
    RSPGame game;
    while(1)
    {
        cout<<"Choose your tool(r-rock, p-paper,s-scissors,e-exit):";
        cin>>ch;
        if(ch=='e')
            break;
        int computerChoice=rand()%3;
        if(computerChoice==0)
        {
            cout<<"Computer chose rock ";
            temp4=Rock(rockStrength);
            computer=&temp4;
        }
        if(computerChoice==1)
        {
            cout<<"Computer chose Paper ";
            temp5=Paper(paperStrength);
            computer=&temp5;
        }
        if(computerChoice==2)
        {
            cout<<"Computer chose Scissors ";
            temp6=Scissors(scissorsStrength);
            computer=&temp6;
        }
        if(ch=='r')
        {
            temp1=Rock(rockStrength);
            human=&temp1;
        }
        if(ch=='p')
        {
            temp2=Paper(paperStrength);
            human=&temp2;
        }
        if(ch=='s')
        {
            temp3=Scissors(scissorsStrength);
            human=&temp3;
        }
        game.playOneGame(human,computer);
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote