2021-Draw not possible

From TRCCompSci - AQA Computer Science
Revision as of 08:34, 4 September 2020 by Admin (talk | contribs) (Created page with "=Actual Code= <syntaxhighlight lang=c#> public static void DisplayEndMessages(Player player1, Player player2) { Console.WriteLine(); Co...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Actual Code

        public static void DisplayEndMessages(Player player1, Player player2)
        {
            Console.WriteLine();
            Console.WriteLine(player1.GetName() + " final state: " + player1.GetStateString());
            Console.WriteLine();
            Console.WriteLine(player2.GetName() + " final state: " + player2.GetStateString());
            Console.WriteLine();
            if (player1.GetVPs() > player2.GetVPs())
            {
                Console.WriteLine(player1.GetName() + " is the winner!");
            }
            else
            {
                Console.WriteLine(player2.GetName() + " is the winner!");
            }
        }

Issue

When a game is finished, the code above is used to display the outcome of the game. The original code checks to see if player1 VP's are greater than player2 VP's, however if the VP's are the same player2 is declared the winner.

Solution

We need to adapt the if..else statement by adding an 'else if':

            if (player1.GetVPs() > player2.GetVPs())
            {
                Console.WriteLine(player1.GetName() + " is the winner!");
            }
            else
            {
                Console.WriteLine(player2.GetName() + " is the winner!");
            }

Will become:

            if (player1.GetVPs() > player2.GetVPs())
            {
                Console.WriteLine(player1.GetName() + " is the winner!");
            }
            else if (player1.GetVps() == player2.GetVPs())
            {
                Console.WriteLine("Its a draw!");
            }
            else
            {
                Console.WriteLine(player2.GetName() + " is the winner!");
            }