Skip to content

Instantly share code, notes, and snippets.

@xseano
Last active October 3, 2018 20:57
Show Gist options
  • Select an option

  • Save xseano/d274d11b8df90d6fdfa67ee2f8aaf64f to your computer and use it in GitHub Desktop.

Select an option

Save xseano/d274d11b8df90d6fdfa67ee2f8aaf64f to your computer and use it in GitHub Desktop.
Project 2
#include <iostream>
#include <iterator>
#define CHOICE_RANGE 3
#define VECTOR_LEN 3
using namespace std;
struct Player
{
int win;
int tie;
int loss;
int rock;
int paper;
int scissor;
};
/**
* @brief Initializes user's stats.
*
* @param user
* @return Player
*/
Player initialize(Player user)
{
user.win = 0;
user.tie = 0;
user.loss = 0;
user.rock = 0;
user.paper = 0;
user.scissor = 0;
return user;
}
void playGame(Player*, Player*);
string isWinner(int, int);
string getMaxObj(int);
string getWeakness(int);
/**
* @brief Main function to handle game operations and scoring.
*
* @return int
*/
int main()
{
string playAgain;
Player user;
user = initialize(user);
Player computer;
computer = initialize(computer);
int* maxObjNum;
int maxObjPos;
int numDiff;
string maxObj;
string objOpp;
cout << "Let's play Rock, Paper, Scissors" << endl;
playGame(&user, &computer);
while(true)
{
cout << "Would you like to play again (Y for yes, N for no)?" << endl;
cin >> playAgain;
if (playAgain == "Y")
{
playGame(&user, &computer);
}
else
{
int rounds = user.win + user.tie + user.loss;
cout << "========== RESULTS ==========" << endl;
cout << "Wins: " << user.win << endl;
cout << "Ties: " << user.tie << endl;
cout << "Losses: " << user.loss << endl;
cout << "Comp Wins: " << computer.win << endl;
cout << "Comp Ties: " << computer.tie << endl;
cout << "Comp Losses: " << computer.loss << endl;
cout << "=============================" << endl;
cout << "========== USAGE ============" << endl;
cout << "Rock: " << user.rock << endl;
cout << "Paper: " << user.paper << endl;
cout << "Scissors: " << user.scissor << endl;
cout << "Comp Rock: " << computer.rock << endl;
cout << "Comp Paper: " << computer.paper << endl;
cout << "Comp Scissors: " << computer.scissor << endl;
cout << "=============================" << endl;
cout << "========== OUTCOME ==========" << endl;
if (computer.win > user.win)
{
int crps[VECTOR_LEN] = {
computer.rock,
computer.paper,
computer.scissor
};
maxObjNum = max_element(crps, crps + VECTOR_LEN);
maxObjPos = distance(crps, maxObjNum);
maxObj = getMaxObj(maxObjPos);
objOpp = getWeakness(maxObjPos);
numDiff = computer.win - user.win;
cout << "The computer won " << numDiff << " more rounds than you, using " << maxObj << " the most." << endl;
cout << "Since the computer used " << maxObj << " " << *maxObjNum << " times out of the " << rounds << " rounds, you should have chosen " << objOpp << " to have win the most times." << endl;
}
else
{
int rps[VECTOR_LEN] = {
user.rock,
user.paper,
user.scissor
};
maxObjNum = max_element(rps, rps + VECTOR_LEN);
maxObjPos = distance(rps, maxObjNum);
maxObj = getMaxObj(maxObjPos);
numDiff = user.win - computer.win;
if (numDiff <= 0)
{
cout << "You did not win anymore rounds than the computer, therefore a tie, using " << maxObj << " the most." << endl;
}
else
{
cout << "You won " << numDiff << " more rounds than the computer, using " << maxObj << " the most." << endl;
}
}
cout << "=============================" << endl;
break;
return 0;
}
}
}
/**
* @brief Function that implements all game logic.
*
* @param user
* @param computer
*/
void playGame(Player* user, Player* computer)
{
int choice;
int myChoice;
bool isValid = true;
string method;
string myMethod;
string outcome;
string chose = "You chose ";
string myChose = "The computer chose ";
myChoice = rand() % CHOICE_RANGE + 1;
cout << "Enter 1 for rock, 2 for paper, 3 for scissors" << endl;
cin >> choice;
switch (choice)
{
case 1:
user->rock += 1;
method = chose + "rock";
break;
case 2:
user->paper += 1;
method = chose + "paper";
break;
case 3:
user->scissor += 1;
method = chose + "scissors";
break;
default:
isValid = false;
method = to_string(choice) + " is not a valid choice";
break;
}
switch (myChoice)
{
case 1:
computer->rock += 1;
myMethod = myChose + "rock";
break;
case 2:
computer->paper += 1;
myMethod = myChose + "paper";
break;
case 3:
computer->scissor += 1;
myMethod = myChose + "scissors";
break;
default:
break;
}
if (isValid == true)
{
outcome = isWinner(choice, myChoice);
if (outcome == "win")
{
user->win += 1;
computer->loss += 1;
outcome = "Therefore, you win!";
}
else if (outcome == "tie")
{
user->tie += 1;
computer->tie += 1;
outcome = "Therefore, you tied!";
}
else
{
user->loss += 1;
computer->win += 1;
outcome = "Therefore, you lose!";
}
cout << method << endl;
cout << myMethod << endl;
cout << outcome << endl;
}
else
{
cout << method << endl;
}
}
/**
* @brief Compare user and computer rock, paper, scissor choice
* to determine who wins.
*
* @param choice
* @param myChoice
* @return string
*/
string isWinner(int choice, int myChoice)
{
if (choice == myChoice)
{
return "tie";
}
if (choice == 1)
{
if (myChoice == 2)
{
return "loss";
}
else
{
return "win";
}
}
if(choice == 2)
{
if (myChoice == 3)
{
return "loss";
}
else
{
return "win";
}
}
if (choice == 3)
{
if (myChoice == 1)
{
return "loss";
}
else
{
return "win";
}
}
return 0;
}
/**
* @brief Determine which object was used most.
*
* @param pos
* @return string
*/
string getMaxObj(int pos)
{
switch(pos)
{
case 0:
return "rock";
break;
case 1:
return "paper";
break;
case 2:
return "scissors";
break;
default:
break;
}
return 0;
}
/**
* @brief Determine the object's respective weakness.
*
* @param pos
* @return string
*/
string getWeakness(int pos)
{
switch(pos)
{
case 0:
return "paper";
break;
case 1:
return "scissors";
break;
case 2:
return "rock";
break;
default:
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment