Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shuffle a list [duplicate]

Tags:

c#

list

shuffle

Using C# to write a memory game. So I have a list of 54 images. I can get another list to grab eight of them images at random. I then want to add them eight images from my second list to another list, twice for each image, simple enough.

Now I want to shuffle the list and I am a little stuck. The reason I want to shuffle the list is because I want the pictures to be random each time the game (memory) is loaded or button(restart) is clicked... Here is what I have for this issue:

private void Form2_Load(object sender, EventArgs e)
{
    //generate random number
    Random r = new Random();

    //play music via media player
    axWindowsMediaPlayer1.uiMode = "none";
    axWindowsMediaPlayer1.URL = ("Theme.mp3");            

    //create limage list
    List<Image> imagesEasy;
    //populate the 'imageEasy' list
    imagesEasy = new List<Image>();
    imagesEasy.Add(Image.FromFile(@"Baraka01.gif"));
    imagesEasy.Add(Image.FromFile(@"Baraka02.gif"));
    imagesEasy.Add(Image.FromFile(@"CyberSubZero01.gif"));
    imagesEasy.Add(Image.FromFile(@"CyberSubZero02.gif"));
    imagesEasy.Add(Image.FromFile(@"Cyrax01.gif"));
    imagesEasy.Add(Image.FromFile(@"Cyrax02.gif"));
    imagesEasy.Add(Image.FromFile(@"Ermac01.gif"));
    imagesEasy.Add(Image.FromFile(@"Ermac02.gif"));
    imagesEasy.Add(Image.FromFile(@"Jade01.gif"));
    imagesEasy.Add(Image.FromFile(@"Jade02.gif"));
    imagesEasy.Add(Image.FromFile(@"Jax01.gif"));
    imagesEasy.Add(Image.FromFile(@"Jax01.gif"));
    imagesEasy.Add(Image.FromFile(@"JohnnyCage01.gif"));
    imagesEasy.Add(Image.FromFile(@"JohnnyCage02.gif"));
    imagesEasy.Add(Image.FromFile(@"Kabal01.gif"));
    imagesEasy.Add(Image.FromFile(@"Kabal02.gif"));
    imagesEasy.Add(Image.FromFile(@"Kano01.gif"));
    imagesEasy.Add(Image.FromFile(@"Kano02.gif"));
    imagesEasy.Add(Image.FromFile(@"Katana01.gif"));
    imagesEasy.Add(Image.FromFile(@"Katana02.gif"));
    imagesEasy.Add(Image.FromFile(@"Kratos01.gif"));
    imagesEasy.Add(Image.FromFile(@"Kratos02.gif"));
    imagesEasy.Add(Image.FromFile(@"KungLau01.gif"));
    imagesEasy.Add(Image.FromFile(@"KungLau02.gif"));
    imagesEasy.Add(Image.FromFile(@"LiuKang01.gif"));
    imagesEasy.Add(Image.FromFile(@"LiuKang02.gif"));
    imagesEasy.Add(Image.FromFile(@"Mileena01.gif"));
    imagesEasy.Add(Image.FromFile(@"Mileena02.gif"));
    imagesEasy.Add(Image.FromFile(@"NightWolf01.gif"));
    imagesEasy.Add(Image.FromFile(@"NightWolf02.gif"));
    imagesEasy.Add(Image.FromFile(@"NoobSaibot01.gif"));
    imagesEasy.Add(Image.FromFile(@"NoobSaibot02.gif"));
    imagesEasy.Add(Image.FromFile(@"QuanChi01.gif"));
    imagesEasy.Add(Image.FromFile(@"QuanChi01.gif"));
    imagesEasy.Add(Image.FromFile(@"Raiden01.gif"));
    imagesEasy.Add(Image.FromFile(@"Raiden02.gif"));
    imagesEasy.Add(Image.FromFile(@"Scorpian01.gif"));
    imagesEasy.Add(Image.FromFile(@"Scorpian02.gif"));
    imagesEasy.Add(Image.FromFile(@"Sektor01.gif"));
    imagesEasy.Add(Image.FromFile(@"Sektor02.gif"));
    imagesEasy.Add(Image.FromFile(@"ShangTsung01.gif"));
    imagesEasy.Add(Image.FromFile(@"ShangTsung02.gif"));
    imagesEasy.Add(Image.FromFile(@"Sheeva01.gif"));
    imagesEasy.Add(Image.FromFile(@"Sheeva02.gif"));
    imagesEasy.Add(Image.FromFile(@"Sindel01.gif"));
    imagesEasy.Add(Image.FromFile(@"Sindel02.gif"));
    imagesEasy.Add(Image.FromFile(@"Smoke01.gif"));
    imagesEasy.Add(Image.FromFile(@"Smoke02.gif"));
    imagesEasy.Add(Image.FromFile(@"Sonya01.gif"));
    imagesEasy.Add(Image.FromFile(@"Sonya02.gif"));
    imagesEasy.Add(Image.FromFile(@"Stryker01.gif"));
    imagesEasy.Add(Image.FromFile(@"Stryker02.gif"));
    imagesEasy.Add(Image.FromFile(@"SubZero01.gif"));
    imagesEasy.Add(Image.FromFile(@"SubZero02.gif"));

    List<Image> imagesReady;
    //populate the 'imageReady' list to fill remaining 8 pictureBoxes
    imagesReady = new List<Image>();
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);
    imagesReady.Add(imagesEasy[r.Next(54)]);

    List<Image> imagesFinal;
    //populate the 'imageFinal' list to fill remaining 8 pictureBoxes
    imagesFinal = new List<Image>();
    imagesFinal.Add(imagesReady[0]);
    imagesFinal.Add(imagesReady[1]);
    imagesFinal.Add(imagesReady[2]);
    imagesFinal.Add(imagesReady[3]);
    imagesFinal.Add(imagesReady[4]);
    imagesFinal.Add(imagesReady[5]);
    imagesFinal.Add(imagesReady[6]);
    imagesFinal.Add(imagesReady[7]);
    imagesFinal.Add(imagesReady[0]);
    imagesFinal.Add(imagesReady[1]);
    imagesFinal.Add(imagesReady[2]);
    imagesFinal.Add(imagesReady[3]);
    imagesFinal.Add(imagesReady[4]);
    imagesFinal.Add(imagesReady[5]);
    imagesFinal.Add(imagesReady[6]);
    imagesFinal.Add(imagesReady[7]);

    //
    //need to add or call shuffle method here before assigning pictures to pictureBoxes
    //

    //assign pictures to pictureBoxes from the imagesFinal list
    pictureBox1.Image = imagesFinal[0];
    pictureBox2.Image = imagesFinal[1];
    pictureBox3.Image = imagesFinal[2];
    pictureBox4.Image = imagesFinal[3];
    pictureBox5.Image = imagesFinal[4];
    pictureBox6.Image = imagesFinal[5];
    pictureBox7.Image = imagesFinal[6];
    pictureBox8.Image = imagesFinal[7];
    pictureBox9.Image = imagesFinal[8];
    pictureBox10.Image = imagesFinal[9];
    pictureBox11.Image = imagesFinal[10];
    pictureBox12.Image = imagesFinal[11];
    pictureBox13.Image = imagesFinal[12];
    pictureBox14.Image = imagesFinal[13];
    pictureBox15.Image = imagesFinal[14];
    pictureBox16.Image = imagesFinal[15];
}
like image 664
Daniel Lucas Avatar asked Aug 27 '12 06:08

Daniel Lucas


People also ask

How do you shuffle elements in a list?

The shuffle() method takes a sequence, like a list, and reorganize the order of the items. Note: This method changes the original list, it does not return a new list.

How do I shuffle two lists?

Method : Using zip() + shuffle() + * operator In this method, this task is performed in three steps. Firstly, the lists are zipped together using zip(). Next step is to perform shuffle using inbuilt shuffle() and last step is to unzip the lists to separate lists using * operator.

How do you scramble a list in Python?

In Python, you can shuffle (= randomize) a list, string, and tuple with random. shuffle() and random. sample() . random.

How do I shuffle a list of strings in Python?

Shuffle List in Python. List is an ordered sequence of items. You can shuffle these items using shuffle() function of random module. shuffle() function takes a sequence and shuffles the order of elements.


1 Answers

This can easily be accomplished using linq OrderBy with Random.

var rand = new Random();
var randomList = imagesEasy.OrderBy (x => rand.Next()).ToList();
like image 76
Magnus Avatar answered Oct 20 '22 07:10

Magnus