How to add score counter to rock paper scissors game? I can't get score counter work. (Player wins, Computerwins)
I got it working without functions but now when I added them I don't just get it how to do it...

#include <iostream>
#include <math.h>
#include <string>

int numGen ();
int choiceConverter (std :: string);
void DisplayComputer (int);
void winner (int, int);

int main ()   
{
std :: string playerChoose;
int player;
int compChoice;
int rounds = 0;
int gains = 0;
int machineGains = 0;

// Ask the user how many rounds to play? 
std :: cout << "How many rounds do you want to play?" << std :: endl;  
std :: cout << "Enter rounds>";
std :: cin >> rounds;

for (int round = 1; round <= rounds; round ++)
{

std :: cout << "Round number:" << round << "/" << rounds << std :: endl;

std :: cout << "\nSelect rock, paper or scissors:";
std :: cin >> playerChoose;
std :: cout << "\ nYour selection is:" << PlayerChoose << "\ n";



compChoice = numGen ();
player = choiceConverter (playerChoice);
displayComputer (compChoice);
winner (compChoice, player);
}

} 
int numGen ()
{
std :: srand (time (0));
int randomi = rand ()% 3 + 1;

return randomi;
}

int choiceConverter(std :: string player1) 
{
int numEquiv = 0;

if (player1 == "rock")
character code = 1;
else if (player1 == "paper")
character code = 2;
else if (player1 == "scissors")
character code = 3;    
return numEquiv
}
void displayComputer (int player2)
{
if (player2 == 1)
{
std :: cout << "The computer chose rock \ n";
}
else if (player2 == 2)
{
std :: cout << "The computer chose paper \ n";
    }
else if (player2 == 3)
{
std :: cout << "The computer chose scissors \ n";
}
}

void winner (int player1, int player2)
{

if ((player1 == 1 && player2 == 2) || (player1 == 2 && player2 == 3) || (player1 == 3 && player2 == 1))
{
std :: cout << "\nYou won the game! \ n \ n";

}
else if ((player1 == 1 && player2 == 3) || (player1 == 2 && player2 == 1) || (player1 == 3 && player2 == 2))
{
std :: cout << "\ nComputer won the game! \ n \ n";

}
else
{
std :: cout << "\nDraw \ n \ n";
}   
}

Recommended Answers

All 2 Replies

There were several errors in your code and it would not compile. Once I got through that it was easy to add counters to the code and pass these to the functions to calculate the score. Here is a working example:

#include <iostream>
#include <math.h>
#include <string>

int numGen();
int choiceConverter(std ::string);
void displayComputer(int);
void displayScores(int &, int &);
void winner(int, int, int &, int &);

int main()
{
  std ::string playerChoose;
  int player;
  int compChoice;
  int rounds = 0;
  int gains = 0;
  int machineGains = 0;
  int playerScore = 0;
  int cpuScore = 0;

  // Ask the user how many rounds to play?
  std ::cout << "How many rounds do you want to play?" << std ::endl;
  std ::cout << "Enter rounds>";
  std ::cin >> rounds;

  for (int round = 1; round <= rounds; round++)
  {

    std ::cout << "Round number:" << round << "/" << rounds << std ::endl;

    std ::cout << "\nSelect rock, paper or scissors:";
    std ::cin >> playerChoose;
    std ::cout << "\nYour selection is:" << playerChoose << "\n";

    compChoice = numGen();
    player = choiceConverter(playerChoose);
    displayComputer(compChoice);
    winner(compChoice, player, playerScore, cpuScore);
  }
  displayScores(playerScore, cpuScore);
}

int numGen()
{
  std ::srand(time(0));
  int randomi = rand() % 3 + 1;

  return randomi;
}

int choiceConverter(std ::string player1)
{
  int numEquiv = 0;

  if (player1 == "rock")
    numEquiv = 1;
  else if (player1 == "paper")
    numEquiv = 2;
  else if (player1 == "scissors")
    numEquiv = 3;
  return numEquiv;
}

void displayComputer(int player2)
{
  if (player2 == 1)
  {
    std ::cout << "The computer chose rock \n";
  }
  else if (player2 == 2)
  {
    std ::cout << "The computer chose paper \n";
  }
  else if (player2 == 3)
  {
    std ::cout << "The computer chose scissors \n";
  }
}

void displayScores(int &playerScore, int &cpuScore)
{
  std ::cout << "\nPlayer score = " << playerScore << "\n";
  std ::cout << "\nComputer score = " << cpuScore << "\n";
}

void winner(int player1, int player2, int &playerScore, int &cpuScore)
{

  if ((player1 == 1 && player2 == 2) || (player1 == 2 && player2 == 3) || (player1 == 3 && player2 == 1))
  {
    playerScore++;
    std ::cout << "\nYou won the game! \n";
    std::cout << "You have won " << playerScore << " games.\n\n";
  }
  else if ((player1 == 1 && player2 == 3) || (player1 == 2 && player2 == 1) || (player1 == 3 && player2 == 2))
  {
    cpuScore++;
    std ::cout << "\nComputer won the game! \n \n";
    std::cout << "Computer has won " << cpuScore << " games.\n\n";
  }
  else
  {
    std ::cout << "\nDraw \n \n";
  }
}

Good luck

commented: This is great. Also, the professor is likely to find this interesting. +1 +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.