我在做什么是生成一个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
你可以,但你不必。它只会帮助将来访问者引导到其他有用的线程。 –