PictureBox图像随机化 C#

3

我正在开发一个拼图滑块程序,尝试在图片框中随机化图片。我在互联网上进行了一些研究,但没有找到可以使用的示例。以下是我的代码:

        Random r = new Random();

        PictureBox[] picBox = new PictureBox[9];
        picBox[0] = new PictureBox();
        picBox[1] = new PictureBox();
        picBox[2] = new PictureBox();
        picBox[3] = new PictureBox();
        picBox[4] = new PictureBox();
        picBox[5] = new PictureBox();
        picBox[6] = new PictureBox();
        picBox[7] = new PictureBox();
        picBox[8] = new PictureBox();

我有位图数组:
        Bitmap[] pictures = new Bitmap[9];
        pictures[0] = new Bitmap(@"1.1Bright.jpg");
        pictures[1] = new Bitmap(@"1.2Bright.jpg");
        pictures[2] = new Bitmap(@"1.3Bright.jpg");
        pictures[3] = new Bitmap(@"2.1Bright.jpg");
        pictures[4] = new Bitmap(@"2.2Bright.jpg");
        pictures[5] = new Bitmap(@"2.3Bright.jpg");
        pictures[6] = new Bitmap(@"3.1Bright.jpg");
        pictures[7] = new Bitmap(@"3.2Bright.jpg");
        pictures[8] = new Bitmap(@"3.3Dark.jpg");

我试过几种方法,但不知道如何将随机图片[]设置到picBox[]中:
        for(int i=0; i<=8;i++)
        {
            picBox[i].Image= pictures[r.Next(0,9)];
        }

这里的问题是一些图片框,如picBox [1]和picBox [6]是重复的图片。我该如何使它们不重复? 举个例子会更好,谢谢。

2个回答

4

只需填充数组并使用洗牌算法即可。

可以考虑实现为扩展方法:

namespace ExtensionMethods
{
    public static class Extensions
    {
        static Random rng = new Random();

        public static void shuffle<T>(this T[] array)
        {
            // i is the number of items remaining to be shuffled.
            for (int i = array.Length; i > 1; i--)
            {
                // Pick a random element to swap with the i-th element.
                int j = rng.Next(i);  // 0 <= j <= i-1 (0-based array)
                // Swap array elements.
                T tmp = array[j];
                array[j] = array[i - 1];
                array[i - 1] = tmp;
            }
        }

    }
}

调用示例:

使用扩展方法;

namespace ConsoleApplication
{

    static class Program
    {
        static void Main()
        {
            int[] array = new int[] {1,2,3,4,5,6,7,8,9};

            array.Shuffle();
        }
    }
}

哇,谢谢!看来我得学习这个算法了。不过可能需要一些时间。与此同时,有没有一些简单的编码可以让我这个数学比较弱的人开始尝试呢 :X - cheesebunz
这对我来说有点复杂,哈哈,但它确实有效,我理解了逻辑。 - cheesebunz
直觉告诉我们,您需要遍历项目集合,并将每个项目与另一个随机项目交换。为了确保洗牌是完全随机的,没有偏差(即没有一种组合比另一种更可能),您需要确保只与当前项目之后的项目进行交换(或者在大多数实现中向后运行数组以简化数学计算)。不涉及数学问题,这只是您必须信任人们的事情。 - ICR

4
创建一个大小与图片数组相等的布尔型数组。
bool[] usedPictures = new bool[pictures.Length];

将这个数组的值设置为false。现在确定你的随机数,并测试该元素是否被使用,类似于以下内容:
int iCount = 0;
Random random = new Random();
while (iCount < pictures.Length)
{
    int attempt = random.Next(0, pictures.Length);

    //Ensures you will only use an available picture
    if (usedPictures[attempt] == false)
    {            
        picBox[attempt].Image= pictures[iCount];
        doorUsed[attempt] = true;
        iCount++;
    }
}

嘿,Robb,谢谢谢谢谢谢你!它百分之百有效,所有我的图像都被洗牌和随意排列!给你10/10的大拇指:):) - cheesebunz
这对于需要洗牌的少量项目(9)来说很好用,但您应该意识到这个解决方案不可扩展。如果您将来需要洗牌更多的项目,那么最好研究更好的洗牌算法。 - ICR
@ICR,当然。我想既然他的难题如此固定,这个解决方案会以简单的方式完成任务,这也正是他想要的。 - Robb
是的,谢谢大家。当我开发一个包含更多项目的程序时,我会研究更好的算法。谢谢大家,加油 :) - cheesebunz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接