Show the Winner

πŸ“– Instructions#

We have the sum and we know whether it’s odd or even. Now we need to decide who wins the round.

The player wins if their side matches the sum. If the player chose evens and the sum is even β€” they win. If they chose odds and the sum is odd β€” they win too.

We already have sumIsEven telling us whether the sum is even. We need one more boolean: whether the player is playing evens. Then we compare them β€” if they match, the player wins.

🧠 Recall

Two boolean variables can be compared with == just like numbers. If both are true or both are false the comparison is true. If they differ it is false:

bool a = true;
bool b = true;
bool match = a == b;  // true

βœ… What to Do#

Declare a boolean variable playerIsEven that stores whether the player chose evens. Then write an if/else statement that prints You win this round! if playerIsEven matches sumIsEven, or Computer wins this round! otherwise.

🎯 Expected Outcome#

Player chose odds (1), sum is odd β€” player 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: 1
Sum is: 3
You win this round!

Player chose odds (1), sum is even β€” computer 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:
1
You chose: 1
Computer chose: 1
Sum is: 2
Computer wins this round!

πŸ’‘ Hints#

Hint 1

You already know how to store a comparison result in a boolean variable β€” you did it in the previous lesson.

Hint 2

The player is playing evens if their choice equals 2. That’s the boolean you need to store.

Hint 3

The condition in the if statement compares playerIsEven with sumIsEven using ==.

⚠️ Common Mistakes#

Comparing playerChoice to sumIsEven directly

playerChoice == sumIsEven compares an integer to a boolean, which causes a compile error. First convert playerChoice to a boolean by checking whether it equals 2, then compare the two booleans.

Getting the win condition backwards

The player wins when their side matches the sum β€” playerIsEven == sumIsEven. Writing playerIsEven != sumIsEven reverses the result and the computer wins every time the player should win.

Using playerChoice instead of playerIsEven in the condition

if (playerChoice == sumIsEven) compares an integer to a boolean. Declare playerIsEven first, then use it in the condition.

πŸ™ˆ Solution#

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

Program.csChanges

  // Decide the round winner.
+ bool playerIsEven = playerChoice == 2;
+ if (playerIsEven == sumIsEven)
+ {
+     Console.WriteLine("You win this round!");
+ }
+ else
+ {
+     Console.WriteLine("Computer wins this round!");
+ }

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

// 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!");
}
else
{
    Console.WriteLine("Computer wins this round!");
}

(...)