简体   繁体   English

从列表中选择一个随机变量。 如果不是所选的,则选择另一个。

[英]Pick a random random from a list. If it's not the chosen one, pick another one.

I have this Guess the number game. 我有这个猜数字游戏。 The user thinks of a number and the computer will guess it. 用户想到一个数字,计算机就会猜出来。 I have this code but the thing is, every attempt that computer makes there's a chance that the random number will repeat. 我有此代码,但事实是,计算机进行的每次尝试都有可能重复随机数。 I would like to know how to create a random list, the computer will pick a number from the list, if it's not correct I would like to have that number deleted so it will not be picked up again. 我想知道如何创建一个随机列表,计算机将从列表中选择一个数字,如果不正确,我希望删除该数字,这样就不会再次提取它。 I am new in this field so I would really appreciate any help. 我是该领域的新手,因此,我将非常感谢您的帮助。

private void btnStartTheGame_Click(object sender, EventArgs e)
{
    int guessTheNumber = Convert.ToInt32(txtNumberGuess.Text);
    DialogResult dialogResult;
    do
    {
        Random newNumberGenerator = new Random();
        number = newNumberGenerator.Next(0, 10);
        dialogResult = MessageBox.Show("Is number" + number.ToString() + " you are thinking about?", "Answer the question!", MessageBoxButtons.YesNo);
    }while (dialogResult == DialogResult.No);

    MessageBox.Show("Congratulation! You guessed the number!!");       
}
List<int> randomNumbers = Enumerable.Range(0,10).ToList();
DialogResult dialogResult;
Random newNumberGenerator = new Random();
do
{
    int index = newNumberGenerator.Next(0, randomNumbers.Count);
    dialogResult = MessageBox.Show("Is number" + randomNumbers[index] + " you are thinking about?", "Answer the question!", MessageBoxButtons.YesNo);
    if(dialogResult==DialogResult.No)
    {
        randomNumbers.RemoveAt(index);
    }
}
while (dialogResult == DialogResult.No);

Here is a short but complete program which does as you require (I have used a console application rather than forms): 这是一个简短而完整的程序,可根据您的需要进行操作(我使用的是控制台应用程序而不是表格):

private static void Main(string[] args)
{
    string dialogResult = "";
    bool[] alreadyGuessed = new bool[10];
    int guesses = 0;

    Random newNumberGenerator = new Random();
    do
    {
        int number = newNumberGenerator.Next(0, 10);
        if (!alreadyGuessed[number])
        {
            guesses++;
            alreadyGuessed[number] = true;
            Console.WriteLine("Are you thinking of the number " + number.ToString() + "?")
            dialogResult = Console.ReadLine();
        }
    }
    while (dialogResult.ToUpper() != "Y" && guesses < 10);

    if (dialogResult.ToUpper() == "Y")
    {
        Console.WriteLine("I guessed the number!");
    }
    else
    {
        Console.WriteLine("No numbers left!");
    }
    Console.ReadLine();
}

The logic in your original program seemed somewhat backwards; 您原始程序中的逻辑似乎有些倒退。 it wasn't clear whether it was the user or the computer doing the guessing. 目前尚不清楚是用户还是计算机来进行猜测。 This program assumes that you are thinking of a number between 0 and 9 (inclusive) and leaving the computer to try to guess it, but you can obviously adapt it to your needs. 该程序假定您正在考虑一个介于0到9(包括0)之间的数字,并且让计算机尝试猜测它,但是您显然可以根据需要对其进行调整。 I have also omitted any validation logic. 我也省略了任何验证逻辑。

EDIT : As Tarec correctly points out, this doesn't extend to very large number sets. 编辑 :正如Tarec正确指出的那样,这不会扩展到非常大的数字集。 To do that you'd need a proper shuffling algorithm like this: 为此,您需要一个适当的改组算法,如下所示:

public static void Shuffle(int[] array)
{
    var random = new Random();
    for (int i = array.Length; i > 1; i--)
    {
        int j = random.Next(i);
        int temp = array[j];
        array[j] = array[i - 1];
        array[i - 1] = temp;
    }
}

Then you could change your program to this: 然后,您可以将程序更改为此:

private static void Main(string[] args)
{
    string dialogResult = "";
    int size = 10;

    int[] array = Enumerable.Range(0, size).ToArray();
    Shuffle(array);

    for (int i = 0; i < size; i++)
    {
        int number = array[i];
        Console.WriteLine("Are you thinking of the number " + number.ToString() + "?");
        dialogResult = Console.ReadLine();
        if (dialogResult.ToUpper() == "Y")
        {
            break;
        }
    }

    if (dialogResult.ToUpper() == "Y")
    {
        Console.WriteLine("I guessed the number!");
    }
    else
    {
        Console.WriteLine("No numbers left!");
    }
    Console.ReadLine();
}

Now you can change 'size' to whatever you like and the program will work correctly. 现在,您可以将“大小”更改为所需的大小,程序将正常运行。 This is probably overkill for the original poster's needs, though. 但是,对于原始海报的需求而言,这可能是过大的选择。

I modified your code by adding an array of number to be chosen. 我通过添加要选择的数字数组来修改了您的代码。 Then I random only the index of the array. 然后,我只随机化数组的索引。 If the number if not correct, the number is removed from the array and redo again. 如果数字不正确,将从阵列中删除该数字,然后再次重做。

int guessTheNumber = Convert.ToInt32(txtNumberGuess.Text);
DialogResult dialogResult;
ArrayList numberList = new ArrayList();
for(int i=1; i<10; i++)
   numberList.Add(i);
int length = index.Count;
do
{
   Random newNumberGenerator = new Random();
   index = newNumberGenerator.Next(length);
   dialogResult = MessageBox.Show("Is number" + numberList[index].ToString() + " you are thinking about?", "Answer the question!", MessageBoxButtons.YesNo);
   if( dialogResult == DialogResult.No) 
   {
      numberList.RemoveAt(index);
      length = length - 1;
      if(length == 0)
      {
          MessageBox.Show("No number left!!");  
          return;
      }
   }
}while (dialogResult == DialogResult.No);
MessageBox.Show("Congratulation! You guessed the number!!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM