0

This is my code below in an event button click handler. Whenever I click once it will randomize the images. Is there a possible solution for my code to loop once instead of infinite times looping while clicking.

I am doing a picture matching memory game, so therefore it keep randomizing while i click the button.

shuffled01 = dict01.Shuffle();

void btn_Click(object sender, EventArgs e)
{
    foreach (var a in shuffled01)
    {
        button.Name = "a" + a.Key.ToString();
        button.MinHeight = 100;
        button.MinWidth = 100;
        //   button.Name = "a" + a.Key.ToString();
        ImageBrush brush = new ImageBrush();
        BitmapImage bitmap = new BitmapImage();
        button.Background = brush;
        bitmap.BeginInit();
        bitmap.UriSource = new Uri(@"C:\inetpub\wwwroot\istellar 22-10-13\iStellarMobile\iStellarMobile\" 
            + a.Value.ToString().Substring(1), UriKind.Absolute);
        bitmap.ToString();
        bitmap.EndInit();
        brush.ImageSource = bitmap;
    }
}

Thanks in advance.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3044300
  • 45
  • 1
  • 5

3 Answers3

0

It seems that you need to shuffle keys in your dictionary and then iterate them. In this case I'd do it as follows:

void Main()
{
    var dic = new Dictionary<string, string>();

    dic.Add("a", "b");
    dic.Add("c", "s");
    dic.Add("d", "2");
    dic.Add("e", "r");
    dic.Add("f", "g");

    var range = Enumerable.Range(0, dic.Keys.Count()).Shuffle(); 

    var kArr = dic.Keys.ToArray();
    foreach (var num in range) {
        var key = kArr[num];
        Console.WriteLine("{0}, {1}", key, dic[key]);
    }


}

// take shuffling from here : https://stackoverflow.com/questions/5807128/an-extension-method-on-ienumerable-needed-for-shuffling
public static class Ext {
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
    {
        var rng = new Random();
        return source.Shuffle(rng);
    }

    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (rng == null) throw new ArgumentNullException("rng");

        return source.ShuffleIterator(rng);
    }

    private static IEnumerable<T> ShuffleIterator<T>(
        this IEnumerable<T> source, Random rng)
    {
        var buffer = source.ToList();

        for (int i = 0; i < buffer.Count; i++)
        {
            int j = rng.Next(i, buffer.Count);
            yield return buffer[j];

            buffer[j] = buffer[i];
        }
    }
}

Sample results: First run d, 2 / f, g / e, r / c, s / a, b

Second run a, b / e, r / f, g / c, s / d, 2

PS I used shuffling from here An extension method on IEnumerable needed for shuffling

Community
  • 1
  • 1
Rustam
  • 1,766
  • 18
  • 34
0

Is there a possible solution for my code to loop once instead of infinite times looping while clicking.

so you mean you need to run it only once?, doesn't a

private bool isRunned;

would solve it? and then setting it to true after running or you can unsubscribe the event of the button by

button.Click -= click_handler;
DevEstacion
  • 1,947
  • 1
  • 14
  • 28
0

You use break; keyword when you found your matching

foreach (var a in shuffled01)
{
if(CondStatisfied == true)
{
  break;
}
}
taha ahmed
  • 19
  • 6