Exemplo n.º 1
0
        static void Main(string[] args)
        {
            bool playAgain = true;

            //Asks the user to to play a game.
            Console.WriteLine("Welcome to the Grand Circus Casino! Roll the dice ? (y / n): ");
            string userInput = Console.ReadLine();

            while (playAgain)
            {
                //using try catch to check formatting, it could be only characters that will be allowed y,Y,n,N.
                try
                {
                    char characterUserInput = char.Parse(userInput);

                    if (characterUserInput == 'Y' || characterUserInput == 'y')
                    {
                        Console.WriteLine("How many sides should each die have?");
                        //try catch is used to check formatting that will only allow numbers to be entered.
                        try
                        {
                            int intUserInput = int.Parse(Console.ReadLine());

                            //An objects that refers to an instance of a class.
                            DiceRollerApp firstClass = new DiceRollerApp();
                            firstClass.Rolls();
                        }
                        catch (FormatException)
                        {
                            Console.WriteLine("Only numbers allowed. Try again.");
                            playAgain = true;
                        }
                    }
                    else if (characterUserInput == 'N' || characterUserInput == 'n')
                    {
                        Console.WriteLine("Goodbye.");
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter in the correct format and try again.");
                    playAgain = true;
                }
                //To play again.
                Console.WriteLine("Roll again? (y/n): ");
                string stringUserInput = Console.ReadLine();
                try
                {
                    char charUserInput = char.Parse(stringUserInput);
                    {
                        if (charUserInput == 'y' || charUserInput == 'Y')
                        {
                            playAgain = true;
                        }
                        else if (charUserInput == 'n' || charUserInput == 'N')
                        {
                            Console.WriteLine("Goodbye.");
                            playAgain = false;
                        }
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter in the correct format and try again.");
                    playAgain = true;
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int    numSides, lowestSide = 1, rollOne, rollTwo;
            string sides, zeroChoice, rollChoice, replayChoice;
            bool   repeatSides;

            Console.WriteLine("Hello and welcome to the Dice Roller!");

            do
            {
                repeatSides = true;

                Console.Write("Please specify the number of sides the dice will have: ");
                sides = Console.ReadLine();

                do
                {
                    if (int.TryParse(sides, out numSides))
                    {
                        if (numSides <= 1)
                        {
                            Console.WriteLine("The dice must have two or more sides. Please choose a different number.");
                            sides = Console.ReadLine();
                        }
                        else
                        {
                            repeatSides = false;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid Input");
                        sides = Console.ReadLine();
                    }
                } while (repeatSides);



                Console.Write("Do you want the dice to start from 0? (y/n): ");
                zeroChoice = Console.ReadLine().ToLower();

                while (zeroChoice != "y" && zeroChoice != "n")
                {
                    Console.WriteLine("Invalid Input");
                    zeroChoice = Console.ReadLine().ToLower();
                }

                if (zeroChoice == "y")
                {
                    lowestSide = 0;
                    numSides  -= 1;
                }
                else if (zeroChoice == "n")
                {
                    lowestSide = 1;
                }

                do
                {
                    rollOne = DiceRoll(lowestSide, numSides);
                    rollTwo = DiceRoll(lowestSide, numSides);

                    Console.Write($"Die 1: {rollOne}");
                    Console.WriteLine();
                    Console.Write($"Die 2: {rollTwo}");
                    Console.WriteLine();

                    Console.WriteLine(DiceRollerApp.RollMessage(rollOne, rollTwo));

                    Console.Write("Would you like to reroll these dice? (y/n): ");
                    rollChoice = Console.ReadLine().ToLower();

                    while (rollChoice != "y" && rollChoice != "n")
                    {
                        Console.WriteLine("Invalid Input");
                        rollChoice = Console.ReadLine().ToLower();
                    }
                } while (rollChoice == "y");

                Console.Write("Would you like to change the number of sides? (y/n): ");
                replayChoice = Console.ReadLine().ToLower();

                while (replayChoice != "y" && replayChoice != "n")
                {
                    Console.WriteLine("Invalid Input");
                    replayChoice = Console.ReadLine().ToLower();
                }
            } while (replayChoice == "y");

            Console.WriteLine("Goodbye and thanks for rolling!");
            return;
        }