Announce Game Winner

📖 Instructions#

When the while loop exits, one player has reached 2 wins. The next line after the loop is where we announce who won the game.

One approach is to check if playerWinCount equals 2 — but that ties the logic to a specific number. If we later change the game to require 3 wins, we’d have to update that value in two places.

A better approach: compare the two counts against each other. Whoever has the higher count is the winner. That works regardless of how many wins the game requires.

🧠 Recall

✅ What to Do#

After the while loop, write an if/else statement that prints You win the game! if the player’s win count is greater than the computer’s, or Computer wins the game! otherwise.

🎯 Expected Outcome#

Player wins:

Choose a number between 1 and 2:
2
You chose: 2
Computer chose: 1
Sum is: 3
You win this round!
Round Score: You 2 - Computer 1
You win the game!

Computer wins:

Choose a number between 1 and 2:
1
You chose: 1
Computer chose: 1
Sum is: 2
Computer wins this round!
Round Score: You 1 - Computer 2
Computer wins the game!

💡 Hints#

Hint 1

The if/else goes after the closing } of the while loop.

Hint 2

The condition compares the two win count variables against each other — not against a fixed number.

Hint 3

Use the greater than operator to check which count is higher.

⚠️ Common Mistakes#

Placing the if/else inside the loop

Putting the game winner announcement inside the while loop prints it after every round, not just at the end. It must go after the closing } of the while loop.

Comparing against 2 instead of against the other count

if (playerWinCount == 2) works for this game but is fragile. If the win condition changes, it breaks. Compare the two counts against each other instead.

Using >= instead of >

playerWinCount >= computerWinCount would be true even when the counts are equal — which can’t happen when the loop exits, but is poor logic. Use > to clearly express that the player has more wins than the computer.

🙈 Solution#

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

Program.csChanges

  // Announce the game winner.
+ if (playerWinCount > computerWinCount)
+ {
+     Console.WriteLine("You win the game!");
+ }
+ else
+ {
+     Console.WriteLine("Computer wins the game!");
+ }

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;

// Repeat rounds until one of the players wins two times.
while (playerWinCount < 2 && computerWinCount < 2)
{
    // Round starts here.

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

    Random random = new Random();
    int computerNumber = random.Next(1, 3);
    Console.WriteLine($"Computer chose: {computerNumber}");

    int sum = playerNumber + computerNumber;
    Console.WriteLine($"Sum is: {sum}");

    bool sumIsEven = sum % 2 == 0;

    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.
}

// Announce the game winner.
if (playerWinCount > computerWinCount)
{
    Console.WriteLine("You win the game!");
}
else
{
    Console.WriteLine("Computer wins the game!");
}