Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generate random image placements without reusing the placement

Posted on 2004-10-30
4
Medium Priority
?
182 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:iamdodge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:danaseaman
ID: 12456210
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
 

Author Comment

by:iamdodge
ID: 12458053
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
 
LVL 22

Accepted Solution

by:
danaseaman earned 760 total points
ID: 12458190
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
 

Author Comment

by:iamdodge
ID: 12458269
MUCH APPRECIATED!! THANKS A LOT!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question