c++ programming (inheritance) NO SPAM PLZ two players compete by choosing Rock,
ID: 3804701 • Letter: C
Question
c++ programming (inheritance) NO SPAM PLZ
two players compete by choosing Rock, Paper or Scissors and the winner of the game is determined by comparing the choices of the players. Rock beats Scissors, Scissors beats Paper, Paper beats Rock.
You are asked to modify these classes with the names provided in the following individual stages.
1.Computer.cpp computer always chooses Rock(the base class for all computer players)
• New computer players: Create the following computer players making use of inher-itance
2.RandomComputer.cpp: RandomComputer returns a random move.
3.Avalanche.cpp: Avalanche always chooses Rock.
4.Bureaucrat.cpp: Bureaucrat always chooses Paper.
5Toolbox.cpp: Toolbox always chooses Scissors.
6Crescendo.cpp: Crescendo moves in the following order: Paper, Scissors, Rock.
7PaperDoll.cpp: Paper doll moves in the following order: Paper, Scissors, Scissors.
8FistfullODollars.cpp:FistfullODollars moves in the following order: Rock, Paper,
Paper.
• 9.referee.cpp: your referee class should be able to match any two players together.
• 10.Tournament.cpp: Create a class called Tournament. In Tournament.cpp there should be a driver function that sets eight players against each other. The bracket follows the illustration as follows
11 human.cpp input the 8 players from the keyborad
In every round, the winner is determined after ve plays (draws count as plays). That is, in every round, two players play against each other ve times. The one who wins more advances. If both win the same number of plays, then the player with a lower index advances. (e.g. if Player2 and Player4 reach a tie after 5 plays, then Player2 advances.) Also, players ”refresh their memories” between rounds. That is, whenever Crescendo enters a round, it plays paper rst.
Your main.cpp le should have access to all your classes. The input consists of one line: a list of computer player names (from the above list, excluding RandomCom-puter), divided by space, where the i-th name represents Playeri in the bracket. The output is the name of the grand winner.
Sample input:
Avalanche Bureaucrat Bureaucrat Toolbox Toolbox Crescendo Crescendo Fistful-lODollars
Sample output: Toolbox
Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "Player.h"
#include "Toolbox.h"
#include "Bureaucrat.h"
#include "Avalanche.h"
#include "Crescendo.h"
#include "PaperDoll.h"
#include "FistfullODollars.h"
#include "Tournament.h"
#include "Referee.h"
#include "AdvancedComputer.h"
#include "Human.h"
using namespace std;
int main(int argc, char* argv[])
{
string computers;
getline(cin, computers); // Getting the string of compuets the user wants to compete in the tournament
istringstream computerStream(computers); // Turning the string into a stringstream
Tournament newTourn;
do
{
string word;
computerStream >> word; // Storing each word from the stream in the string 'word'
// The following block of code deals with adding Players to the tournament according to the users specification
if(word == "Avalanche")
{
Player * newLanche = new Avalanche();
newTourn.addPlayers(newLanche);
}
if(word == "Bureaucrat")
{
Player * newCrat = new Bureaucrat();
newTourn.addPlayers(newCrat);
}
if(word == "Toolbox")
{
Player * newBox = new Toolbox();
newTourn.addPlayers(newBox);
}
if(word == "Crescendo")
{
Player * newCres = new Crescendo();
newTourn.addPlayers(newCres);
}
if(word == "PaperDoll")
{
Player * newDoll = new PaperDoll();
newTourn.addPlayers(newDoll);
}
if(word == "FistfullODollars")
{
Player * newFist = new FistfullODollars();
newTourn.addPlayers(newFist);
}
if(word == "AdvancedComputer")
{
Player * newAdv = new AdvancedComputer();
newTourn.addPlayers(newAdv);
}
} while (computerStream); // While not reached the end of stringstream
cout << newTourn.compete();
}
AdvancedComputer.cpp
#include "AdvancedComputer.h"
#include "Player.h"
#include <string>
#include <iostream> //remove this
#include <cstdlib>
using namespace std;
AdvancedComputer::AdvancedComputer() : Player("AdvancedComputer"){
}
AdvancedComputer::~AdvancedComputer(){
}
void AdvancedComputer::addAnswer(string ans)
{
userAnswers.push_back(ans);
}
string AdvancedComputer::performMove()
{
if(userAnswers.size() > 0) // Checking the back of the vector (which contains its opponents answers) and then chosing the latest answer
{
if(userAnswers.back() == "Rock")
{
return "Rock";
}
else if(userAnswers.back() == "Paper")
{
return "Paper";
}
else if(userAnswers.back() == "Scissors")
{
return "Scissors";
}
}
else
{
int handInt = rand() % 2; // Randomising the first input because the AdvancedComputer cannot play a previous move if there is none
if (handInt == 0)
{
return "Rock";
}
else if(handInt == 1)
{
return "Paper";
}
else if(handInt == 2)
{
return "Scissors";
}
}
return 0; // Remove Warning Flag
}
AdvancedComputer.h
#ifndef AdvancedComputer_H
#define AdvancedComputer_H
#include <string>
#include "Player.h"
#include <vector>
class AdvancedComputer : public Player {
public:
AdvancedComputer();
std::string performMove();
void addAnswer(std::string ans);
~AdvancedComputer();
private:
int handInt;
std::vector<std::string> userAnswers;
};
#endif
Avalanche.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "Avalanche.h"
using namespace std;
Avalanche::Avalanche() : Player("Avalanche") {}
Avalanche::~Avalanche() {}
string Avalanche::performMove()
{
hand = "Rock"; // Always picks rock
return hand;
}
Avalanche.h
#ifndef Avalanche_H
#define Avalanche_H
#include "Player.h"
#include <string>
class Avalanche : public Player {
public:
Avalanche();
std::string performMove();
~Avalanche();
private:
std::string hand;
};
#endif
Bureaucrat.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "Bureaucrat.h"
using namespace std;
Bureaucrat::Bureaucrat() : Player("Bureaucrat") {}
Bureaucrat::~Bureaucrat() {}
string Bureaucrat::performMove()
{
hand = "Paper"; // Always picks paper
return hand;
}
Bureaucrat.h
#ifndef Bureaucrat_H
#define Bureaucrat_H
#include <string>
#include "Player.h"
class Bureaucrat : public Player {
public:
Bureaucrat();
std::string performMove();
~Bureaucrat();
private:
std::string hand;
};
#endif
Crescendo.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "Crescendo.h"
using namespace std;
Crescendo::Crescendo() : Player("Crescendo") {
ctr = 0;
}
Crescendo::~Crescendo() {}
void Crescendo::setCtr(int newCtr)
{
ctr = newCtr; // This ctr will be reset so the AI is 'reset' each round
}
string Crescendo::performMove()
{
if (ctr == 0)
{
return "Paper";
}
else if (ctr == 1)
{
return "Scissors";
}
else if (ctr == 2)
{
return "Rock";
}
return 0; // Remove Warning Flag
}
Crescendo.h
#ifndef Crescendo_H
#define Crescendo_H
#include <string>
#include "Player.h"
class Crescendo : public Player {
public:
Crescendo();
std::string performMove();
void setCtr(int);
~Crescendo();
private:
std::string hand;
int ctr;
};
#endif
FistfullODollars.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "FistfullODollars.h"
using namespace std;
FistfullODollars::FistfullODollars() : Player("FistfullODollars") {
ctr = 0;
}
FistfullODollars::~FistfullODollars() {}
void FistfullODollars::setCtr(int newCtr)
{
ctr = newCtr; // This ctr will be reset so the AI is 'reset' each round
}
string FistfullODollars::performMove()
{
if (ctr == 0)
{
return "Rock";
}
else if (ctr == 1)
{
return "Paper";
}
else if (ctr == 2)
{
return "Paper";
}
return 0; // Remove Warning Flag
}
FistfullODollars.h
#ifndef FistfullODollars_H
#define FistfullODollars_H
#include <string>
#include "Player.h"
class FistfullODollars : public Player {
public:
FistfullODollars();
std::string performMove();
void setCtr(int);
~FistfullODollars();
private:
std::string hand;
int ctr;
};
#endif
Human.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "Human.h"
using namespace std;
Human::Human() : Player("Avalanche") {}
Human::~Human() {}
string Human::performMove()
{
string userHand;
cin >> userHand;
return userHand;
}
Human.h
#ifndef Human_H
#define Human_H
#include <string>
#include "Player.h"
#include <vector>
class Human : public Player {
public:
Human();
std::string performMove();
~Human();
private:
};
#endif
PaperDoll.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "PaperDoll.h"
using namespace std;
PaperDoll::PaperDoll() : Player("PaperDoll") {
ctr = 0;
}
PaperDoll::~PaperDoll() {}
void PaperDoll::setCtr(int newCtr)
{
ctr = newCtr;
}
string PaperDoll::performMove()
{
if (ctr == 0)
{
return "Paper";
}
else if (ctr == 1)
{
return "Scissors";
}
else if (ctr == 2)
{
return "Scissors";
}
return 0; // Remove Warning Flag
}
PaperDoll.h
#ifndef PaperDoll_H
#define PaperDoll_H
#include <string>
#include "Player.h"
class PaperDoll : public Player {
public:
PaperDoll();
std::string performMove();
void setCtr(int);
~PaperDoll();
private:
std::string hand;
int ctr;
};
#endif
Player.cpp
#include <string>
#include <iostream>
#include "Player.h"
using namespace std;
Player::Player(){
}
Player::Player(string name2)
{
name = name2;
}
Player::~Player(){
}
string Player::getName()
{
return name;
}
void Player::setCtr(int ctr)
{
}
void Player::addAnswer(string ans)
{
}
string Player::performMove()
{
return "";
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
class Player {
public:
Player();
Player(std::string);
std::string getName();
virtual std::string performMove();
virtual void setCtr(int); // Used only in crescendo fisftfull and paperdoll
virtual void addAnswer(std::string); // used only in AdvancedComputer
~Player();
private:
std::string name;
};
#endif
Referee.cpp
#include <string>
#include <vector>
#include <iostream>
#include <cstring>
#include "Player.h"
#include "Referee.h"
#include "AdvancedComputer.h"
using namespace std;
Referee::Referee() {
}
Referee::~Referee() {
}
string Referee::Fight(Player *p1, Player *p2)
{
string p1move, p2move, winner;
bool isval;
p1move = p1->performMove(); // Perform p1's move
p2move = p2->performMove(); // Perform p2's move
// If either p1 or p2 are advanced computers they need to store the others answer
if(p1->getName() == "AdvancedComputer")
{
p1->addAnswer(p2move);
}
if(p2->getName() == "AdvancedComputer")
{
p2->addAnswer(p1move);
}
isval = checkValid(p1move);
if(!isval)
{
return "INVALID"; // If either move is invalid return invalid
}
isval = checkValid(p2move);
if(!isval)
{
return "INVALID"; // If either move is invalid return invalid
}
winner = checkWin(p1move, p2move);
return winner;
}
bool Referee::checkValid(string hand)
{
if ((hand=="Scissors") or (hand=="Rock") or (hand=="Paper")) // All valid moves
{
return 1;
}
return 0;
}
string Referee::checkWin(string p1move, string p2move)
{
// These if statements are just the implementation of the rules of rock paper scissors
if(p1move == "Rock")
{
if( p2move == "Paper" )
{
return "p2";
}
else if(p2move == "Rock")
{
return "Draw";
}
else if(p2move == "Scissors")
{
return "p1";
}
}
else if(p1move == "Paper")
{
if( p2move == "Paper" )
{
return "Draw";
}
else if(p2move == "Rock")
{
return "p1";
}
else if(p2move == "Scissors")
{
return "p2";
}
}
else if(p1move == "Scissors")
{
if( p2move == "Paper" )
{
return "p1";
}
else if(p2move == "Rock")
{
return "p2";
}
else if(p2move == "Scissors")
{
return "Draw";
}
}
return 0; // Remove Warning Flag
}
Referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include <string>
#include "Player.h"
class Referee {
public:
Referee();
~Referee();
std::string getMove(std::string);
std::string Fight(Player *p1, Player *p2);
private:
std::string checkWin(std::string, std::string);
bool checkValid(std::string);
};
#endif
Toolbox.cpp
#include <string>
#include <iostream>
#include "Player.h"
#include "Toolbox.h"
using namespace std;
Toolbox::Toolbox() : Player("Toolbox") {}
Toolbox::~Toolbox() {}
string Toolbox::performMove()
{
hand = "Scissors"; // Always picks scissors
return hand;
}
Toolbox.h
#ifndef TOOLBOX_H
#define TOOLBOX_H
#include <string>
#include "Player.h"
class Toolbox : public Player {
public:
Toolbox();
std::string performMove();
~Toolbox();
private:
std::string hand;
};
#endif
Tournament.cpp
#include <string>
#include <vector>
#include <iostream>
#include "Player.h"
#include "Tournament.h"
#include "Referee.h"
using namespace std;
Tournament::Tournament() {
}
Tournament::~Tournament() {
}
string Tournament::compete() {
int i =0; // ITERATOR VARIABLE
int j; // ITERATOR VARIABLE
unsigned int k; // ITERATOR VARIABLE
string fight_winner; // STORES THE RUNNING WINNER
int p1wins = 0; // STORES P1 WINS
int p2wins = 0; // STORES P2 WINS
Referee newRef; // CREATING A REF FOR THE TOURNAMENT
for(k=0; k<Round1.size()/2; k++) // there will always be an amount of fights equal to half the members in the round vector
{
for(j = 0; j < 5; j++)
{
fight_winner = newRef.Fight(Round1.at(i), Round1.at(i+1)); // Finding the fight winners and storing in the string
if(fight_winner == "p1")
{
p1wins++; // Iterating p1 wins
}
else if(fight_winner == "p2")
{
p2wins++; // Iterating p2wins
}
}
// Following block of code is pushing back the winners into the 'Round2 Vector'
if(p1wins > p2wins)
{
Round2.push_back(Round1.at(i));
}
else if(p2wins > p1wins)
{
Round2.push_back(Round1.at(i+1));
}
else if(p2wins == p1wins)
{
Round2.push_back(Round1.at(i));
}
// Iterating i by two because each game is played 0 vs. 1, 2 vs. 3 NOT 0 vs. 1, 1 vs. 2
// Then resetting the variables that store wins
p1wins = 0;
p2wins = 0;
i+=2;
}
i = 0; // Reset i to 0
//RESETTING PLAYERS
// This is important because players like crescendo and fistfullodollars need to be reset between rounds
for(k=0; k<Round2.size(); k++)
{
Round2.at(k)->setCtr(0);
}
//
for(k=0;k<Round2.size()/2;k++) // There will always be an amount of fights equal to half the members in the round vector
{
for(j = 0; j < 5; j++) // Playing 5 fights
{
fight_winner = newRef.Fight(Round2.at(i), Round2.at(i+1)); // Finding the fight winners and storing in the string
if(fight_winner == "p1")
{
p1wins++; // Iterating p1 wins
}
else if(fight_winner == "p2")
{
p2wins++; // Iterating p2wins
}
}
// Following block of code is pushing back the winners into the 'Round2 Vector'
if(p1wins > p2wins)
{
Round3.push_back(Round2.at(i));
}
else if(p2wins > p1wins)
{
Round3.push_back(Round2.at(i+1));
}
else if(p2wins == p1wins)
{
Round3.push_back(Round2.at(i));
}
// Iterating i by two because each game is played 0 vs. 1, 2 vs. 3 NOT 0 vs. 1, 1 vs. 2
// Then resetting the variables that store wins
i+=2;
p1wins = 0;
p2wins = 0;
}
//RESETTING PLAYERS
// This is important because players like crescendo and fistfullodollars need to be reset between rounds
for(k=0; k<Round3.size(); k++)
{
Round3.at(k)->setCtr(0);
}
//
//ROUND 3
for(j = 0; j < 5; j++) // Playing 5 fights
{
fight_winner = newRef.Fight(Round3.at(0), Round3.at(1)); // Finding the fight winners and storing in the string
if(fight_winner == "p1")
{
p1wins++; // Iterating p1 wins
}
else if(fight_winner == "p2")
{
p2wins++; // Iterating p2wins
}
}
// Checking who won and returning the winner
if(p1wins > p2wins)
{
return Round3.at(0)->getName();
}
else if(p2wins > p1wins)
{
return Round3.at(1)->getName();
}
else if(p2wins == p1wins)
{
return Round3.at(0)->getName();
}
return "INVALID WINNER"; // Satisfying warning command (control reaches end of non-void function)
}
void Tournament::addPlayers(Player* newPlayer)
{
Round1.push_back(newPlayer); // Pushing back new players into the Round1 vector
}
Tournament.h
#ifndef TOURNAMENT_H
#define TOURNAMENT_H
#include <string>
#include <vector>
#include "Player.h"
class Tournament {
public:
Tournament();
void addPlayers(Player*);
std::string compete();
~Tournament();
private:
std::vector<Player*> Round1;
std::vector<Player*> Round2;
std::vector<Player*> Round3;
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.