Randomising

I have designed a sliding numbers game with the numbers 1 to 8 on a 9x9 matrix. I can't make the rnd function work so that every time the user clicks on start command button, the numbers are in a different order.  I can make the numbers randomise when I click on the new game command button, but as you can see I havn't used very good programming technique. (can you suggest an alternative?) Also, I want a restart command button to restart the game from where it began not jumble the numbers up as it has been doing.  I think I may need to use an array?  This is my code as it stands - thankyou for your help!

Option Explicit
Dim x As Integer
Dim typecode As Integer
Dim Reply As Integer


Private Sub cmdExit_Click()
Dim typecode As Integer
Dim Reply As Integer
typecode = 32 + 4
Reply = MsgBox("Play Again?", typecode, "")
If Reply = 7 Then End
x = Int(Rnd * 8) + 1
cmdSquare1 = True
cmdSquare2 = True
cmdSquare3 = True
cmdSquare4 = True
cmdSquare5 = True
cmdSquare6 = True
cmdSquare7 = True
cmdSquare8 = True
End Sub
Private Sub cmdNew_Click()
x = Int(Rnd * 8) + 1
cmdSquare1 = True
cmdSquare2 = True
cmdSquare3 = True
cmdSquare4 = True
cmdSquare5 = True
cmdSquare6 = True
cmdSquare7 = True
cmdSquare8 = True
End Sub

Private Sub cmdRestart_Click()
x = Int(Rnd * 8) + 1
cmdSquare1 = True
cmdSquare2 = True
cmdSquare3 = True
cmdSquare4 = True
cmdSquare5 = True
cmdSquare6 = True
cmdSquare7 = True
cmdSquare8 = True
End Sub

Private Sub cmdSquare1_Click()
If ((cmdSquare1.Top = cmdEmptySquare.Top) And (Abs(cmdSquare1.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare1.Left
cmdSquare1.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare1.Left = cmdEmptySquare.Left) And (Abs(cmdSquare1.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare1.Top
cmdSquare1.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub
Private Sub cmdSquare2_Click()
If ((cmdSquare2.Top = cmdEmptySquare.Top) And (Abs(cmdSquare2.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare2.Left
cmdSquare2.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare2.Left = cmdEmptySquare.Left) And (Abs(cmdSquare2.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare2.Top
cmdSquare2.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub
Private Sub cmdSquare3_Click()
If ((cmdSquare3.Top = cmdEmptySquare.Top) And (Abs(cmdSquare3.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare3.Left
cmdSquare3.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare3.Left = cmdEmptySquare.Left) And (Abs(cmdSquare3.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare3.Top
cmdSquare3.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub

Private Sub cmdSquare4_Click()
If ((cmdSquare4.Top = cmdEmptySquare.Top) And (Abs(cmdSquare4.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare4.Left
cmdSquare4.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare4.Left = cmdEmptySquare.Left) And (Abs(cmdSquare4.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare4.Top
cmdSquare4.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub

Private Sub cmdSquare5_Click()
If ((cmdSquare5.Top = cmdEmptySquare.Top) And (Abs(cmdSquare5.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare5.Left
cmdSquare5.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare5.Left = cmdEmptySquare.Left) And (Abs(cmdSquare5.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare5.Top
cmdSquare5.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub

Private Sub cmdSquare6_Click()
If ((cmdSquare6.Top = cmdEmptySquare.Top) And (Abs(cmdSquare6.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare6.Left
cmdSquare6.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare6.Left = cmdEmptySquare.Left) And (Abs(cmdSquare6.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare6.Top
cmdSquare6.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub

Private Sub cmdSquare7_Click()
If ((cmdSquare7.Top = cmdEmptySquare.Top) And (Abs(cmdSquare7.Left - cmdEmptySquare.Left))) Then
Dim temp
temp = cmdSquare7.Left
cmdSquare7.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare7.Left = cmdEmptySquare.Left) And (Abs(cmdSquare7.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare7.Top
cmdSquare7.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub

Private Sub cmdSquare8_Click()
If ((cmdSquare8.Top = cmdEmptySquare.Top) And (Abs(cmdSquare8.Left - cmdEmptySquare.Left))) Then

Dim temp
temp = cmdSquare8.Left
cmdSquare8.Left = cmdEmptySquare.Left
cmdEmptySquare.Left = temp
End If
If ((cmdSquare8.Left = cmdEmptySquare.Left) And (Abs(cmdSquare8.Top - cmdEmptySquare.Top))) Then
temp = cmdSquare8.Top
cmdSquare8.Top = cmdEmptySquare.Top
cmdEmptySquare.Top = temp
End If
cmdRestart.Enabled = True
End Sub








Private Sub UserForm_Click()

End Sub
sesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JuiletteConnect With a Mentor Commented:
Above
0
 
JuiletteCommented:
You have to use the word Randomize
before you use the function

ie.
Randomize
dim x
x = Int(Rnd*10)
will produce random between 1 and 10
0
 
JuiletteCommented:
To restart you will have to save the numbers into and array and then load them back when starting over.

0
 
vikiingCommented:
Friend Ses:

   The random number generator under VB works (as in the vast majority of cases) based on a method known as "the seed", this is, the generator start, for the first time, with a known value. Then, each time a number is generated, part of that number is fed to the own generator, changing its internal values to obtain another (different) number.

Each time you start a program, as the seed is always the same, the same sequence of numbers is generated, once and again.

As Juliette said, the verb Randomize lets you change the value of the seed, giving a totally different sequence of numbers.

Thus, if you should use, say, Randomize 500, the sequence you'll get will be different, but, each time you run the program, once again, you'll get the same sequence of numbers (a good start point for de/crypting routines).

The verb Randomize, used with no arguments, it's the same as "Randomize Timer", which picks up the machine clock value to feed the initial seed.

As that value changes each time you run your program, the sequence of numbers will be different each time, and you'll never get the same sequence.

Of course, if you run the program today and tomorrow, at THE EXACT SAME TIME (precise up to 5 hundredths of second), yes, you will obtain the same sequence of values.
0
All Courses

From novice to tech pro — start learning today.