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
βοΈ 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.
}