Below you will find my first attempt at a C++ implementation of the classic game Rock-paper-scissors. If someone could look it over and point out or suggest any things that I could change to improve it, that would be brilliant and I would greatly appreciate it.
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include <memory>
#include <random>
class Player
{
private:
std::string name;
std::string choice;
public:
Player(std::string Name);
void setName(std::string Name);
std::string getName() const;
void setChoice(std::string c);
std::string makeChoice();
std::string getChoice() const;
};
#endif
Player.cpp
#include "Player.h"
Player::Player(std::string Name)
{
name=Name;
choice=" ";
}
void Player::setName(std::string Name)
{
name=Name;
}
std::string Player::getName() const
{
return name;
}
std::string Player::makeChoice()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 3);
int playerChoice=dis(gen);
switch(playerChoice)
{
case 1:
choice="rock";
break;
case 2:
choice="paper";
break;
case 3:
choice="scissors";
break;
default:
std::cout << "Please make a valid choice" << std::endl;
break;
}
return choice;
}
void Player::setChoice(std::string c)
{
choice=c;
}
std::string Player::getChoice() const
{
return choice;
}
Main.cpp
#include "Player.cpp"
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
using namespace std;
void determineWinner(unique_ptr<Player> const & p1, unique_ptr<Player> const & p2)
{
if(p1->getChoice()=="rock" && p2->getChoice()=="scissors")
{
cout << p1->getName() << " wins!" << endl;
}
else if(p1->getChoice()=="scissors" && p2->getChoice()=="rock")
{
cout << p2->getName() << " wins!" << endl;
}
else if(p1->getChoice()=="paper" && p2->getChoice()=="rock")
{
cout << p1->getName() << " wins!" << endl;
}
else if(p1->getChoice()=="rock" && p2->getChoice()=="paper")
{
cout << p2->getName() << " wins!" << endl;
}
else if(p1->getChoice()=="scissors" && p2->getChoice()=="paper")
{
cout << p1->getName() << " wins!" << endl;
}
else if(p1->getChoice()=="paper" && p2->getChoice()=="scissors")
{
cout << p2->getName() << " wins!" << endl;
}
else
{
cout << "It's a tie!" << endl;
}
}
void playRockPaperScissors2(unique_ptr<Player> const & p1, unique_ptr<Player> const & p2)
{
char player1Choice;
char player2Choice;
cout << "Player 1, please make your choice:" << endl;
cout << "Press (r) for rock, (p) for paper, or (s) for scissors" << endl;
cin >> player1Choice;
switch(player1Choice)
{
case 'r':
p1->setChoice("rock");
break;
case 'p':
p1->setChoice("paper");
break;
case 's':
p1->setChoice("scissors");
break;
default:
cout << "Please make a valid choice" << endl;
break;
}
cout << "Player 2, please make your choice:" << endl;
cout << "Press (r) for rock, (p) for paper, or (s) for scissors" << endl;
cin >> player2Choice;
switch(player2Choice)
{
case 'r':
p2->setChoice("rock");
break;
case 'p':
p2->setChoice("paper");
break;
case 's':
p2->setChoice("scissors");
break;
default:
cout << "Please make a valid choice" << endl;
break;
}
cout << "Ready, set" << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "rock..." << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "paper..." << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "scissors..." << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "shoot..." << endl;
cout << p1->getName() << ": " << p1->getChoice() << endl;
cout << p2->getName() << ": " << p2->getChoice() << endl;
determineWinner(p1,p2);
}
void playRockPaperScissors(unique_ptr<Player> const & p1, unique_ptr<Player> const & p2)
{
//Simulates players choosing either rock, paper, or scissors
string player1Choice=p1->makeChoice();
string player2Choice=p2->makeChoice();
p1->setChoice(player1Choice);
p2->setChoice(player2Choice);
cout << "Ready, set" << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "rock..." << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "paper..." << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "scissors..." << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "shoot..." << endl;
cout << p1->getName() << ": " << p1->getChoice() << endl;
cout << p2->getName() << ": " << p2->getChoice() << endl;
determineWinner(p1,p2);
}
int main()
{
unique_ptr<Player> player1{new Player("Player1")};
unique_ptr<Player> player2{new Player("Player2")};
char modeChoice;
cout << "Welcome to this simulation of the classic game Rock, Paper, Scissors!" << endl;
cout << "There are two modes to choose from:" << endl;
cout << "Press 1 for the completely computer simulated version or press 2 if you and a friend want to square off!" << endl;
cout << "Or, if you've played this game too many times and are sick of it, press (q) to quit" << endl;
cin >> modeChoice;
switch(modeChoice)
{
case '1':
playRockPaperScissors(player1,player2);
break;
case '2':
playRockPaperScissors2(player1,player2);
break;
case 'q':
cout << "Program terminating..." << endl;
break;
default:
cout << "Please make a valid choice" << endl;
break;
}
}