限制玩家可以使用的提示数量:c#中的数字猜谜游戏

本文关键字:数字 游戏 玩家 可以使 提示 | 更新日期: 2023-09-27 17:50:26

我正在设计一款数字猜谜游戏,即从主机读取4位数。计算机随机生成4位数,用户尝试猜测数字及其正确顺序。程序以0A0B或1A3B或4A0B等格式返回答案,如果您猜对的数字正确并且它的位置也正确,则返回a,如果您猜对的数字正确但它的位置错误,则返回B。

我想实现一个提示系统,当用户输入单词"hint"时提供提示。如何限制用户输入单词"hint"的次数?这是目前为止的代码

static void Main(string[] args)
{
    while (true)
    {
            Console.WriteLine("********************************");
            Console.WriteLine("What number do you think it is ?");
            Console.WriteLine("********************************");
            Console.WriteLine();
            bool GameOver = false;
            int[] targetNumber = GenerateRandomNumber(); // Generates the Random Number
            while (!GameOver)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine(); // Gets an answer from User
                if (answer.ToLower() == "hint") // Designed to Provide a hint.
                {
                    int Rando = GenerateRandomNumberforHint(); // function I created
                   // Console.WriteLine("The Random Number generated was {0}",Rando);
                    if (Rando == 0)
                    {
                        Console.WriteLine("{0}XXX", targetNumber[Rando]);
                    }
                    else if (Rando == 1)
                    {
                        Console.WriteLine("X{0}XX", targetNumber[Rando]);
                    }
                    else if (Rando == 2)
                    {
                        Console.WriteLine("XX{0}X", targetNumber[Rando]);
                    }
                    else if (Rando == 3)
                    {
                        Console.WriteLine("XXX{0}", targetNumber[Rando]);
                    }
                    continue;
                }

           /* Code to check if the number is correct */     
            Console.ReadLine();
        }
    }

我是否需要创建一个数据结构,如果我需要,我如何实现它?我对C Sharp非常陌生,所以任何帮助都会很感激

限制玩家可以使用的提示数量:c#中的数字猜谜游戏

我认为这正是你想要的:)

int hitcounter = 0;
int lastHint = -1;
while (!GameOver)
{ 
    Console.Write("Answer: ");
    string answer = Console.ReadLine();
    if (answer.ToLower() == "hint")
    {
        if (hitcounter < 10)
        {  
                       //preveintg double hint. something like that. 
                       //you can use later array or list if you need more hint
                       int hint = generaterandomhints();
                       while (hint == lastHint)
                       { 
                           hint = generaterandomhints();
                       }
           hitcounter++;
           //write the hints
        }
        else
        {
            //write you reached the maximum of the hints
        }
    }
}