5

使用 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

4 回答 4

18

这可以使用 linq OrderBywith轻松完成Random

var rand = new Random();
var randomList = imagesEasy.OrderBy (x => rand.Next()).ToList();
于 2012-08-27T07:38:08.063 回答
1

尝试实现这个方法:

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

于 2012-08-27T07:35:48.273 回答
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 )]);

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

        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 ];
于 2012-08-27T09:15:14.620 回答
-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);
}
于 2012-08-27T07:37:29.490 回答