I am working on a puzzle slider program and am trying to make the images random inside the image box. I did some research on the Internet, I could not find any examples that could work on. These are my codes:
random R = new random (); Picturebox [] picBox = new image box [9]; Picobox [0] = new image box (); PicoBox [1] = New Picture Box (); Pickbox [2] = New Picture Box (); Picox [3] = new image box (); PicoBox [4] = New Picture Box (); PicoBox [5] = New Picture Box (); Picox [6] = new image box (); Picox [7] = new image box (); Pickbox [8] = New Picture Box ();
I also have a bitmap array:
bitmap [] pictures = new bitmap [9]; Picture [0] = new bitmap (@ "1.1Bright.jpg"); Image [1] = new bitmap (@ "1.2 bit .jpg"); Image [2] = new bitmap (@ "1.3 bytes.jpg"); Image [3] = new bitmap (@ "2.1 byte.jpg"); Picture [4] = new bitmap (@ "2.2 byte .jpg"); Picture [5] = new bitmap (@ "2.3Bright.jpg"); Picture [6] = new bitmap (@ "3.1 bytes.jpg"); Image [7] = new bitmap (@ "3.2 byte .jpg"); Image [8] = new bitmap (@ "3.3 dark .jpg");
I tried some methods but I do not remember how to set random images in the picture []:
for (int I = 0; i & lt; = 8; i ++) {picBox [i]. Picture = picture [r. Next (9)]; }
The problem here is that some pictureboks eg Pickbox [1] and picbox [6] are duplicated images. How can I repeat them? Examples are highly appreciated.
Create bools equal to the size of the picture array
bool [] Used picture = new bool [pictures.Length];
Set the value of this array to false
Now define your random number, and test if that element is used or not Something like this:
int iCount = 0; Random random = new random (); While (iCount & lt; pictures.Length) {int attempts = Random Next (0, picture. Length); // ensures that you will only use one available picture if used ([attempt] == wrong) {picBox [Attempt] Picture = image [iCount]; Door use [effort] = true; ICount ++; }}
Comments
Post a Comment