2012-11-01 130 views
-2

我有兩個問題,我不知道如何解決。c#擲骰子(我<= maxRolls)

diceThrow()應該隨機擲出一個骰子,並提出一個答案1-6多次,但只有一個1-6的答案,只有這樣做。即(6,6,6,6,6,6等)

rollDice(),我不知道如果我只是不好界定「i」或maxRolls,但它應該是,當i > maxRolls,程序應該結束並重置。

任何有關如何解決這些問題的建議非常感謝,謝謝!

//somewhere else in code 
    int maxRolls = RollsNumber(); 
    int throwresult = diceThrow(); 
    int i; 
//******************************* 

    private void rollButton_Click(object sender, EventArgs e) 
    { 
     rollDice(); 
     wagerTextBox.Text = null; 
     wagerTextBox.Text = scoreTextBox.Text; 
     diceThrow(); 

     MessageBox.Show(Convert.ToString(throwresult)); 

     if (maxRolls < i) 
     { 
      MessageBox.Show("You got too greedy."); 

      //reset the form 

     } 
    } 
    // Decides the maximum number of rolls before the player loses 
    static public int RollsNumber() 
    { 
     Random rolls = new Random(); 
     return rolls.Next(1, 10); 

    } 
    // Throws the dice 
    static public int diceThrow() 
    { 
     Random dice = new Random(); 
     return dice.Next(1, 7); 
    } 
    private void rollDice() 
    { 
     for (i = 0; i <= maxRolls; i++) 
     { 

       int wager = Convert.ToInt32(wagerTextBox.Text); 


       int score = wager * 100; 


       scoreTextBox.Text = Convert.ToString(score); 



     } 
    } 

}}

+4

不要創建一個新的隨機。關於爲什麼閱讀隨機文檔。請搜索真正的問題**「[C#]複製隨機」**找到許多SO重複。 -1以鼓勵使用搜索功能。 – 2012-11-01 01:30:41

+0

好吧,我會那樣做的,謝謝! –

+0

繼續http://stackoverflow.com/questions/13168977/for-loop-dice-roll-and-textbox-text-updating-troubles-c-sharp – lboshuizen

回答

0

您使用的是帶有隨機相同的種子。

作爲MSDN狀態在Random class

從種子值的隨機數生成開始。如果重複使用相同的種子,則生成相同的一系列數字。產生不同序列的一種方法是使種子值與時間相關,從而與每個新的Random實例產生不同的序列。

你的情況一個簡單的方法是不要每次都創建新的隨機數。

// Throws the dice 
static Random diceRandom = new Random(); 
static public int diceThrow() 
{ 
    return diceRandom .Next(1, 7); 
} 
+0

爲了得到這個工作,我將第一行(因爲它不會像現在這樣工作)更改爲'public static Random diceRandom = new Random();'但它仍然播種同樣,我在錯誤的方向移動? –

+0

另外,我的問題的第二部分的任何想法? maxRolls似乎仍然與我無關。 –

+0

@SethE我是什麼?因爲它從來沒有設置將始終爲0. – Rohit