8.1 Announce the Game Winner

In the next step, we need to:

💻 C#

// Announce the game winner.

The winner is the first one to reach two wins.

The while loop stops when it happens, and the execution continues on the next line. It’s there where we’ll declare the game winner.


We know that for the program to reach the line after the while loop, one of the players win count should be 2.

One approach could be to check the value of the player win count. If it’s 2, than the player is the winner, else it’s the computer.

🤖 Pseudocode

if playerWinCount is equal to 2
    Player wins the game
else 
    Computer wins the game

The only downside is that if we decide to change the amount of wins needed, there is one extra place to change it.

So, there is a better comparison to declare the game winner: it’s the one with the higher win count.

If the player win count is greater than the computer, the player wins, else the computer wins.

🤖 Pseudocode

if playerWinCount is greater than computerWinCount
    Player wins the game
else 
    Computer wins the game

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!

🖨️ 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: 2
# Sum is: 3

--+ You win this round!
# Round Score: You 1 - Computer 0

--> Choose a number between 1 and 2:
1

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

--! Computer wins this round!
# Round Score: You 1 - Computer 1

--> Choose a number between 1 and 2:
1

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

--! Computer wins this round!
# Round Score: You 1 - Computer 2

--! Computer wins the game!

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

✏️ Program.cs - Changes

    // Announce the game winner.
++  if (playerWinCount > computerWinCount)
++  {
++      GamePrint.Success("You win the game!");
++  }
++  else
++  {
++      GamePrint.Error("Computer wins the game!");
++  }

🗒️ Program.cs - Final

(...)

// Announce the game winner.
if (playerWinCount > computerWinCount)
{
    GamePrint.Success("You win the game!");
}
else
{
    GamePrint.Error("Computer wins the game!");
}