Play Multiple Rounds

📖 Instructions#

The game currently plays one round and stops. We need it to keep playing until one player wins twice.

To repeat a block of code, we use a while loop. It keeps running as long as its condition is true. When the condition becomes false, the loop stops.

🧠 Recall

The condition here involves two checks at once: the player’s win count must be less than 2 and the computer’s win count must be less than 2. As soon as either reaches 2, the loop stops.

To combine two conditions so that both must be true, we use the logical AND operator:

while (conditionA && conditionB)
{
    // repeated logic
}

&& means both conditions must be true for the loop to continue. As soon as either becomes false, the loop exits.

🎓 Learn More

✅ What to Do#

Wrap the round code — everything between // Round starts here. and // Round ends here. — in a while loop that continues as long as both win counts are less than 2.

🎯 Expected Outcome#

The game now plays multiple rounds. It stops as soon as one player reaches 2 wins:

Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
1
You chose Odds.
Choose a number between 1 and 2:
2
You chose: 2
Computer chose: 2
Sum is: 4
Computer wins this round!
Round Score: You 0 - Computer 1
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 1
Choose a number between 1 and 2:
2
You chose: 2
Computer chose: 2
Sum is: 4
Computer wins this round!
Round Score: You 1 - Computer 2

💡 Hints#

Hint 1

The while loop goes around the round code — the two counter declarations stay outside it, before the loop.

Hint 2

The condition needs two comparisons joined together. Each one checks whether a win count is less than 2.

Hint 3

Use && to join the two comparisons. Both must be true for the loop to keep running.

⚠️ Common Mistakes#

Moving the counter declarations inside the loop

Declaring playerWinCount and computerWinCount inside the while loop resets them to 0 at the start of every round. The counters must stay outside the loop so they accumulate across rounds.

Using || instead of &&

|| means either condition being true keeps the loop running. That would play forever — even when one player has already won twice — because the other player’s count is still below 2. Use && so the loop stops as soon as either player reaches 2.

Infinite loop — forgetting to increment inside the loop

If the win counters never change, the condition is always true and the loop never stops. Make sure playerWinCount++ and computerWinCount++ are still inside the loop body.

Wrong comparison operator

Using <= instead of < means the loop continues even when a player has exactly 2 wins, playing one extra round. Use < 2 to stop as soon as a player reaches 2.

🙈 Solution#

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

Program.csChanges

  int playerWinCount = 0;
  int computerWinCount = 0;

+ while (playerWinCount < 2 && computerWinCount < 2)
+ {
      // 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;

// Repeat rounds until one of the players wins two times.
while (playerWinCount < 2 && computerWinCount < 2)
{
    // 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.
}

(...)