0

我正在尝试实现一个猜谜游戏。游戏将生成一个介于 1-100 之间的随机数,用户必须猜测该数字。如果距离用户开始玩游戏的时间过去了 30 秒,游戏将为用户生成一个新的随机数。到目前为止,我设法使用计时器来做到这一点。每次经过 30 秒,我都会调用 myTimer 方法,该方法会重新生成一个新的随机数。我想删除计时器并使用事件处理程序。我遵循了一些教程,但我无法弄清楚它是如何实现的。这是我的代码。//调用游戏

 partial class Program
        {
            static void Main(string[] args)
            {
                IGame game1 = new GuessingGameConsole();
                game1.Run(30000);
            }
    
        }


public interface IGame 
{
    void Run(int replacetime);
}



namespace GuessingGame
{
    partial class Program
    {
        public class GuessingGameConsole : GameBase, IGame
        {


            public void Run(int replacetime)
            {

                Timer x = new Timer(replacetime)
                {
                    AutoReset = true
                };
                x.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer);



                int count;
                int inputNumber;
                string username;
                bool flag;
                var watch = System.Diagnostics.Stopwatch.StartNew();
                char playagain;
                int playerid = 1;

                do
                {

                    var rn = GetRandomNumber();
                    x.Start();
                    count = 0;
                    flag = false;
                    while (count < 10 && flag == false)
                    {

                        try
                        {
                            inputNumber = Convert.ToInt32(Console.ReadLine());
                            if (inputNumber == Convert.ToInt32(rn))
                            {
                                flag = true;
                                x.Stop();
                                watch.Stop();

                                Console.WriteLine("Bingo!!! The Magic number was " + rn);
                                Console.WriteLine("Insert your details for the score board.");
                                Console.WriteLine("Name: ");
                                username = Console.ReadLine();

                                UpdateScoreBoards(playerid, username, count, (int)(watch.ElapsedMilliseconds / 1000), DateTime.Now);
                                watch.Reset();
                                watch.Start();

                            }
                            else if (count < 9)
                            {

                                if (inputNumber > Convert.ToInt32(rn))
                                {
                                    Console.WriteLine("Smaller");
                                }
                                else
                                {
                                    Console.WriteLine("Greater");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Oops, maybe next time.");
                            }
                        }
                        catch (Exception)

                        {
                            Console.WriteLine("Please enter a number");
                        }
                        count++;
                        //Console.WriteLine(count);

                    }
                    x.Stop();

                    Console.WriteLine("Do you want to play again? y/n");


                    playagain = Console.ReadKey().KeyChar;
                    Console.WriteLine();

                    while (playagain != 'y' && playagain != 'Y' && playagain != 'n' && playagain != 'N')
                    {
                        Console.WriteLine();
                        Console.WriteLine("Please type y or n");
                        playagain = Console.ReadKey().KeyChar;

                    }

                } while (playagain == 'y' || playagain == 'Y');

                Console.WriteLine("Bye :D");
            }
        }
    }
}




namespace GuessingGame
{
    partial class Program
    {
        public class GameBase
        {

            public int GetRandomNumber()
            {

               ..
            }

            public void UpdateScoreBoards()
            {
                ..
            }

            public void WriteToJsonFile<Scoreboard>() where Scoreboard : new()
            {
                ..
            }

            public void MyTimer(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("30 seconds passed, a new number is now generated");
                //int count = 0;
                GetRandomNumber();

            }

        }
    }
}
4

0 回答 0