Track Score

πŸ“– Instructions#

The game currently announces the round winner but doesn’t remember how many rounds each player has won. We need two counters β€” one for the player, one for the computer β€” and increment the right one after each round.

A counter is just an integer variable starting at 0. Each time the corresponding player wins, we add 1 to it using the increment operator.

🧠 Recall

The counters need to be declared before the round starts so they persist between rounds later. Incrementing happens inside the correct branch of the if/else that decides the winner.

βœ… What to Do#

Declare two integer variables playerWinCount and computerWinCount before the round. Increment the correct one inside each branch of the winner if/else. Then print the score at the end of the round as Round Score: You {playerWinCount} - Computer {computerWinCount}.

🎯 Expected Outcome#

Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
1
You chose Odds.
Choose a number between 1 and 2:
1
You chose: 1
Computer chose: 2
Sum is: 3
You win this round!
Round Score: You 1 - Computer 0

πŸ’‘ Hints#

Hint 1

Declare both counters before the // Round starts here. comment, initialised to 0.

Hint 2

Increment playerWinCount in the if branch, computerWinCount in the else branch β€” right after the message that announces the winner.

Hint 3

The score line goes after the closing } of the if/else block, before // Round ends here.. Use string interpolation to embed both counter values in the message.

⚠️ Common Mistakes#

Declaring the counters inside the round

Declaring playerWinCount and computerWinCount after // Round starts here. means they reset to 0 every round once the loop is added. Declare them before the round so they persist.

Incrementing both counters regardless of the winner

Incrementing inside both branches means both counters increase every round. Each counter should only increment in its own branch.

Printing the score before incrementing

If the print statement comes before the increment, the score will always show the value from the previous round. Increment first, then print.

πŸ™ˆ Solution#

Tried, you must, before reveal the solution you may.

Program.csChanges

+ int playerWinCount = 0;
+ int computerWinCount = 0;

  // Round starts here.

  // Player enters a number between 1 and 2.
  Console.WriteLine("Choose a number between 1 and 2:");
  string rawNumber = Console.ReadLine();
  int playerNumber = int.Parse(rawNumber);
  Console.WriteLine($"You chose: {playerNumber}");

  // Computer randomly generates a number between 1 and 2.
  Random random = new Random();
  int computerNumber = random.Next(1, 3);
  Console.WriteLine($"Computer chose: {computerNumber}");

  // Add the two numbers.
  int sum = playerNumber + computerNumber;
  Console.WriteLine($"Sum is: {sum}");

  // Check if the sum is odd or even.
  bool sumIsEven = sum % 2 == 0;

  // Decide the round winner.
  bool playerIsEven = playerChoice == 2;
  if (playerIsEven == sumIsEven)
  {
      Console.WriteLine("You win this round!");
+     playerWinCount++;
  }
  else
  {
      Console.WriteLine("Computer wins this round!");
+     computerWinCount++;
  }

+ Console.WriteLine($"Round Score: You {playerWinCount} - Computer {computerWinCount}");

  // Round ends here.

Program.csFinal

Console.WriteLine("Welcome to Odds and Evens!");

// Player chooses to play with odds or evens.
Console.WriteLine("Choose 1 for Odds or 2 for Evens:");
string input = Console.ReadLine();
int playerChoice = int.Parse(input);

if (playerChoice == 1)
{
    Console.WriteLine("You chose Odds.");
}
else
{
    Console.WriteLine("You chose Evens.");
}

int playerWinCount = 0;
int computerWinCount = 0;

// Round starts here.

// Player enters a number between 1 and 2.
Console.WriteLine("Choose a number between 1 and 2:");
string rawNumber = Console.ReadLine();
int playerNumber = int.Parse(rawNumber);
Console.WriteLine($"You chose: {playerNumber}");

// Computer randomly generates a number between 1 and 2.
Random random = new Random();
int computerNumber = random.Next(1, 3);
Console.WriteLine($"Computer chose: {computerNumber}");

// Add the two numbers.
int sum = playerNumber + computerNumber;
Console.WriteLine($"Sum is: {sum}");

// Check if the sum is odd or even.
bool sumIsEven = sum % 2 == 0;

// Decide the round winner.
bool playerIsEven = playerChoice == 2;
if (playerIsEven == sumIsEven)
{
    Console.WriteLine("You win this round!");
    playerWinCount++;
}
else
{
    Console.WriteLine("Computer wins this round!");
    computerWinCount++;
}

Console.WriteLine($"Round Score: You {playerWinCount} - Computer {computerWinCount}");

// Round ends here.

(...)