Android Question Randomise image locations

Beja

Expert
Licensed User
Longtime User
Hi,
I have 16 bitmap images in the files pane in the IDE.
These bitmaps are initially assigned to 16 ImageViews in the designer
The ImageVews are arranged in 4x4, so that bmp(0) is on ImageView(0) and so on

At runtime, I want the views be arranged in different order and randomly. so instead of imageview(0) be in the
top-left, it can be in any other place.

Any piece of code appreciated. Thanks in advance.
 

KMatle

Expert
Licensed User
Longtime User
You have assigned all the images inside the designer?

Ok, then you can swap the images like this. I don't know your code, so change it like you need. I didn't check the code.

B4X:
Dim iv as imageview, f, t as int 'f=from, t=to
for i=0 to 10'number of swaps
    f=rnd(0,16) 'gives a number between 1 and 15
    t=rnd(0,16)
    iv=youriv(f)
    youriv(f)=youriv(t)
    youriv(t)=iv
next
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
take a look at my Pairs4Android game, I used a routime that took an array of images names and returned a randomized array...up to 50 images names in my game
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi Klaus,
Thanks so much, and I just discovered I can't create an array with the designer. wrongly assumed I can rename them as array.

@Cableguy
Thanks so much for the link to your creation.. my project is not pairs match game, so no need to hide the tiles.. but I would
appreciate if you shared the routine that randomize the array. I didn't try Klaus code for the above reasons.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Hi Beja

Here are the subs that deal with the ramdomization of the image names array

B4X:
Sub Scramble(c) 'This sub creates an array of pairs of numbers, ie, if c = 4, Images Array will be Image1,Image2,Image3,Image4,Image1,Image2,Image3,Image4
For Id = 0 To c
Images = Images & Id & ","
Next c
Images = Images & Images
End Sub
 
Sub RandomSelect
ID=""
Last=StrLength(Images)
Pointer=Rnd(0,Last-1)
Do Until StrAt(Images,Pointer)="," Or Pointer=0
Pointer=Pointer-1
Loop
If Pointer>0 Then Images=StrRemove(Images,Pointer,1)
Do Until StrAt(Images,Pointer)=","
ID=ID&StrAt(Images,Pointer)
Images=StrRemove(Images,Pointer,1)
Loop
If Pointer=0 Then Images=StrRemove(Images,Pointer,1)
Return ID
End Sub
this one calls the subs in the correct order and sets some image attribs

B4X:
Scramble(49)
Do While StrLength(Images)>1
ImageId=RandomSelect
x=x+1
Control("vPic"&x).Image=ImageId&".jpg"
Control("vText"&x).Text=ImageId
Control("vpic"&x).Mode=cStretchImage
loop

The zip is of the Pairs4PPc version as I cannot find the android one, but you can open it in any text editor.
 

Attachments

  • Pairs.zip
    203.9 KB · Views: 149
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks cableguy
Would you edit the function to randomize one set of images (not pairs)?
thanks in advance
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Just ignore the scamble sub and create your own array, then call ramdomselect to return a new randomised array.
Credits to the RandomSelect sub goes to Klaus that gave invaluable help for this particular project of mine.

Precision, the RandomSelect sub does NOT return a randomised array, but chooses a value randomly from the array, returns it and deletes it from the array so that it cannot be chosen again.
 
Upvote 0
Top