6.3 Deciding the Round Winner

In the last step, we:

💻 C#

// Add the two numbers.
// Check if the sum is odd or even.
// --> Decide the round winner.

If the sum is evens, the winner is the one playing with evens. It can be either the player or the computer. It depends on the player initial choice.


At the beginning, we stored the player’s choice in a variable. That variable holds the value 1 if the player is odds and 2 if evens.

💻 C#

playerSide == 2 // The player is evens.

If the player is evens and the sum is evens, the player is the winner, otherwise, the computer wins the round.


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: 1
# Sum is: 3

--+ You win this round!

🖨️ 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:
1

# You chose: 1
# Computer chose: 1
# Sum is: 2

--! Computer wins this round!

🙈 Solution
Tried, you must, before reveal the solution you may. - Yoda

✏️ Program.cs - Changes

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

🗒️ Program.cs - Final

(...)

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

(...)