How Can I Sort ImageViews Randomly?

PharCyDeD

Active Member
Licensed User
Longtime User
I am making a matching game with the following setup:

vfzexv.png


The game is far from done so ignore the mismatch of names, but is there a way to sort these ImageViews in a grid but randomly? This way the game will never start the same.

:sign0085:
 

JonPM

Well-Known Member
Licensed User
Longtime User
How are you adding the views? Through the designer?
I would create them programmically with a for loop, and add each to the activity using Rnd
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I am adding them through the designer, but I know the specific locations that they need to be moved to (randomly). Is there a way I can create a loop that places them in random locations? I can move them of course by doing:

B4X:
ImageView1.Top = 17
ImageView1.Left = 28

...but that is of course not random and I do need them to be in a certain order. For instance, my top row all needs to start at ".Top = 17". Can you show me a quick/small example of how you would do it programmically? Just like maybe 3 ListViews being random sorted or something? I am really stumped on this and it is driving me crazy :BangHead:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would suggest you to define the ImageViews as an array of ImageViews instead of defining them in the Designer with different names.
Then in two For / Next loops you can calculate and set the Top and Left properties of the ImageView. How do you define the random position and how are they memorized ?
It would be easier to help you if you posted the project as a zip file.

Best regards.
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
My project is HUGE so for the sake of ease I am uploading a simple version to show what I am trying to do. I can move ImageView1 around to the spots I need with ease, however, I tried an attempt to control the 'random' placing and it just doesn't seem to be working right. If you click the 'Move' button you can move it around to specific yet random locations as much as you like, if click the 'Randomize' it seems to half work. Not sure what I am doing, but you will be able to know more once you open the zip.

Some sample code:

B4X:
Dim ImageView1Spot As Int
Dim ImageView2Spot As Int
Dim Spot1Taken As Boolean
Dim Spot2Taken As Boolean

B4X:
ImageView1Spot = Rnd(1, 10)
ImageView2Spot = Rnd(1, 10)

   'Spot 1
      ''''''''''''''''''''''''''
   If ImageView1Spot = 1 Then
      If Spot1Taken = False Then
         ImageView1.Top = 30
         ImageView1.Left = 0
         Spot1Taken = True
      Else
         
      End If
   End If
 

Attachments

  • MyTestVersion.zip
    20.9 KB · Views: 170
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
You have 30 ImageView locations.
I would set up an array holding their locations,
then shuffle that array,
then assign each ImageView to each one, like this:

B4X:
Location(0,0) = 0: Location(0,1) = 30 ' left and top
Location(1,0) = 40: Location(1,1) = 30
Location(2,0) = 80: Location(2,1) = 30
...
Location(29, 0) = 120: Location(29,1) = 150

For i = 0 To 29
    Locs(i) = i
Next

For i = 0 To 29
    j = Rnd(i, 30)
    x = Locs(i) 
    Locs(i) = Locs(j)   
    Locs(j) = x
Next

For i = 0 to 29
    ImgView1(i).Left = Location(Locs(i), 0)
    ImgView1(i).Top = Location(Locs(i), 1)
Next

Oops, I just noticed that I started thinking that you had 36 views instead of 30. I have corrected the code above to use 30 views.
 
Last edited:
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Thanks for your reply nford..I will give this code a shot when I get back from lunch. I am curious though...How will I be able to handle multiple resolutions with this? Is there anyway to detect a resolution like:

B4X:
If Resolution = 480 x 320 Then
Location(0,0) = 0: Location(0,1) = 30 ' left and top
Location(1,0) = 40: Location(1,1) = 30
Location(2,0) = 80: Location(2,1) = 30
End If

If Resolution = 1280 x 800 Then
Location(0,0) = 0: Location(0,1) = 30 ' left and top
Location(1,0) = 40: Location(1,1) = 30
Location(2,0) = 80: Location(2,1) = 30
End If

I will post back if I run into any problems or have questions about the code you posted. I will be back soon.
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Thank you so much for the link. It looks to be exactly what I need. I am probably going to have to do some reading about setting up arrays since I have no experience with them. I need to gain a better understanding of your posted code so I can implement it. I gotta head out to run some errands, but I will read up on the subject before I post any more questions. Thank you and everyone else who has helped me out in this thread. If anything pops up in your mind that I should be looking into or doing then please feel free to post!

:sign0188:
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Here's how I set up an ImageView array in a keyboard app:

B4X:
Sub Globals
   Dim imgKey00, imgKey01, imgKey02, imgKey03, imgKey04 As ImageView
   Dim imgKey05, imgKey06, imgKey07, imgKey08, imgKey09 As ImageView   
   Dim Keys() As ImageView
End Sub

Sub Activity
   Keys = Array As ImageView(imgKey00, imgKey01, imgKey02, imgKey03, imgKey04, _
                               imgKey05, imgKey06, imgKey07, imgKey08, imgKey09)

In Designer, I set up each of the above views with an Event Name of zKbd_Keys and Tags 0 through 16.
Then lets say that I want to position imgKey03 -- I would refer to Keys(3).Left = ... and Keys(3).Top = ...

If I want to catch the Click event for any of those ImageViews, I use zKbd_Keys_Click.
 
Last edited:
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I have to use the ImageViews tags to compare them as a "match" in the game. I really couldn't think of another way to compare them. In the picture you can see Blue1 and Blue1Match. They both share the same tag in my app which is "blue1.png" since they both use blue1.png as a background image. The way you set up the array in your post requires the tags to be changed? What should I do here?

btw...I do have 36 imageviews in my actual project. I just realized you said something about the number of them in another post. I know that doesn't mean too much, but just wanted to toss it out there.
 
Last edited:
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Actually I think I can compare the new setup like this:


B4X:
Sub Blue1Match_Click
        If Answer2.Tag = "0" Then 
         If Answer1.Tag = "4" Then
            YouGotItRight
            ScoreAndProgress
            
         Else
            
            YouGotItWrong
            
         End If
      End If
End Sub

Assuming that I jus make both imageviews that are blue have 0 and 4 tags instead of them both having blue1.png as a tag. I have more complex code that determines whether or not it is their first click, etc... but for the sake of ease on the eyes I am keeping it simple on here. I do need to catch the click event though so how would I use zKbd_Keys_Click to catch all the events?
 
Upvote 0
Top