lundi 7 mars 2016

c# Random int not updating quick enough for purpose random button creation [duplicate]

This question already has an answer here:

What I'm doing is generating a 5x5 grid filled with random buttons. The issue is, the random int I am using doesn't update frequently enough to make it truly random it will display the entire grid as one button type 95% of the time and split between 2 buttons 5% of the time. I need each cell to be randomly chosen. Here is my code

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

Here are some pictures of how it populates the grid.

http://ift.tt/1Ypr88F




Aucun commentaire:

Enregistrer un commentaire