C Chen's simple number guessing game

Keywords: C# less

Rules of the game:

A random number is generated by the system. The player has the chance to guess the number three times. If the number is guessed within three times, the player will get it right. Otherwise, Game Over!

 

Code design Description:

1. First, design a simple welcome interface, and prompt the player whether to start the game;

        public void Rule()
        {
            string symbol;
            bool flag = false;
            Console.WriteLine("***********************************************");
            Console.WriteLine("------------Welcome to the number guessing game!-------------");
            Console.WriteLine("We will randomly generate a number from 1 to 10 for players to guess!");
            Console.WriteLine("  Take a look at your luck and offer you three guesses!");
            Console.WriteLine("\tY--Start the game\t\tN--Quit game");
            Console.WriteLine("***********************************************");
            Console.Write("Whether to enter the game:");
            symbol = Convert.ToString(Console.ReadLine());
            while (flag == false)
            {
                switch (symbol)
                {
                    case "Y":
                        Console.Clear();
                        Console.WriteLine("The game begins!");
                        flag = true;
                        break;
                    case "N":
                        Console.WriteLine("Quit the game!");
                        Console.ReadKey();
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid symbol entered!");
                        Console.Write("Whether to enter the game:");
                        symbol = Convert.ToString(Console.ReadLine());
                        break;
                }
            }           
        }

 

2. A random number is automatically generated by the system;

        public int SetRandom()
        {
            int number;

            var random = new Random();
            number = random.Next(1,10); // The random number obtained by this method is greater than or equal to 1 and less than 10;
            return number;
        }

 

3. Enter a value by the player and compare it with the random number generated by the system to determine whether it is the same. If it is the same, it means that the player guesses right. Otherwise, continue to guess until three opportunities are used and feedback Game Over!

public void Guess(int num)
        {
            int number;
            int count = 0;
            Console.WriteLine("Please enter a guess:");
            number = Convert.ToInt32(Console.ReadLine());

            while (num <= 10)
            {
                if (number == num)
                {
                    Console.WriteLine("Congratulations, you're right!");
                    break;
                }
                else
                {
                    count++; // Get player input times
                    Console.WriteLine("Dear. Wrong guess.! You use{0}Second chance\n", count);

                    if (count >= 3) //Judge whether the player has entered three times
                    {
                        Console.WriteLine("You've run out of guesses, Game Over!");
                        break;
                    }
                    Console.WriteLine("Please enter the guess value again:");
                    number = Convert.ToInt32(Console.ReadLine());

                }

            }

        }

 

Complete code

using System;

namespace GuessNumberApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var guessNumber = new GuessNumber();
            var ruleExplain = new RuleExplain();

            ruleExplain.Rule();
            int number = guessNumber.SetRandom();
            guessNumber.Guess(number);

            Console.ReadKey();
        }
    }
    class RuleExplain
    {
        public void Rule()
        {
            string symbol;
            bool flag = false;
            Console.WriteLine("***********************************************");
            Console.WriteLine("------------Welcome to the number guessing game!-------------");
            Console.WriteLine("We will randomly generate a number from 1 to 10 for players to guess!");
            Console.WriteLine("  Take a look at your luck and offer you three guesses!");
            Console.WriteLine("\tY--Start the game\t\tN--Quit game");
            Console.WriteLine("***********************************************");
            Console.Write("Whether to enter the game:");
            symbol = Convert.ToString(Console.ReadLine());
            while (flag == false)
            {
                switch (symbol)
                {
                    case "Y":
                        Console.Clear();
                        Console.WriteLine("The game begins!");
                        flag = true;
                        break;
                    case "N":
                        Console.WriteLine("Quit the game!");
                        Console.ReadKey();
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid symbol entered!");
                        Console.Write("Whether to enter the game:");
                        symbol = Convert.ToString(Console.ReadLine());
                        break;
                }
            }           

        }
    }

    class GuessNumber
    {
        public int SetRandom()
        {
            int number;

            Random random = new Random();
            number = random.Next(1, 10); // The random number obtained by this method is greater than or equal to 1 and less than 10;
            return number;
        }

        public void Guess(int num)
        {
            int number;
            int count = 0;
            Console.WriteLine("Please enter a guess:");
            number = Convert.ToInt32(Console.ReadLine());

            while (num <= 10)
            {
                if (number == num)
                {
                    Console.WriteLine("Congratulations, you're right!");
                    break;
                }
                else
                {
                    count++; // Get player input times
                    Console.WriteLine("Dear. Wrong guess.! You use{0}Second chance\n", count);

                    if (count >= 3) //Judge whether the player has entered three times
                    {
                        Console.WriteLine("You've run out of guesses, Game Over!");
                        break;
                    }
                    Console.WriteLine("Please enter the guess value again:");
                    number = Convert.ToInt32(Console.ReadLine());

                }

            }

        }
    }
}

Posted by MadnessRed on Thu, 02 Apr 2020 15:10:59 -0700