如何随机播放列表

时间:2012-08-27 06:27:54

标签: c# list shuffle

使用C#编写记忆游戏。所以我列出了54张图片。我可以得到另一个列表来随机抓取其中的八个图像。然后我想将他们的第二个列表中的八个图像添加到另一个列表中,每个图像两次,这很简单。

现在我想洗牌,我有点卡住了。我想要改变列表的原因是因为我希望每次加载游戏(内存)或点击按钮(重启)时图片都是随机的......以下是我对此问题的看法:

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

4 个答案:

答案 0 :(得分:6)

这可以使用linq OrderByRandom轻松完成。

var rand = new Random();
var randomList = imagesEasy.OrderBy (x => rand.Next()).ToList();

答案 1 :(得分:0)

答案 2 :(得分:0)

感谢大家的帮助,我最后使用一个填充了随机数的数组,通过一个带有嵌套while循环的for循环,确保我没有重复。

        int numbA;
        int[] chosenA = new int[8];
        for (int i = 0; i < 8; i++)
        {
            numbA = r.Next(9);

            for (int b = 0; b < 8; b++)
            {
                while (chosenA.Contains(numbA) == true)
                {
                    numbA = r.Next(9);
                    b = 0;
                }
            }
            chosenA[i] = numbA;
        }

然后使用数组值从列表中调用图片。

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

为确保图片随机放入图片框中,我再次使用另一个for循环,嵌套while循环生成更高的随机数。

        pictureBox1.Image = imagesFinal[chosenB[0] -1 ];
        pictureBox2.Image = imagesFinal[chosenB[1] -1 ];
        pictureBox3.Image = imagesFinal[chosenB[2] -1 ];
        pictureBox4.Image = imagesFinal[chosenB[3] -1 ];
        pictureBox5.Image = imagesFinal[chosenB[4] -1 ];
        pictureBox6.Image = imagesFinal[chosenB[5] -1 ];
        pictureBox7.Image = imagesFinal[chosenB[6] -1 ];
        pictureBox8.Image = imagesFinal[chosenB[7] -1 ];
        pictureBox9.Image = imagesFinal[chosenB[8] -1 ];
        pictureBox10.Image = imagesFinal[chosenB[9] -1 ];
        pictureBox11.Image = imagesFinal[chosenB[10] -1 ];
        pictureBox12.Image = imagesFinal[chosenB[11] -1 ];
        pictureBox13.Image = imagesFinal[chosenB[12] -1 ];
        pictureBox14.Image = imagesFinal[chosenB[13] -1 ];
        pictureBox15.Image = imagesFinal[chosenB[14] -1 ];
        pictureBox16.Image = imagesFinal[chosenB[15] -1 ];

答案 3 :(得分:-1)

您可以尝试以下内容:

List<int> list = new List<int>();
Random rnd = new Random(SEED);
for(int i = 0; i <  15; i++)
{
int nextValue = rnd.Next(MIN,MAX);
while(list.Contains(nextValue))
      nextValue = rnd.Next(MIN,MAX);
}