Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
picturebox图像随机化C#_C#_Picturebox_Windows Mobile 6_Random - Fatal编程技术网

picturebox图像随机化C#

picturebox图像随机化C#,c#,picturebox,windows-mobile-6,random,C#,Picturebox,Windows Mobile 6,Random,我正在开发一个拼图滑块程序,并尝试在PictureBox中随机排列图像。我在互联网上做了一些研究,但找不到任何我可以研究的例子。以下是我的代码: Random r = new Random(); PictureBox[] picBox = new PictureBox[9]; picBox[0] = new PictureBox(); picBox[1] = new PictureBox(); picBox[2]

我正在开发一个拼图滑块程序,并尝试在PictureBox中随机排列图像。我在互联网上做了一些研究,但找不到任何我可以研究的例子。以下是我的代码:

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

for(inti=0;i只需填充数组并使用算法即可

可能实现为一种扩展方法:

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

    }
}

创建一个与图片数组大小相等的布尔数组

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++;
    }
}
inticount=0;
随机=新随机();
while(i计数<图片长度)
{
int trunt=random.Next(0,pictures.Length);
//确保您只使用可用图片
if(usedPictures[尝试]==false)
{            
picBox[尝试]。图像=图片[iCount];
doorUsed[尝试]=真;
iCount++;
}
}

Wow Thank看来我得研究算法了。不过这需要一些时间。同时,对于我比较弱的数学头脑来说,有没有简单的编码:X这对我来说相当复杂lol,但它确实起作用,我得到了逻辑直觉,你通过收集项目,用另一个rand交换每个项目om项。为了确保洗牌是正确随机的,没有偏差(即没有组合比另一个更可能),您需要确保只交换当前项之后的项(或之前的项,因为大多数实现向后运行数组以简化计算)。不必进行数学计算,这只是你必须信任他人的事情。嘿,robb谢谢谢谢谢谢谢谢谢谢!它100%有效,而且我所有的图像都被刷得乱七八糟!10/10向u竖起大拇指:):)这对于如此少量的要洗牌的项目来说效果很好(9),但你应该知道这个解决方案无法扩展。如果您将来需要洗牌更多的项目,那么值得研究更好的洗牌算法。@ICR,当然。我想,由于他的谜题是如此的静态,这个解决方案将以一种简单的方式完成这项工作,这正是他真正想要的。是的,谢谢大家。在开发包含更多项的程序时,我将研究更好的算法。谢谢大家,加油:)
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++;
    }
}