2014-04-01 11 views
0

我猜這是猜數字遊戲。用戶會想到一個數字,電腦會猜測它。我有這樣的代碼,但事實是,計算機每次嘗試都有可能重複隨機數。我想知道如何創建一個隨機列表,計算機會從列表中選擇一個數字,如果它不正確,我希望刪除該數字以便它不會被再次拾取。我是這個領域的新人,所以我非常感謝任何幫助。從列表中隨機挑選一個。如果不是所選的那個,請選擇另一個。

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!!");  
} 

回答

0

這裏是一個簡短而完整的程序,它不會因爲你需要(我使用一個控制檯應用程序,而非形式):

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(); 
} 

在原始程序中的邏輯似乎有些倒退;目前還不清楚是用戶還是計算機做猜測。這個程序假設你正在考慮一個介於0和9之間的數字(包括這個數字),並讓計算機試圖猜測它,但你顯然可以根據自己的需要進行調整。我也省略了任何驗證邏輯。

編輯:正如Tarec正確指出的那樣,這不會擴展到非常大的數量集合。要做到這一點你需要一個合適的洗牌算法是這樣的:

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; 
    } 
} 

然後,你可以將程序更改此:

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(); 
} 

現在你可以改變「大小」,以任何你喜歡的和程序將正常工作。不過,這對於原始海報的需求可能是過度的。

+0

這不是最佳解決方案。它是非確定性的,因爲你正在運行你的循環,直到所有的隨機數被使用,這是錯誤的。在每次迭代中最後一位數字的一些近似變體中,您將有1/10的機會實際擊中正確的數字。想象一下,你用1000或1000萬的數字而不是10來運行它,你註定要失敗。 – Tarec

+0

當然。但我們並不是針對1,000或1,000,000個數字運行,而是針對10個運行。這不是一個可擴展的解決方案,但它確實解決了OP的具體問題。 (另外,更改爲一百萬個數字(自動檢查)在大約三秒鐘內運行,所以我認爲'註定失敗'有點誇張!) –

+0

非常感謝。這工作得很好! – MrDevo

0
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); 
+0

我沒有downvote,但我認爲你的答案仍然允許重複條目被選中。 (加上我不認爲'List randomNumbers = Enumerable.Range(0,10);'compiles) –

+0

我不是downvoter,但我想他也想從這些randomNumbers中移除猜測數字;-)否則,它看起來沒問題。 –

+1

這是我的錯,顯然我忘了刪除舊物品。當然dialogResult對象必須在循環之前聲明。此外,這種方法不檢查是否有剩餘數字,用戶可以輕鬆回答「否」10次,然後在'randomNumbers [index]'調用中崩潰。 – Tarec

0

我通過添加要選擇的數組的數組來修改您的代碼。然後我只隨機選擇數組的索引。如果數字不正確,則從陣列中刪除該數字並重新進行重做。

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!!"); 
相關問題