Murder face Murder face - 2 months ago 17
C++ Question

How to track computer wins and player wins until user quits?

I am new to c++ and I need help figuring out how to keep track of wins for both the computer and player until the player quits. I was thinking I could declare a variable globally but I am not sure if that would be good practice. This is what I have so far.

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

// function declarations
int computerRoll();
int computerGuess();
void winner(int uGuess, int cGuess, int uSum, int cSum);

int main()
{
// variable declaration
int userSum = 0, userGuess = 0, aiGuess = 0, aiSum = 0, play = 0;

while (true) {

// ask user for sum of 3 dice rolls
cout << "Enter the sum of your 3 dice rolls: ";
cin >> userSum;
cout << "\n\n";

// check for correct user input
while (userSum > 18 || userSum < 3)
{
cout << "\a\a\a Please enter a sum between 3 and 18: ";
cin >> userSum;
cout << "\n\n";
}

//computer guess
aiGuess = computerGuess();
cout << "The computer has guessed " << aiGuess << " For your number";
cout << "\n\n";

// computer rolls dice 3 times
aiSum = computerRoll();

// User guesses the amount that was rolled by the computer
cout << "Please guess the sum of the 3 dice rolls by the computer: ";
cin >> userGuess;
cout << "\n\n";

// check for correct user input
while (userGuess > 18 || userGuess < 3)
{
cout << "\a\a\a Please enter a sum between 3 and 18: ";
cin >> userGuess;
cout << "\n\n";
}

// Program determins winner/ keeps score
winner(userGuess, aiGuess, userSum, aiSum);

// ask user to keep playing or quit
cout << "Enter any number to keep playing. Enter -1 to stop: ";
cin >> play;
cout << "\n\n";

if (play == -1)
{
break;
}
else
{
continue;
}
}
return 0;
}

// simulate the computer rolling 3 dice, get sum
int computerRoll()
{
int srand(time(NULL));
int computerSum = 0;
const int MAX = 3;
int computerArray[MAX];

for (int i = 0; i < 3; i++)
{
// Just ignor the array for now, was just trying something out
computerArray[i] = rand() % (6 + 1);
computerSum += computerArray[i];
}

return computerSum;
}
// simulate the computer guessing the sum of the 3 dice player has rolled
int computerGuess()
{
int srand(time(NULL));
int guess = 0;

guess = rand() % (18 + 1);

return guess;
}
// Get the difference in guesses
void winner(int uGuess, int cGuess, int uSum, int cSum)
{
int userDifference = 0;
int compDifference = 0;
int compWins = 0;
int userWins = 0;

if (uGuess >= cSum)
{
userDifference = uGuess - cSum;
}
else if (uGuess <= cSum)
{
userDifference = cSum - uGuess;
}
if (cGuess >= uSum)
{
compDifference = cGuess - uSum;
}
else if (cGuess <= uSum)
{
compDifference = uSum - cGuess;
}
if (userDifference >= compDifference)
{
cout << "Computer wins!\n\n";
compWins = compWins++;
cout << userWins << " - " << compWins;
cout << "\n\n";
}
else // userDifference < compDifference
{
cout << "You win!\n\n";
userWins = userWins++;
cout << userWins << " - " << compWins;
cout << "\n\n";
}

}

Answer

Here's an algorithm:

unsigned int computer_wins = 0U;
unsigned int player_wins = 0U;
bool quit_the_game = false;
while (!quit_the_game)
{
  Prompt the user for input.
  if User wants to quit, break out of loop.
  Calculate computer's move.
  Determine winner.
  if (winner == computer) ++computer_wins;
  else ++player_wins;
}
Display player and computer wins.

The implementation is left as an exercise for the OP.