Please explain this code! - TechRepublic
General discussion
April 26, 2001 at 04:57 PM
toliver

Please explain this code!

by toliver . Updated 25 years, 1 month ago

For I = 0 To 15
‘replace with card back
imgHidden(I).Picture = imgBack.Picture
imgHidden(I).Visible = True
Next I
‘Randomly sort 16 integers using Shuffle routine
‘Behind contains indexes (0-7) for hidden pictures
Call Shuffle(16, Behind)
For I = 0 To 15
If Behind(I) > 8 Then
Behind(I) = Behind(I) – 8
End If
‘Shift value back one since picture indices are from 0 to 7 (not 1 to 8)
Behind(I) = Behind(I) – 1
Next I
Choice = 1
cmdNew.Enabled = False
cmdExit.Caption = “&Stop”
End Sub

For LoopCounter = 1 To numberofitems
NumberList(LoopCounter) = LoopCounter
Next LoopCounter
‘Work through Remaining values
‘Start at NumberOfItems and swap one value
‘at each For/Next loop step
‘After each step, Remaining is decreased by 1
For Remaining = numberofitems To 2 Step -1
‘Pick item at random
ItemPicked = Int(Rnd * Remaining) + 1
‘Swap picked item with bottom item
TempValue = NumberList(Remaining)
NumberList(Remaining) = NumberList(ItemPicked)
NumberList(ItemPicked) = TempValue
Next Remaining
‘End Sub

‘Call cmdNew_Click
End Sub

Private Sub imgHidden_Click(Index As Integer)
‘If trying to pick same box, picking already selected box
‘or trying pick when not playing, exit
If (Choice = 2 And Index = Picked(1)) Or Behind(Index) = -1 Or cmdNew.Enabled Then
Exit Sub
End If
‘Display selected picture
imgHidden(Index).Picture = imgChoice(Behind(Index)).Picture
imgHidden(Index).Refresh
If Choice = 1 Then
Picked(1) = Index
Choice = 2
Exit Sub
End If
Guesses = Guesses + 1
lblGuesses.Caption = Format(Guesses, “0”)
Picked(2) = Index
If Behind(Picked(1)) = Behind(Picked(2)) Then

Behind(Picked(1)) = -1
Behind(Picked(2)) = -1
Remaining =Remaining – 1
Else
‘If no match, blank picture, restore backs

imgHidden(Picked(1)).Picture = imgBack.Picture
imgHidden(Picked(2)).Picture = imgBack.Picture
End If
Cho

This discussion is locked

All Comments