2

I would like to display four images at the same time and upon form load the images switch places. Currently, images will appear in different numbers, for example: 1 image will appear or 2 images, etc up 4. I also want to ensure that no duplicates will appear.

Code from Form1_Load:

PictureBox[] boxes = new PictureBox[4];

boxes[0] = pictureBox0;
boxes[1] = pictureBox1;
boxes[2] = pictureBox2;
boxes[3] = pictureBox3;

for (int i = 0; i < boxes.Length; i++)
{
    int switcher = r.Next(0, 5);

    switch (switcher)
    {
        case 0:
            { boxes[i].Image = Properties.Resources.dog0; } break;
        case 1:
            { boxes[i].Image = Properties.Resources.dog1; } break;
        case 2:
            { boxes[i].Image = Properties.Resources.dog2; } break;
        case 3:
            { boxes[i].Image = Properties.Resources.dog3; } break;
    }
}

enter image description here Two examples given above as to what currently happens.

Update - Working

The program now moves images around upon Load and there are no duplicates :)

List<Bitmap> resources = new List<Bitmap>();
resources.Add(Properties.Resources.dog0);
resources.Add(Properties.Resources.dog1);
resources.Add(Properties.Resources.dog2);
resources.Add(Properties.Resources.dog3);

resources = resources.OrderBy(a => Guid.NewGuid()).ToList();

for (int i = 0; i < resources.Count; i++)
{
    pictureBox0.Image = resources[0];
    pictureBox1.Image = resources[1];
    pictureBox2.Image = resources[2];
    pictureBox3.Image = resources[3];
}

enter image description here enter image description here

Two example given above showing what happens now that it works.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
rah
  • 161
  • 1
  • 3
  • 14

2 Answers2

1

As M.kazem Ahkhary points out you need to shuffle the images:

List<Bitmap> resources = new List<Bitmap>();
resources.Add(Properties.Resources.dog0);
resources.Add(Properties.Resources.dog1);
resources.Add(Properties.Resources.dog2);
resources.Add(Properties.Resources.dog3);

resources = resources.OrderBy(a => Guid.NewGuid()).ToList(); // Dirty but effective shuffle method

pictureBox0.Image = resources[0];
pictureBox1.Image = resources[1];
pictureBox2.Image = resources[2];
pictureBox3.Image = resources[3];
Gecko
  • 1,333
  • 1
  • 14
  • 26
  • Thank you, how do I implement this? Shall I replace anything? – rah Sep 03 '16 at 16:44
  • Made some edits, it was not completely correct. You can normally just paste this code over your provided code – Gecko Sep 03 '16 at 16:48
  • It's giving me errors one from each of the "resources.Add"s about not being able to convert Bitmap to String and the other error is Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) and that "resource" doesn't exist in the current context. – rah Sep 03 '16 at 16:56
  • I updated the answer. The resources list had to be a list of Bitmaps I think and the orderdEnumerable had to be converted to a List. Last, it was resources and not resource for the appointing of the Image properties. – Gecko Sep 03 '16 at 17:00
  • @stefchri, consider using `someRandom.Next(someInt)` instead of `Guid.NewGuid()`. Do you have any reason to use NewGuid and not Random class as a random "generator? – Gian Paolo Sep 03 '16 at 20:49
  • @GianPaolo https://dotnetfiddle.net/KHK5XY shows that Guid.NewGuid() in orderby is more performant. – Gecko Sep 04 '16 at 07:12
  • 1
    @GeckoIT note It can be that NewGuid is "less random" than Random.Next(): sorting a list that is already "more ordered" is quicker. Anyway, both do the trick, I prefer using Random – Gian Paolo Sep 05 '16 at 12:27
  • Random is indeed the best way I concur – Gecko Sep 05 '16 at 17:45
1

The implementation is quite simple. Firstly, you need to shuffle the array and then iterate through it. Fisher–Yates shuffle.

Create a method ShuffleImages as below:

public void ShuffleImages(PictureBox[] img)
{
    Random r = new Random();
    for (int i = 0; i < img.Length - 1; i++)
    {
        int j = r.Next(i, img.Length);
        PictureBox temp = img[j];
        img[j] = img[i];
        img[i] = temp;                
    }
}

and call the method in your Form1_Load event:

private void Form1_Load(object sender, EventArgs e)
{
    PictureBox[] boxes = new PictureBox[4];
    boxes[0] = pictureBox0;
    boxes[1] = pictureBox1;
    boxes[2] = pictureBox2;
    boxes[3] = pictureBox3;

    ShuffleImages(boxes); //call the method

    for (int i = 0; i <= 3; i++)
    {
        switch (i)
        {
            case 0:
                {  boxes[i].Image = Properties.Resources.dog0;  }
                break;
            case 1:
                {  boxes[i].Image = Properties.Resources.dog1;  }
                break;
            case 2:
                {  boxes[i].Image = Properties.Resources.dog2;  }
                break;
            case 3:
                {  boxes[i].Image = Properties.Resources.dog3;  }
                break;
        }
    }
}

                          shuffled_imgs

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32