Generate random image placements without reusing the placement

Ok so here's my code

spin is a button

P1 is a picture array and there are 4 of them

timer1 is a timer


Private Sub spin_Click()
If spin.Caption = "SPIN" Then
spin.Caption = "STOP"
Else
spin.Caption = "SPIN"
End If
If Timer1.Enabled = True Then
Timer1.Enabled = False
Else
Timer1.Enabled = True
End If
End Sub
Private Sub Timer1_Timer()
For i = 0 To 3
If P1(i).Left = "2520" Then
P1(i).Left = Rnmp
P1(i).Top = Rnmp
ElseIf P1(i).Left = "3720" Then
P1(i).Left = Rnmp
P1(i).Top = Rnmp
ElseIf P1(i).Left = "5280" Then
P1(i).Left = Rnmp
P1(i).Top = Rnmp
ElseIf P1(i).Left = "6480" Then
P1(i).Left = Rnmp
P1(i).Top = Rnmp
End If
Randomize
Rnmp = Int(Rnd * 4) + 1
Select Case Rnmp
Case "1"
P1(i).Top = "4680"
P1(i).Left = "2520"
Case "2"
P1(i).Top = "3480"
P1(i).Left = "3720"
Case "3"
P1(i).Top = "3480"
P1(i).Left = "5280"
Case "4"
P1(i).Top = "4680"
P1(i).Left = "6480"
End Select
Next i
l1.Caption = P1(0).Left & " " & P1(0).Top
l2.Caption = P1(1).Left & " " & P1(1).Top
l3.Caption = P1(2).Left & " " & P1(2).Top
l4.Caption = P1(3).Left & " " & P1(3).Top
End Sub


l1, l2, l3 and l4 are labels that show the positions of the pictures and they DO show that the pictures are overlapping

What happens with this code is the 4 pictures start in designated places, which are equivalent to each of the 4 cases. Picture one starts where case 1 is, picture 2 where case 2 is, etc.

The problem arises when I start "spinning" or moving the images. I want them to move to one of the case points other than where they start, and NOT overlapping another image where they currently do. They need to pick their own unused space and then keep moving to a different one.

How is this possible?

Thanks in advance,
    Mike
iamdodgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danaseamanCommented:
Use the same code one uses to shuffle a deck of cards but in this case the deck is 4 vs 52. You want to randomize but have no duplicates.

http://www.acky.net/vb/vbgames/shuffle.zip
http://www.imt.net/~joe/matt/program/vb/Tutorials/ShuffleDeck.txt

You may also want to preset an array of positions:
Option Explicit

Private Type POINTAPI
   X                    As Long
   Y                    As Long
End Type

Dim pos(3) As POINTAPI

Private Sub Form_Load()
   'Load positions
   pos(0).Y = 4680
   pos(0).X = 2520
   
   pos(1).Y = 3480
   pos(1).X = 3720
   
   pos(2).Y = 3480
   pos(2).X = 5280
   
   pos(3).Y = 4680
   pos(3).X = 6480
End Sub

Private Sub spin_Click()
   Timer1.Enabled = Not (Timer1.Enabled)
   Spin.Caption = IIf(Timer1.Enabled, "STOP", "SPIN")
End Sub

After shuffle do something like this:
For i= 0 to 3
   p1(i).Move pos(Deck(i)).X, pos(Deck(i)).Y
Next
0
iamdodgeAuthor Commented:
It would help a LOT if this was arranged in such a way as to replace all of my code with something that works. So far all this has managed to do is stick the pictures in each of those 4 positions and keep them there, they don't keep moving as the timer interval goes.
0
danaseamanCommented:
You need to add the card shuffle code. Here is the complete code:

Option Explicit

Private Type POINTAPI
   X                    As Long
   Y                    As Long
End Type

Dim pos(3)              As POINTAPI

Dim Cards(3)            As Integer 'Cards is an array of 4 numbers 0-3
Dim CardValue, X

Private Sub spin_Click()
   Timer1.Enabled = Not (Timer1.Enabled)
   spin.Caption = IIf(Timer1.Enabled, "STOP", "SPIN")
End Sub

Private Sub Shuffle()
   X = 0
   Erase Cards
   'This is where the random shuffle is made.
   'First, we initialize the randomizer.
   Randomize
   'Cardvalue is then set as a random number from 1 to 4,
   CardValue = Int((4 * Rnd) + 1)
   'and the value of the first element of the array is set.
   Cards(0) = CardValue
   'We must repeat the last three steps for the next 11 cards, using the
   'IsInArray function to check for duplicates.
   For X = 1 To 3
      Do While IsInArray = True
         'If IsInArray is true, we keep looping until a non-duplicate number
         'is generated by the randomizer.
         Randomize
         CardValue = Int((4 * Rnd) + 1)
         IsInArray
      Loop
      Cards(X) = CardValue
   Next X

End Sub

Public Function IsInArray() As Boolean
   'This function checks for duplicate numbers in the array.
   Dim Y                As Integer
   For Y = 0 To X
      If CardValue = Cards(Y) Then
         IsInArray = True
         Exit Function
      End If
   Next Y
   IsInArray = False
End Function

Private Sub Form_Load()
   'Init positions
   pos(0).Y = 4680
   pos(0).X = 2520

   pos(1).Y = 3480
   pos(1).X = 3720

   pos(2).Y = 3480
   pos(2).X = 5280

   pos(3).Y = 4680
   pos(3).X = 6480
End Sub

Private Sub Timer1_Timer()
   Dim i                As Long
   Shuffle
   For i = 0 To 3
      P1(i).Move pos(Cards(i) - 1).X, pos(Cards(i) - 1).Y
   Next
   L1.Caption = P1(0).Left & " " & P1(0).Top
   L2.Caption = P1(1).Left & " " & P1(1).Top
   L3.Caption = P1(2).Left & " " & P1(2).Top
   L4.Caption = P1(3).Left & " " & P1(3).Top
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iamdodgeAuthor Commented:
MUCH APPRECIATED!! THANKS A LOT!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.