• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Game using Control arrays

I have a program that works on the principle of a childs game, the object of the game is to move the cells until all the numbers appear on the screen like this
1 2 3
4 5 6
7 8 Empty(mtcell)
I have enclosed the code i have already done, what I need is a way of starting the game so that every new game has a random starting position for the numbers could this be done by using a control array and how please.
I'm using VB5
Option Explicit
Dim x as Integer
Dim Temp
private Sub cmd1_click( )
If ((cmd1.Top = mtcell.Top) And (ABS(mtcell.Left - cmd1.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd1.Left
      cmd1.Left = Temp
End If
If ((cmd1.Left = mtcell.Left) And (ABS(cmd1.Top - mtcell.Top) = 1440)) Then
      Temp = cmd1.Top
      cmd1.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd2_click( )
If ((cmd2.Top = mtcell.Top) And (ABS(mtcell.Left - cmd2.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd2.Left
      cmd2.Left = Temp
End If
If ((cmd2.Left = mtcell.Left) And (ABS(cmd2.Top - mtcell.Top) = 1440)) Then
      Temp = cmd2.Top
      cmd2.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd3_click( )
If ((cmd3.Top = mtcell.Top) And (ABS(mtcell.Left - cmd3.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd3.Left
      cmd3.Left = Temp
End If
If ((cmd3.Left = mtcell.Left) And (ABS(cmd3.Top - mtcell.Top) = 1440)) Then
      Temp = cmd3.Top
      cmd3.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd4_click( )
If ((cmd4.Top = mtcell.Top) And (ABS(mtcell.Left - cmd4.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd4.Left
      cmd4.Left = Temp
End If
If ((cmd4.Left = mtcell.Left) And (ABS(cmd4.Top - mtcell.Top) = 1440)) Then
      Temp = cmd4.Top
      cmd4.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd5_click( )
If ((cmd5.Top = mtcell.Top) And (ABS(mtcell.Left - cmd5.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd5.Left
      cmd5.Left = Temp
End If
If ((cmd5.Left = mtcell.Left) And (ABS(cmd5.Top - mtcell.Top) = 1440)) Then
      Temp = cmd5.Top
      cmd5.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd6_click( )
If ((cmd6.Top = mtcell.Top) And (ABS(mtcell.Left - cmd6.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd6.Left
      cmd6.Left = Temp
End If
If ((cmd6.Left = mtcell.Left) And (ABS(cmd6.Top - mtcell.Top) = 1440)) Then
      Temp = cmd6.Top
      cmd6.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd7_click( )
If ((cmd7.Top = mtcell.Top) And (ABS(mtcell.Left - cmd7.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd7.Left
      cmd7.Left = Temp
End If
If ((cmd7.Left = mtcell.Left) And (ABS(cmd7.Top - mtcell.Top) = 1440)) Then
      Temp = cmd7.Top
      cmd7.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub

private Sub cmd8_click( )
If ((cmd8.Top = mtcell.Top) And (ABS(mtcell.Left - cmd8.Left) = 1680)) Then
      Temp = mtcell.Left
      mtcell.left = cmd8.Left
      cmd8.Left = Temp
End If
If ((cmd8.Left = mtcell.Left) And (ABS(cmd8.Top - mtcell.Top) = 1440)) Then
      Temp = cmd8.Top
      cmd8.Top = mtcell.Top
      mtcell.Top = Temp
End If
End Sub








0
dypsey
Asked:
dypsey
  • 3
  • 2
  • 2
  • +1
1 Solution
 
mcriderCommented:
You can use Randomize and RND to generate random numbers and incorporate them into your program.  For example:

Dim MyValue
Randomize      ' Initialize random-number generator.

MyValue = Int((6 * Rnd) + 1)      ' Generate random value between 1 and 6.


Cheers!
0
 
Erick37Commented:
0
 
dypseyAuthor Commented:
Do I put this code in General Declarations
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
KDivadCommented:
Richard?

If so, "John" has posted code for this game on the other board. Apparently it works because at the end of the message he says something like:

Well, I gotta go. This game is really addictive! Thanks!

If not, then everybody just ignore me...
0
 
Erick37Commented:
I have some example source code, if you need more help.
0
 
dypseyAuthor Commented:
Eric37 can you let me have some examples please
E-Mail RRichie@aol.com
0
 
Erick37Commented:
I could post the code here.
Sample program based on your code which uses a control array, and starts up in random order.

But first please reject the currently proposed answer, unless you think it is a good answer. :-)
0
 
mcriderCommented:
Here is a subroutine that you can call when you're ready to populate the buttons with random numbers.  It uses the randomize function I gave you...


Cheers!

THE CODE:

Sub LoadButtons()
    Dim RndIndex(1 To 8) As Integer
    Dim MyValue As Integer
    Dim iVal As Integer
    Dim jVal As Integer
    Dim Vswitch As Boolean
    Randomize ' Initialize random-number generator.
    For iVal = 1 To 8
        MyValue = Int((8 * Rnd) + 1) ' Generate random value between 1 and 8.
        Do
            Vswitch = False
            For jVal = 1 To iVal
                If RndIndex(jVal) = MyValue Then Vswitch = True
            Next jVal
            If Vswitch = False Then Exit Do
            MyValue = MyValue + 1
            If MyValue = 9 Then MyValue = 1
        Loop
        RndIndex(iVal) = MyValue
    Next iVal
    Cmd1.Caption = CStr(RndIndex(1))
    Cmd2.Caption = CStr(RndIndex(2))
    Cmd3.Caption = CStr(RndIndex(3))
    Cmd4.Caption = CStr(RndIndex(4))
    Cmd5.Caption = CStr(RndIndex(5))
    Cmd6.Caption = CStr(RndIndex(6))
    Cmd7.Caption = CStr(RndIndex(7))
    Cmd8.Caption = CStr(RndIndex(8))
End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now