I don't realy understand your loop.
while (playerWin < 5)
It only checks if the player. What if the computer gets to 5 first? So you would want to check if either win unless its only game over if player gets 5.
while (playerWin < 5 && computerWin < 5)
Instead of writing this twice
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
switch(playerChoice)
{
case 1: cout << "You: Rock" << endl;
break;
case 2: cout << "You: Paper" << endl;
break;
case 3: cout << "You: Scissors" << endl;
break;
}
computerChoice = rand() % 3 + 1;
switch(computerChoice)
{
case 1: cout << "Computer: Rock" << endl;
break;
case 2: cout << "Computer: Paper" << endl;
break;
case 3: cout << "Computer: Scissors" << endl;
break;
}
|
You could make it a function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void PrintNames(int picked)
{
switch(picked)
{
case 1:
cout << "Paper";
break;
case 2:
cout << "Scissors";
break;
case 3:
cout << "Rock";
break;
}
}
|
Also instead of all the crazy comparisons you can use some simple math.
Assuming Paper = 1, Scissors = 2, Rock = 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
turnScore = playerChoice - comChoice;
if(turnScore == 0) //Tie
{
cout << "Tie! You bolth picked "; PrintNames(playerChoice); cout << endl;
tie++;
}
else if(turnScore == 2) //Comp
{
Winner(1,comChoice,playerChoice);
compWin++;
}
else if(turnScore == -2) //Player Wins
{
Winner(0,comChoice,playerChoice);
playerWin++;
}
else if(turnScore < 0) //Comp Wins
{
Winner(1,comChoice,playerChoice);
compWin++;
}
else //Player Wins
{
Winner(0,comChoice,playerChoice);
playerWin++;
}
|
Could get rid of this and the counter Var cuz its not realy doing anything but cloning playerWin.
1 2 3 4 5 6 7 8 9 10
|
if(computerWin == 5)
{
playerWin = 100;
cout << "\nTHE COMPUTER WON!!!" << endl;
}
if(playerWin == 5)
{
cout << "\nYOU WON!!!" << endl;
}
|
And just replace it with your function like this
1 2 3 4 5 6 7 8 9
|
if(playerWin == 5 || computerWin == 5){
cout << endl << endl;
cout << "---------- Game Statistics ----------" << endl;
cout << "Ties: " << tie << endl;
cout << "Player Wins: " << playerWin << endl;
cout << "Computer Wins: " << computerWin << endl;
cout << "_____________________________________" << endl << endl << endl;
}
|
You don't need that to be function you are only calling it once. Your just using more memory by cloning ( tie->x , playerWin->counter ->y and computerWins ->z)
Altho with as much memory and the processors we have now it doesn't matter much altho with a big project it could add up.
And add this function to stop the need to type the same thing over and over.
1 2 3 4 5 6 7 8 9 10
|
void Winner(int player , int comNum , int playerNum)
{
if(player == 0)
{
cout << "You Wins! ";
}else{
cout << "Computer Wins! ";
}
PrintNames(comNum); cout << " BEATS "; PrintNames(playerNum); cout << endl;
}
|
Good luck and hope some of this helps :)