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.
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
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.
Rock, Paper, Scissors is a two-player game in which each player chooses one of three items....
Rock, Paper, Scissors is a two-player game in which each player chooses one of three items. If both players choose the same item, the game is tied. Otherwise, the rules that determine the winner are: (a) Rock always beats Scissors (Rock crushes Scissors) (b) Scissors always beats Paper (Scissors cut Paper) (c) Paper always beats Rock (Paper covers Rock) Implement function rps() that takes the choice ('R', 'P', or 'S') of player 1 and the choice of player 2, and...
CSIS 113A C++ Programming Level1 Rock, Paper, Scissors Introduction In this assignment you will create a...
CSIS 113A C++ Programming Level1 Rock, Paper, Scissors Introduction In this assignment you will create a program that will simulate the popular children’s game “Rock, Paper, Scissors”; using only Boolean logic and practice using the for-loop and logical operators as well as continue getting used to data of various types. Skills: string input/output, data types, logical operators, and control structures Rock-Paper-Scissors Rock-Paper-Scissors is a game where two players, on the count of 3, show the rock/paper/scissors symbol with their hand....
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 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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT