Rahmat Cahyo
Rahmat Cahyo

Reputation: 25

showing random grid c#

I have a problem to showing random a few grid in windows phone. I created my own code, it works fine but its not simple. Maybe there is a simpler way to show a random grid in windows phone.

This is what I need to my app:

I want to click a button to show a random grid. When I click again, it should show another grid and hide the previous grid. It should now show any grid twice.

This is my own code:

List<int> number = new List<int> { 1,2,3,4,5 }; //create list
private void Button_Click_2(object sender, RoutedEventArgs e)
{
    int numberrandom;
    Random bsd = new Random();
    if (number.Count > 0) // get random number from (numberlist) without repetition
    {
        int fIndex = bsd.Next(0, number.Count); 

        numberrandom = number[fIndex]; 
        txtbox1.Text = numberrandom.ToString(); // show random number at txtbox
        number.RemoveAt(fIndex);            
    }
    else 
    {
        messagebox.show("no more grid show");
    }

    int a = Convert.ToInt32(txtbox1.Text); // convert number txtbox from string to int
    int val = a;
    switch (val)
    {
        case 1: //show grid 1
            grid1.Visibility = Visibility.Visible; 
            grid2.Visibility = Visibility.Collapsed;
            grid3.Visibility = Visibility.Collapsed;
            grid4.Visibility = Visibility.Collapsed;
            grid5.Visibility = Visibility.Collapsed;
        break;
        case 2: //show grid 2
            grid1.Visibility = Visibility.Collapsed;
            grid2.Visibility = Visibility.Visible;
            grid3.Visibility = Visibility.Collapsed;
            grid4.Visibility = Visibility.Collapsed;
            grid5.Visibility = Visibility.Collapsed;
            break;
        case 3: //show grid 3
            grid1.Visibility = Visibility.Collapsed;
            grid2.Visibility = Visibility.Collapsed;
            grid3.Visibility = Visibility.Visible;
            grid4.Visibility = Visibility.Collapsed;
            grid5.Visibility = Visibility.Collapsed;
        break;
        case 4: //show grid 4
            grid1.Visibility = Visibility.Collapsed;
            grid2.Visibility = Visibility.Collapsed;
            grid3.Visibility = Visibility.Collapsed;
            grid4.Visibility = Visibility.Visible;
            grid5.Visibility = Visibility.Collapsed;
        break;
        case 5: //show grid 5
            grid1.Visibility = Visibility.Collapsed;
            grid2.Visibility = Visibility.Collapsed;
            grid3.Visibility = Visibility.Collapsed;
            grid4.Visibility = Visibility.Collapsed;
            grid5.Visibility = Visibility.Visible;
        break;
    }

Upvotes: 1

Views: 154

Answers (3)

Zachary Yates
Zachary Yates

Reputation: 13386

There are a couple of improvements you can make:

List<Grid> grids = new List<Grid> { grid1, grid2, grid3, gridN ... };
int gridIndex = 0;

void Load() {
    grids.Shuffle(); // use the extension method linked below
}

void Button_Click_2(object sender, RoutedEventArgs e) {
    if (gridIndex >= grids.Count)
        return;

    if (gridIndex > 0) {
        grids[gridIndex - 1].Visibility = Visibility.Collapsed;
    }
    grids[gridIndex].Visibility = Visibility.Visible;
    gridIndex++;
}

Do you need to display the original grid number? If not, the code above is much simpler. Here is the Shuffle() extension method, I got it from this answer:

public static void Shuffle<T>(this IList<T> list)  
{  
    Random rng = new Random();  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}

Upvotes: 1

David P
David P

Reputation: 2093

I think what you want to do is just to random sort your list of integers once before you begin.

List<int> lNumbers = New List<int> {1,2,3,4,5};
var rnd = New Random();
lNumbers = lNumbers.OrderBy(ob => rnd.Next()).ToList();

Then just keep an index counter variable, and display the next one in the list each time the button is clicked.

Although I am not too familiar with Winodws Phone, I would think you could use FindControl in order to get the right gridview control to hide, and the right one to unhide.

Hope this helps.

:) David

PS - The only thing I don't like about Mr Yate's answer is:

foreach(var grid in grids) {
    grid.Visibility = Visibility.Collapsed;
}
grids[gridIndex].Visibility = Visibility.Visible;

You don't need to set all of the grids visibility to Collpased. Only the last one:

if (gridIndex > 0) {
    grids[gridIndex - 1].Visibility = Visibility.Collapsed;
}
grids[gridIndex].Visibility = Visibility.Visible;

Upvotes: 1

Kevin Gosse
Kevin Gosse

Reputation: 39007

Your code can indeed be simplified. For instance, you can put all your grids in a list and loop on it to set the visibility:

List<int> number = new List<int> { 0, 1, 2, 3, 4 }; //create list
private void Button_Click_2(object sender, RoutedEventArgs e)
{
    int numberrandom;
    Random bsd = new Random();

    if (number.Count > 0) // get random number from (numberlist) without repetition
    {
        int fIndex = bsd.Next(0, number.Count); 

        numberrandom = number[fIndex]; 
        txtbox1.Text = (numberrandom + 1).ToString(); // show random number at txtbox
        number.RemoveAt(fIndex);
    }
    else 
    { 
        MessageBox.Show("no more grid show");
    }

    var grids = new List<Grid> { grid1, grid2, grid3, grid4, grid5 };

    for (int i = 0; i < grids.Count; i++)
    {
        grids[i].Visibility = i == numberrandom ? Visibility.Visible : Visibility.Collapsed;
    }
}

Upvotes: 1

Related Questions