7.3 Playing Multiple Rounds

Let’s handle the second step - playing consecutive rounds until one player wins at least two times.


To repeat the round logic, we need a while statement.

πŸŽ“ Learn


The condition, in this case, is one of the players winning two times.

Which is the same as saying, we’ll play new rounds while both win counts are less than two.

As soon as one of the win counts reaches two, the while loop stops and the game is over.

πŸ€– Pseudocode

while playerWinCount is less than 2 and computerWinCount is less than 2
    Repeat the round logic

We already covered how to create a condition using a comparison.

But, in this case we have a compound condition made of two comparisons. Both need to be true for the condition to be true.

πŸŽ“ Learn


βœ… What to Do

🎯 Expected Outcome

πŸ–¨οΈ Console

+--------------------------------+
+-- Welcome to Odds and Evens! --+
+--------------------------------+


--> Choose 1 for Odds or 2 for Evens:
1

# You chose Odds, Computer is Evens

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

πŸ™ˆ Solution
Tried, you must, before reveal the solution you may. - Yoda

✏️ Program.cs - Changes

    // Repeat until one of the players wins two rounds.
↓↓  int playerWinCount = 0;
↓↓  int computerWinCount = 0;

++  while (playerWinCount < 2 && computerWinCount < 2)
++  {
↓↓      // Round starts here.
↓↓
↓↓      // Player enters a number between 1 and 2.
↓↓      GamePrint.Input("Choose a number between 1 and 2");
↓↓      int playerNumber = GameInput.Number(1, 2);
↓↓
↓↓      GamePrint.Message("You chose: " + playerNumber);
↓↓
↓↓      // Computer randomly generates a number between 1 and 2.
↓↓      int computerNumber = GameRandom.Number(1, 2);
↓↓      GamePrint.Message("Computer chose: " + computerNumber);
↓↓
↓↓      // Add the two numbers.
↓↓      int sum = playerNumber + computerNumber;
↓↓      GamePrint.Message("Sum is: " + sum);
↓↓
↓↓      // Check if the sum is odd or even.
↓↓      bool sumIsEvens = sum % 2 == 0;
↓↓
↓↓      // Decide the round winner.
↓↓      bool playerIsEvens = playerSide == 2;
↓↓      if (playerIsEvens == sumIsEvens)
↓↓      {
↓↓          GamePrint.Success("You win this round!");
↓↓          playerWinCount++;
↓↓      }
↓↓      else
↓↓      {
↓↓          GamePrint.Error("Computer wins this round!");
↓↓          computerWinCount++;
↓↓      }
↓↓
↓↓      GamePrint.Message("Round Score: You " + playerWinCount + " - Computer " + computerWinCount);
↓↓
↓↓      // Round ends here.
++  }

πŸ—’οΈ Program.cs - Final

(...)

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.
    GamePrint.Input("Choose a number between 1 and 2");
    int playerNumber = GameInput.Number(1, 2);

    GamePrint.Message("You chose: " + playerNumber);

    // Computer randomly generates a number between 1 and 2.
    int computerNumber = GameRandom.Number(1, 2);
    GamePrint.Message("Computer chose: " + computerNumber);

    // Add the two numbers.
    int sum = playerNumber + computerNumber;
    GamePrint.Message("Sum is: " + sum);

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

    // Decide the round winner.
    bool playerIsEvens = playerSide == 2;
    if (playerIsEvens == sumIsEvens)
    {
        GamePrint.Success("You win this round!");
        playerWinCount++;
    }
    else
    {
        GamePrint.Error("Computer wins this round!");
        computerWinCount++;
    }

    GamePrint.Message("Round Score: You " + playerWinCount + " - Computer " + computerWinCount);

    // Round ends here.
}