我在做什麼是生成一個5x5網格填充隨機按鈕。問題是,我正在使用的隨機int沒有足夠頻繁地更新以使其成爲真正的隨機數,它將95%的時間顯示爲一個按鈕類型的整個網格,並在5%的時間內在2個按鈕之間進行分割。我需要隨機選擇每個細胞。這裏是我的代碼隨機int沒有足夠快速更新的目的隨機按鈕創建
private void btnFillGrid_Click(object sender, RoutedEventArgs e) {
stkGameBoardColumnOne.Children.Clear();
stkGameBoardColumnTwo.Children.Clear();
stkGameBoardColumnThree.Children.Clear();
stkGameBoardColumnFour.Children.Clear();
stkGameBoardColumnFive.Children.Clear();
for (int i = 0; i < 25; i++) {
Random rnd = new Random();
Button btnMapCell = new Button();
Potion cellPotion = new Potion("", 0, "");
btnMapCell.Foreground = new SolidColorBrush(Colors.White);
int randomPotion = rnd.Next(0, 4);
if (randomPotion == 0) {
//Potion cellPotion = new Potion("Small Healing Potion", 25, "green");
cellPotion.Name = "Small Healing Potion";
cellPotion.AffectValue = 25;
cellPotion.Color = "green";
btnMapCell.Background = new SolidColorBrush(Colors.Green);
}
else if (randomPotion == 1) {
//Potion cellPotion = new Potion("Medium Healing Potion", 50, "blue");
cellPotion.Name = "Medium Healing Potion";
cellPotion.AffectValue = 50;
cellPotion.Color = "blue";
btnMapCell.Background = new SolidColorBrush(Colors.Blue);
}
else if (randomPotion == 2) {
//Potion cellPotion = new Potion("Large Healing Potion", 100, "purple");
cellPotion.Name = "Large Healing Potion";
cellPotion.AffectValue = 100;
cellPotion.Color = "purple";
btnMapCell.Background = new SolidColorBrush(Colors.Purple);
}
else if (randomPotion == 3) {
//Potion cellPotion = new Potion("Extreme Healing Potion", 200, "red");
cellPotion.Name = "Extreme Healing Potion";
cellPotion.AffectValue = 200;
cellPotion.Color = "red";
btnMapCell.Background = new SolidColorBrush(Colors.Red);
}
btnMapCell.Content = "";
btnMapCell.Width = 75;
btnMapCell.Height = 75;
btnMapCell.Content = cellPotion.Name + "\r\n" + "(" + cellPotion.AffectValue + ")";
if (i >= 0 && i < 5) {
stkGameBoardColumnOne.Children.Add(btnMapCell);
}
else if (i >= 5 && i < 10) {
stkGameBoardColumnTwo.Children.Add(btnMapCell);
}
else if (i >= 10 && i < 15) {
stkGameBoardColumnThree.Children.Add(btnMapCell);
}
else if (i >= 15 && i < 20) {
stkGameBoardColumnFour.Children.Add(btnMapCell);
}
else if (i >= 20 && i < 25) {
stkGameBoardColumnFive.Children.Add(btnMapCell);
}
}
}
下面是一些如何填充網格的照片。
對不起我搜索,但沒有找到任何重複。如果需要,我可以刪除問題。 – kalenpw
你可以,但你不必。它只會幫助將來訪問者引導到其他有用的線程。 –