7.2 Track the Score

Let’s handle the first step - track how many rounds a player wins.


We can solve it by declaring two new integer variables before the round starts: playerWinCount and computerWinCount.

Each variable will store how many rounds each player has won.


We’ll need to increment each variable when the corresponding player wins.

🎓 Learn


We already know when it happens, so we can add each statement in the correct code block:

💻 C#

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

And finally, we’ll print how the score stands at the end of the round: Round Score: You 1 - Computer 0.


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

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

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

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

✏️ Program.cs - Changes

++  int playerWinCount = 0;
++  int computerWinCount = 0;

    // 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;

// 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.