Home > OS >  How to replay the game and break out of a loop?
How to replay the game and break out of a loop?

Time:07-15

I'm building a program that the dices generates random numbers. I decided to write down in other methods while calling it in the main method. I'm trying to replay the game and break out of the loop by writing in a seperate method. For some reason it won't work and the console shows me an error to which I will show you the picture below.

        static void Main(string[] args)
        { 
        Console.WriteLine("Hey! Welcome to Tina's Dice Game");
        Console.WriteLine("Let's Start!");


        PlayGame();

        PlayAgain();

        RollDice();

        static void PlayGame()
    {
        while (true)
        {
            int Dice1 = RollDice();
            int Dice2 = RollDice();



            Console.WriteLine("Dice 1 = "   Dice1);
            Console.WriteLine("Dice 2 = "   Dice2);

            Console.WriteLine("I got "   Dice1   " and "   Dice2);

            if (Dice1 % 2 == 0 && Dice2 % 2 == 0)
            {
                Console.WriteLine("Evens are better than odds");
            }
            else
            {
                Console.WriteLine("Odds are still cool.");
            }



        }

        


    }
}

        public static void PlayAgain()
    {
        Console.WriteLine("Do you want to play it again? (Yes or No)");
        string answer = Console.ReadLine();

        if (answer == "Yes")
        {
            PlayGame();
        } else
        {
            break;
        }
    }

        public static Random random = new Random();

        public static int RollDice()
        {
        int Dice = random.Next(2, 4);
        return Dice;
        }   

The console prints out: enter image description here

CodePudding user response:

  1. The first issue is with you're not closing your Main method. Add a closing bracket (}) before the static void PlayGame() line (and then remove the extraneous bracket after the PlayGame() function's definition). You might want to reindent your code (that would have made the error glaringly obvious) - there's surely a menu item for that in your IDE.
  2. Secondly, you can't use break when you're not in a loop; you'd use return to "break out" of a function.
  3. Thirdly, you have an infinite loop without break in your PlayGame() method; you'll never get to that PlayAgain() call.

CodePudding user response:

I sense that you are learning so here I give you something similar to what you appear to be trying. I will annotate with comments in the code but here is a basic rundown:

  • in the static void Main() you output some text first
  • Then in my version I create a boolean variable doIt and set it to "true" so that I can use that in the loop; looping until it is no longer true - and since I set it to true initially it does one "game" play with PlayGame();
  • Next I call the PlayAgain method which I changed to return a boolean from your players answer, setting "doIt" to the value: doIt = PlayAgain();
  • It starts the while loop again and if doIt is still true, plays again, otherwise it goes to the next statement (the "Goodbye") then returns exiting the program.

I formatted a bit and here is everything with LONG comments:

using System;

public class Program
{
    static void Main()
    {
        Console.WriteLine("Hey! Welcome to Tina's Dice Game");
        Console.WriteLine("Let's Start!");
        bool doIt = true;
        while (doIt)
        {
            PlayGame();
            doIt = PlayAgain();
        }

        Console.WriteLine("Goodbye!");
        return;
    }

    // here you had PayGame inside Main() so I left it there
    // the main difference is I now call it ONLY from Main()
    // since the other public methods cannot "see" it
    // this fixes one of the issues
    static void PlayGame()
    {
        bool doingWell = true;
        while (doingWell)
        {
            int dice1 = RollDice();
            int dice2 = RollDice();
            Console.WriteLine("Dice 1 = "   dice1);
            Console.WriteLine("Dice 2 = "   dice2);
            Console.WriteLine("I got "   dice1   " and "   dice2);
            doingWell = (dice1 % 2 == 0 && dice2 % 2 == 0);
            if (doingWell)
            {
                Console.WriteLine("Evens are better than odds");
            }
            else
            {
                Console.WriteLine("Odds are still cool.");
            }
        }
    }

    // public method, this returns a boolean from the check:  answer == "Yes"
    // goAgain is set to the true/false value of the condition it checked,
    // returning true only when "Yes" is the answer
    //  Anything else like "YES","yes", "yep", or "no" returns false 
    // since this is a case sensitive comparison
    // reference: https://docs.microsoft.com/en-us/dotnet/csharp/how-to/compare-strings
    public static bool PlayAgain()
    {
        Console.Write("Do you want to play it again? (Yes or No)");
        
        string answer;
        answer = Console.ReadLine();
        Console.WriteLine($"You said:{answer}");
        var goAgain = answer == "Yes";
        return goAgain;
    }

    // I put the "random" in the roll dice method so we create a new one each time    
    public static int RollDice()
    {
        Random random = new Random();
        int dice = random.Next(2, 4);
        return dice;
    }
}
  •  Tags:  
  • c#
  • Related