Add Replayability

πŸ“– Instructions#

The game currently plays once and stops. We want to give the player the option to play again after each game.

The approach is the same as the round loop β€” wrap the game logic in a while loop. But this time the condition isn’t based on a counter. We want the loop to keep running until the player explicitly decides to quit.

The cleanest way to do this is while (true): a loop with no exit condition. It runs forever unless something inside it stops it. That something is the break statement β€” it exits the loop immediately and execution continues on the next line.

🧠 Recall

After announcing the game winner, we ask the player if they want to play again. If they choose to quit, we break out of the loop and print a final message.

πŸŽ“ Learn More

βœ… What to Do#

Wrap the entire game logic in a while (true) loop. Move playerWinCount and computerWinCount inside it so they reset each game. After announcing the winner, ask Press 1 to play again or 2 to quit:, read and parse the input, and break if the player chooses 2. Print Game Over! after the loop.

🎯 Expected Outcome#

The game replays when the player chooses 1 and exits when they choose 2:

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
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 0
You win the game!
Press 1 to play again or 2 to quit:
2
Game Over!

πŸ’‘ Hints#

Hint 1

The outer while (true) wraps everything from the player choosing odds/evens to the game winner announcement.

Hint 2

playerWinCount and computerWinCount must be inside the outer loop so they reset to 0 at the start of each game.

Hint 3

After the game winner is announced, read and parse the player’s choice. If they chose 2, use break to exit the outer loop.

⚠️ Common Mistakes#

Leaving the counters outside the outer loop

If playerWinCount and computerWinCount stay outside the outer loop, they carry over from the previous game and the round loop exits immediately on the second play. Move them inside the outer loop.

Breaking on the wrong value

if (playAgainChoice == 1) breaks when the player wants to continue. Break when they choose 2 β€” the quit option.

Printing Game Over inside the loop

Console.WriteLine("Game Over!") inside the loop prints it at the end of every game, not just when quitting. It belongs after the closing } of the outer while loop.

Forgetting to move Random outside the inner loop

new Random() should be created once, before the outer loop, and reused across all rounds and games. Creating it inside the rounds loop can produce repeated values.

πŸ™ˆ Solution#

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

Program.csChanges

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

+ Random random = new Random();
+
+ while (true)
+ {
      // 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;
+     int playerWinCount = 0;
+     int computerWinCount = 0;

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

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

+     Console.WriteLine("Press 1 to play again or 2 to quit:");
+     string playAgainInput = Console.ReadLine();
+     int playAgainChoice = int.Parse(playAgainInput);
+
+     if (playAgainChoice == 2)
+     {
+         break;
+     }
+ }
+
+ Console.WriteLine("Game Over!");

Program.csFinal

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

Random random = new Random();

while (true)
{
    // 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}");

        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!");
    }

    // Ask the player to play again or quit.
    Console.WriteLine("Press 1 to play again or 2 to quit:");
    string playAgainInput = Console.ReadLine();
    int playAgainChoice = int.Parse(playAgainInput);

    if (playAgainChoice == 2)
    {
        break;
    }
}

Console.WriteLine("Game Over!");