//Preprocessor Directives
#include<iostream>
#include<string>
#include<cstdlib>
#include<cctype>
#include<ctime>
using namespace std;
//Function Prototypes
void SeedRandom();
void PrintHeader();
void PlayOneMatch(int&, int&, int&, int&, int&);
int GetPlayerChoice();
int DrawNum(int);
void PrintMove(int, int, int&, int&);
void WonAGame(char, int&, int&, int&);
void PrintFinalResults(int, int, int, int);
void Reset(int&, int&, int&, int&, int&, int&, int&);
//Main Driver
int main()
{
char WinnerNumber = '0';
int GamesWon = 0,
WinnerMoney = 0,
LoserMoney = 0,
Player1Money = 100,
Player2Money = 100,
Games1Won = 0,
Games2Won = 0,
TieCount = 0,
MatchCounter = 0,
TotalGames = 0;
SeedRandom();
do{
Reset(GamesWon, WinnerMoney, LoserMoney, Games1Won, Games2Won, TieCount, MatchCounter);
PrintHeader();
PlayOneMatch(Games1Won, Games2Won, TieCount, Player1Money, Player2Money);
PrintFinalResults(Games1Won, Games2Won, TieCount, TotalGames+=3);
}while(TotalGames < 20 || !Player1Money || !Player2Money);
return 0;
}
//Function Definitions
void SeedRandom()
{
srand(time(NULL));
}
void PrintHeader()
{
cout << "\n\t----------=====******=====----------"
<< "\n\tRock, Paper, and Scissors:"
<< "\n\t----------=====******=====----------"
<< "\n\n\tThis program plays the EXCITING game of Rock, Paper,"
<< "\n\tand Scissors. Two players choose either Rock, Paper or Scissors,"
<< "\n\tand the results of their picks are compared. Each match is"
<< "\n\tdetermined as follows:"
<< "\n\n\tPlayer 1 Player 2 Result"
<< "\n\t-------- -------- ------"
<< "\n\t--- --- Any matching combo. A tie!"
<< "\n\n\tNow you are about to play against world-class computer"
<< "\n\tchampion Dr. Windows. You are Player 1, and the computer"
<< "\n\tis Player 2. Player 2's moves are randomly chosen by the computer."
<< "\n\tBoth players start with $100 and the game is finished when either one player"
<< "\n\treaches $0 or there have been 3 matches played. The bet per metch is $5.";
}
void PlayOneMatch(int& Games1Won, int& Games2Won, int& TieCount,
int& Player1Money, int& Player2Money)
{
int playerchoice = 0,
computerchoice = 0,
MatchCounter = 0;
do{
cout << "\n\n\tPlayer 1, Enter your choice of"
<< "\n\t 1 Rock"
<< "\n\t 2 Paper"
<< "\n\t 3 Scissors -> ";
playerchoice = GetPlayerChoice();
computerchoice = DrawNum(3);
if(playerchoice == computerchoice)
TieCount++;
else if(playerchoice > computerchoice ||
playerchoice == 1 && computerchoice == 3)
{
++Games1Won;
WonAGame('1', Games1Won, Player1Money, Player2Money);
}
else
{
++Games2Won;
WonAGame('2', Games2Won, Player2Money, Player1Money);
}
PrintMove(playerchoice, computerchoice, Player2Money, Player1Money);
MatchCounter++;
}while(MatchCounter < 3 || !Player1Money || !Player2Money);
}
int GetPlayerChoice()
{
char choice = '0';
cin.get(choice);
cin.ignore(1000,'\n');
while(!isdigit(choice) || choice < '1' || choice > '3')
{
cout << "\n\t\aThat was not a valid entry. Please try again -> ";
cin.get(choice);
cin.ignore(1000,'\n');
}
return atoi(&choice);
}
int DrawNum(int max)
{
double x = RAND_MAX + 1.0;
int y = 0;
y = static_cast<int> (1 + rand() * (max / x));
return (y);
}
void PrintMove(int Move1, int Move2, int& Player1Money, int& Player2Money)
{
string action[] = {"rock", "paper", "scissors"};
cout << "\n\n\tRESULTS OF THIS MOVE"
<< "\n\t=-=-=-=-=-=-=-=-=-=-"
<< "\n\t Player 1 Player 2 Player 1's Player 2's"
<< "\n\tNumber Action Number Action Winner Money Money"
<< "\n\t------ ------ ------ ------ ------ ----- -----"
<< "\n\t " << Move1 << " " << action[Move1-1]
<< " " << Move2 << " " << action[Move2-1] << " ";
if(Move1 == Move2)
cout << "A tie!";
else if(Move1 > Move2 || Move1 == 1 && Move2 == 3)
cout << "Player 1!";
else
cout << "Player 2!";
cout << " $" << Player1Money << " $" << Player2Money;
}
void WonAGame(char WinnerNumber, int& GamesWon, int& WinnerMoney, int& LoserMoney)
{
switch(WinnerNumber)
{
case '1': WinnerMoney-=5;
LoserMoney+=5;
break;
case '2': LoserMoney+=5;
WinnerMoney-=5;
break;
}
}
void PrintFinalResults(int Games1Won, int Games2Won, int TieCount, int TotalGames)
{
cout << "\n\n\n\tAnd there you have it folks, the final match between our two"
<< "\n\tcontestants. The final results for tonight's game are as follows:"
<< "\n\n\t Player 1 Player 2"
<< "\n\t ======== ========"
<< "\n\tGames Won: " << Games1Won << " " << Games2Won
<< "\n\tPercent Won: " << static_cast<int>(((double)Games1Won/3)*100)
<< "% " << static_cast<int>(((double)Games2Won/3)*100) << '%'
<< "\n\n\t\tTotal games tied: " << TieCount
<< "\n\t\tTotal games played: " << TotalGames
<< "\n\t\tThe overall winner is ";
if(Games1Won == Games2Won)
cout << "Both!";
else if(Games1Won > Games2Won)
cout << "Player 1!";
else
cout << "Player 2!";
cout << "\n\n\tStop in again soon to play another exciting match!!!\n\n\n\n\n\n";
}
void Reset(int& GamesWon, int& WinnerMoney, int& LoserMoney, int& Games1Won,
int& Games2Won, int& TieCount, int& MatchCounter)
{
GamesWon = 0;
WinnerMoney = 0;
LoserMoney = 0;
Games1Won = 0;
Games2Won = 0;
TieCount = 0;
MatchCounter = 0;
}