In the final step:
๐ป C#
// Ask the player to play again or quit.We can approach it the same way we did with the round logic, with a while loop.
But instead of repeating rounds, weโll repeat the entire game logic.
๐ป C#
GamePrint.Box("Welcome to Odds and Evens!");
// Game starts here.
// Player chooses to play with odds or evens.
GamePrint.Input("Choose 1 for Odds or 2 for Evens");
int playerNumberType = GameInput.Number(1, 2);
(...)
// Determine the winner.
if (playerWinCount > computerWinCount)
{
GamePrint.Success("You win the game!");
}
else
{
GamePrint.Success("Computer wins the game!");
}
// Game ends here.And we also need a different condition. The loop stops, only when the player chooses to quit, otherwise it should run forever.
๐ค Pseudocode
while forever
repeat game Logic
if player chooses to quit
exit loop
To create such a loop, we can use true as the
condition.
This makes it impossible to stop by manipulating the condition. Instead, we need to use the break statement to stop the loop and continue on the next line.
๐ 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: 1
# Sum is: 3
--+ You win this round!
# Round Score: You 2 - Computer 1
--+ You win the game!
--> Press 1 to play again or 2 to quit:
1
--> 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:
2
# You chose: 2
# Computer chose: 1
# Sum is: 3
--+ You win this round!
# Round Score: You 1 - 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 2 - Computer 1
--+ You win the game!
--> Press 1 to play again or 2 to quit:
2
+----------------+
+-- Game Over! --+
+----------------+
๐ Solution
โ๏ธ Program.cs - Changes
GamePrint.Box("Welcome to Odds and Evens!");
// Ask the player to play again or quit.
++ while(true)
++ {
โโ // Player chooses to play with odds or evens.
โโ GamePrint.Input("Choose 1 for Odds or 2 for Evens");
โโ int playerSide = GameInput.Number(1, 2);
โโ
โโ if (playerSide == 1)
โโ {
โโ GamePrint.Message("You chose Odds, Computer is Evens");
โโ }
โโ else
โโ {
โโ GamePrint.Message("You chose Evens, Computer is Odds");
โโ }
โโ
โโ 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.
โโ }
โโ
โโ // Announce the game winner.
โโ if (playerWinCount > computerWinCount)
โโ {
โโ GamePrint.Success("You win the game!");
โโ }
โโ else
โโ {
โโ GamePrint.Error("Computer wins the game!");
โโ }
++
++ GamePrint.Input("Press 1 to play again or 2 to quit");
++ int playAgainChoice = GameInput.Number(1, 2);
++
++ if (playAgainChoice == 2)
++ {
++ break;
++ }
++ }
++
++ GamePrint.Box("Game Over!");๐๏ธ Program.cs - Final
GamePrint.Box("Welcome to Odds and Evens!");
// Player chooses to play again or quit.
while (true)
{
GamePrint.Input("Choose 1 for Odds or 2 for Evens");
int playerNumberType = GameInput.Number(1, 2);
if (playerNumberType == 1)
{
GamePrint.Message("You chose Odds");
GamePrint.Message("Computer is Evens");
}
else
{
GamePrint.Message("You chose Evens");
GamePrint.Message("Computer is Odds");
}
// 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 userNumber = GameInput.Number(1, 2);
GamePrint.Message("You chose: " + userNumber);
// Computer randomly generates a number between 1 and 2.
int computerNumber = GameRandom.Number(1, 2);
GamePrint.Message("Computer chose: " + computerNumber);
// Sum both numbers.
int sumResult = userNumber + computerNumber;
GamePrint.Message("Sum is: " + sumResult);
// Determine if the sum of the two numbers is odd or even.
bool resultIsEven = sumResult % 2 == 0;
// Determine the round winner.
bool playerIsEven = playerNumberType == 2;
if (playerIsEven == resultIsEven)
{
GamePrint.Success("You win this round!");
playerWinCount++;
}
else
{
GamePrint.Error("Computer wins this round!");
computerWinCount++;
}
// Round ends here.
GamePrint.Message("Round Score: You " + playerWinCount + " - Computer " + computerWinCount);
}
// Determine the winner.
if (playerWinCount > computerWinCount)
{
GamePrint.Success("You win the game!");
}
else
{
GamePrint.Success("Computer wins the game!");
}
GamePrint.Input("Press 1 to play again or 2 to quit");
int playAgainChoice = GameInput.Number(1, 2);
if (playAgainChoice == 2)
{
break;
}
}
GamePrint.Box("Game Over!");