Question

In: Computer Science

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 Computer. Neither of which will have any private data, as they will use the parent’s data. Both will contain a virtual function called makeSelection() that will determine and set the selection for the particular player.

Your main() program will contain a NON-member function called playGame() that will take in your two players. You should ask the user if they would like to pay against another human, or a computer, or if they would like to see two computers play.

You will then need to create the appropriate objects and start the game play. After each round, you must display the number of wins and losses for each player and continue playing the game until the user decides to quit.

Solutions

Expert Solution

Hi, here is the solution. Try it out and let me know if you need any changes.

Player.h

/*
* Player.h
*
* Created on: Jul 20, 2020
* Author: Casper
*/

#ifndef PLAYER_H_
#define PLAYER_H_

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class Player
{
private:
   string name;
   char selection;
   int wins;
   int losses;
public:
   Player(string name);
   virtual char makeSelection() = 0;
   int getLosses() const
   {
       return losses;
   }

   void setLosses(int losses)
   {
       this->losses = losses;
   }

   const string& getName() const
   {
       return name;
   }

   void setName(const string &name)
   {
       this->name = name;
   }

   char getSelection() const
   {
       return selection;
   }

   int getWins() const
   {
       return wins;
   }

   void setWins(int wins)
   {
       this->wins = wins;
   }

   void setSelection(char selection)
   {
       this->selection = selection;
   }
   virtual ~Player(){};
   Player& operator++(); // Prefix increment
   Player& operator--(); // Prefix decrement


};

class Human: public Player
{
public:
   char makeSelection();
   Human(string name);
   ~Human()
   {
   }

};
class Computer: public Player
{
public:
   char makeSelection();
   Computer(string name);
   ~Computer()
   {
   }

};
#endif /* PLAYER_H_ */

Player.cpp

/*
* Player.cpp
*
* Created on: Jul 20, 2020
* Author: Casper
*/

#include "Player.h"

Player::Player(string name)
{
   this->name = name;
   this->wins = 0;
   this->losses= 0;
   this->selection = ' ';
}

Player& Player::operator ++()
{
   this->wins++;
   return *this;
}

Player& Player::operator --()
{
   this->losses++;
   return *this;
}

Human::Human(string name):Player(name)
{
}

Computer::Computer(string name):Player(name)
{
}


char Human::makeSelection()
{
   char s;
   bool exit = false;
   while(!exit)
   {
       cout<<"Enter your choice Rock(R)/Paper(P)/Scissors(S): ";
       cin>> s;
       s = toupper(s);
       if( s == 'S'||s == 'R'||s == 'P')
       {
           this->setSelection(toupper(s));
           exit = true;
       }
       else
       {
           cout<<"Not a valid selection."<<endl;
       }
   }
   return this->getSelection();
}

char Computer::makeSelection()
{
   srand(time(0));
   char selections[3] = {'R','P','S'};
   this->setSelection(selections[rand()%3]);
   return this->getSelection();

}

Game.cpp

/*
* Game.cpp
*
* Created on: Jul 20, 2020
* Author: Casper
*/

#include "Player.h"

void playGame(Player & p1, Player & p2)
{
   char s1,s2;
   s1 = p1.makeSelection();
   s2 = p2.makeSelection();
   if(s1 != s2)
   {
       if(s1 == 'R')
       {
           if(s2 == 'P')
           {
               --p1;
               ++p2;
           }
           else
           {
               ++p1;
               --p2;
           }
       }
       else if(s1=='S')
       {
           if(s2 == 'P')
           {
               ++p1;
               --p2;
           }
           else
           {
               --p1;
               ++p2;
           }
       }
       else if(s1=='P')
       {
           if(s2 == 'R')
           {
               ++p1;
               --p2;
           }
           else
           {
               --p1;
               ++p2;
           }
       }
   }
}
int main()
{
   Human * p1;
   Player *p2;
   bool exit = false;
   char choice;
   string name;
   int round = 0;
   cout << "Enter your name: ";
   cin >> name;
   // set p1 as human player
   p1 = new Human(name);
   while(!exit)
   {
       cout << "Play against Human?(Y/N) : ";
       cin >> choice;
       switch(toupper(choice))
       {
       case 'Y':
           cout<<"Enter opponent name: ";
           cin >> name;
           //set p2 as human player
           p2 = new Human(name);
           exit = true;
           break;
       case 'N':
           // set p2 as computer player
           p2 =new Computer("COMPUTER");
           exit = true;
           break;
       default:
           cout<<"Invalid input. Try again!"<<endl;
           break;
       }
   }

   exit = false;
   do
   {
       round++;
       playGame(*p1,*p2);
       cout<<"-----------------------------------------------"<<endl;
       cout<<"Stats after round "<<round<<endl;
       cout<<"-----------------------------------------------"<<endl;
       cout<<"Player 1 - "<<p1->getSelection()<<endl;
       cout<<"Player 2 - "<<p2->getSelection()<<endl;
       cout<<p1->getName()<<" Wins("<<p1->getWins()<<") Losses("<<p1->getLosses()<<")"<<endl;
       cout<<p2->getName()<<" Wins("<<p2->getWins()<<") Losses("<<p2->getLosses()<<")"<<endl;
       cout<<"-----------------------------------------------"<<endl;
       cout<<"Press Q to exit or any key to play next round";
       cin >> choice;
   }while(choice!='q' && choice !='Q');


}


Related Solutions

Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
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...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
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...
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.
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...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Consider now the following two-player simultaneous-move game, called the rock-paper-scissors-lizard game. R stands for rock, P...
Consider now the following two-player simultaneous-move game, called the rock-paper-scissors-lizard game. R stands for rock, P for paper, S for scissors, and L for lizard. R beats S but loses against P and L; P beats R but loses against S and L; S beats P and L but loses against R; L beats R and P but loses against S. The payoffs for winning is 1 and that for losing is -1; when both players choose the same strategy...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT