Solved

Generate random image placements without reusing the placement

Posted on 2004-10-30
166 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
Question by:iamdodge
    4 Comments
     
    LVL 22

    Expert Comment

    by:danaseaman
    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
    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:
    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
    MUCH APPRECIATED!! THANKS A LOT!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now