본문 바로가기

C# 개인 공부 기록용/개인 실습

숫자 퀴즈 게임 (Number Quiz Game)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Number_Quiz
{
    class Computer
    {
        Random r = new Random();
        private int number_quiz;

        public int number_quiz_gs
        {
            get
            {
                number_quiz = r.Next(1, 101);
                return number_quiz;
            }

            set
            {
                number_quiz = value;
            }
        }


    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Number_Quiz
{
    class User
    {
        private int user_num;

        public int user_num_gs
        {
            get
            {
                return user_num;
            }

            set
            {
                user_num = value;
            }
        }

        public void user()
        {
           user_num = Int32.Parse(Console.ReadLine());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Number_Quiz
{
    class Quiz_Game
    {
        public void Q_Gam()
        {
            int chance = 1;
            int count = 1;
            int i = 1;
            int user_num = 0;
            User user = new User();

            while (chance == 1)
            {
                Computer com = new Computer();
                int com_num = com.number_quiz_gs;

                Console.WriteLine("Guess the correct number from 1 to 100!");
                Console.WriteLine("You are given 8 chances! Good luck!");

                do
                {
                    try
                    {
                        for (i = 1; i <= 8; i++)
                        {
                            do
                            {
                                Console.Write("--> " + count + " Chance : ");
                                user.user();
                                user_num = user.user_num_gs;

                                if (user_num > 100)
                                {
                                    Console.WriteLine("");
                                    Console.WriteLine("--> Error : Please enter only 1 to 100!");
                                }

                            } while (user_num > 100);

                            if (user_num < com_num)
                            {
                                Console.WriteLine("--> Result : It's bigger than " + user_num);
                                Console.WriteLine("");
                                count += 1;
                            }

                            else if (user_num > com_num)
                            {
                                Console.WriteLine("--> Result : It's less than " + user_num);
                                Console.WriteLine("");
                                count += 1;
                            }

                            else if (user_num == com_num)
                            {
                                break;
                            }
                        }

                        Console.WriteLine("");
                        Console.WriteLine("");
                        Console.WriteLine("");

                        Console.WriteLine("Correct Number : " + com_num);

                        if (user_num == com_num)
                        {
                            Console.WriteLine("--> Result : WIN");
                        }

                        else if (i >= 8)
                        {
                            Console.WriteLine("--> Result : GAME OVER");
                        }

                        Console.WriteLine("Do you want to restart the game?");
                        Console.WriteLine("[1] YES");
                        Console.WriteLine("[2] NO");
                        Console.Write("--> Choice : ");
                        chance = Int32.Parse(Console.ReadLine());
                        Console.WriteLine("");

                        if (chance == 2)
                        {
                            Console.WriteLine("");
                            Console.WriteLine("Let's end the game. Thank you.");
                            Environment.Exit(0);
                        }
                        else if (chance == 1)
                        {
                            count = 1;
                            break;
                        }
                    }
                    catch
                    {
                        Console.WriteLine("");
                        Console.WriteLine("--> Error : Please enter a number only!");
                    }
                } while (true);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Number_Quiz
{
    class Program
    {
        static void Main(string[] args)
        {
            Quiz_Game qg = new Quiz_Game();
            qg.Q_Gam();
        }
    }
}