Results 1 to 9 of 9

Thread: The Memory Game

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    6

    The Memory Game

    I'm new around here and if I have posted in the wrong section the I am sorry.

    Anyway, I am having an issue with a game I am developing for my project. As the name states in the title its a Memory Game. (I will post pictures to show you what I mean)







    The issue I am having is that I am creating two levels, one for beginner (less matches) and expert. Now the beginner code works perfectly, but where as the expert doesn't work. Now I used the beginner code however edited it because of the more squares, but now whenever I try to click "New Game" it just crashes VB6.

    Any Ideas? I will provide a copy of my project and code so you guys can take a look for yourself.

    Code:
    Beginner:
    Option Explicit
    Dim lblPos(1 To 16) As Integer
    Dim Tries As Integer
    Dim Pairs As Integer
    
    Sub WasteTime()
    Dim W As Long
    For W = 1 To 50000000
    Next W
    End Sub
    
    Private Sub cmdExit_Click()
    End
    End Sub
    
    
    Private Sub cmdMenu_Click()
    frmGame.Hide
    frmStart.Show
    End Sub
    
    Private Sub cmdNew_Click(Index As Integer)
    Static occupied(1 To 16) As Integer
    Dim I As Integer, GridPos As Integer
    Dim Row As Integer, Column As Integer
    Print "HighScore"
    Randomize
    For I = 1 To 16
    occupied(I) = False
    Next I
    For I = 1 To 16
    PicCover(I).Visible = True
    Do
    Row = Int(Rnd * 4) + 1
    Column = Int(Rnd * 4) + 1
    GridPos = (Row - 1) * 4 + Column
    Loop While occupied(GridPos)
    occupied(GridPos) = True
    lblPos(GridPos) = I
    lblNo(I).Top = 1200 + (Row - 1) * 720
    lblNo(I).Left = 250 + (Column - 1) * 720
    Next I
    Tries = 0
    Pairs = 0
    lblTries.Caption = 0
    End Sub
    
    
    Private Sub Form_Load()
    frmGame.Hide
    frmDifficulty.Show
    End Sub
    
    
    
    Sub PicCover_Click(Index As Integer)
    Static FirstCard As Integer, FirstCover As Integer
    Static SecondCard As Integer, SecondCover As Integer
    PicCover(Index).Visible = False
    Tries = Tries + 1
    DoEvents
    If Tries Mod 2 = 1 Then
    FirstCard = lblPos(Index)
    FirstCover = Index
    Else
    lblTries.Caption = Tries \ 2
    SecondCard = lblPos(Index)
    SecondCover = Index
    If FirstCard Mod 8 = SecondCard Mod 8 Then
    Pairs = Pairs + 1
    If Pairs = 8 Then
    MsgBox "Puzzle Solved..... Good Work"
    Print Tries / 2
    End If
    Else
    WasteTime
    PicCover(FirstCover).Visible = True
    PicCover(SecondCover).Visible = True
    End If
    End If
    End Sub
    
    Expert
    Option Explicit
    Dim lblPos(1 To 32) As Integer
    Dim Tries As Integer
    Dim Pairs As Integer
    
    Sub WasteTime()
    Dim W As Long
    For W = 1 To 50000000
    Next W
    End Sub
    
    Private Sub cmdMenu_Click()
    frmGame.Hide
    frmStart.Show
    End Sub
    
    Private Sub cmdNew_Click(Index As Integer)
    Static occupied(1 To 32) As Integer
    Dim I As Integer, GridPos As Integer
    Dim Row As Integer, Column As Integer
    Print "HighScore"
    Randomize
    For I = 1 To 32
    occupied(I) = False
    Next I
    For I = 1 To 32
    PicCover(I).Visible = True
    Do
    Row = Int(Rnd * 4) + 1
    Column = Int(Rnd * 4) + 1
    GridPos = (Row - 1) * 4 + Column
    Loop While occupied(GridPos)
    occupied(GridPos) = True
    lblPos(GridPos) = I
    lblNo(I).Top = 1200 + (Row - 1) * 720
    lblNo(I).Left = 250 + (Column - 1) * 720
    Next I
    Tries = 0
    Pairs = 0
    lblTries.Caption = 0
    End Sub
    
    Private Sub Form_Load()
    frmGame.Hide
    frmDifficulty.Show
    End Sub
    
    Private Sub PicCover_Click(Index As Integer)
    Static FirstCard As Integer, FirstCover As Integer
    Static SecondCard As Integer, SecondCover As Integer
    PicCover(Index).Visible = False
    Tries = Tries + 1
    DoEvents
    If Tries Mod 2 = 1 Then
    FirstCard = lblPos(Index)
    FirstCover = Index
    Else
    lblTries.Caption = Tries \ 2
    SecondCard = lblPos(Index)
    SecondCover = Index
    If FirstCard Mod 8 = SecondCard Mod 8 Then
    Pairs = Pairs + 1
    If Pairs = 8 Then
    MsgBox "Puzzle Solved..... Good Work"
    Print Tries / 2
    End If
    Else
    WasteTime
    PicCover(FirstCover).Visible = True
    PicCover(SecondCover).Visible = True
    End If
    End If
    End Sub
    Thanks
    Attached Files Attached Files

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: The Memory Game

    I tried using this code and it seemed to work correctly.

    vb Code:
    1. Private Sub cmdNew_Click(Index As Integer)
    2. Static occupied(1 To 32) As Integer
    3. Dim I As Integer, GridPos As Integer
    4. Dim Row As Integer, Column As Integer
    5. Print "HighScore"
    6. Randomize
    7. For I = 1 To 16
    8. occupied(I) = False
    9. Next I
    10. For I = 1 To 16
    11. PicCover(I).Visible = True
    12. Do
    13. Row = Int(Rnd * 4) + 1
    14. Column = Int(Rnd * 4) + 1
    15. GridPos = (Row - 1) * 4 + Column
    16. Loop While occupied(GridPos)
    17. occupied(GridPos) = True
    18. lblPos(GridPos) = I
    19. lblNo(I).Top = 840 + (Row - 1) * 720
    20. lblNo(I).Left = 250 + (Column - 1) * 720
    21. Next I
    22. Tries = 0
    23. Pairs = 0
    24. lblTries.Caption = 0
    25. End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    6

    Re: The Memory Game

    Thanks for the post. I will try that. I will report back with the result.

    Anyway I would like to ask another question. I have a "HighScore" come up on the form. Now when you complete the game it will print the score on the form. But, every time you click new game (Randomises) it keeps printing "HighScore". Is there any way for it to print the score into, lets say a MsgBox. But with the MsgBox I also want it to remember the score of previous tries. Any ideas? Anyway sorry for the addition, I want to try and make my project a bit better.

    Edit: Works now but still has issues. For starters, the game isn't randomizing, and is getting the wrong letter together. Also, it is only working for half the game is working.
    Last edited by Windows1; Aug 12th, 2010 at 01:27 AM.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: The Memory Game

    VB Code:
    1. Option Explicit
    2. Dim lblPos(1 To 16) As Integer
    3. Dim Tries As Integer
    4. Dim Pairs As Integer
    5. Dim highscore As Integer
    6. Sub WasteTime()
    7. Dim W As Long
    8. For W = 1 To 50000000
    9. Next W
    10. End Sub
    11.  
    12. Private Sub cmdExit_Click()
    13. End
    14. End Sub
    15.  
    16.  
    17. Private Sub cmdMenu_Click()
    18. frmGame.Hide
    19. frmStart.Show
    20. End Sub
    21.  
    22. Private Sub cmdNew_Click(Index As Integer)
    23. 'Rewrite this
    24. Static occupied(1 To 16) As Integer
    25. Dim I As Long, GridPos As Integer
    26. Dim Row As Integer, Column As Integer
    27.  
    28. For I = 1 To 16
    29. Randomize
    30. I = I + Int(Rnd * I)
    31. occupied(I) = False
    32. Next I
    33. For I = 1 To 16
    34. PicCover(I).Visible = True
    35. Do
    36. Row = Int(Rnd * 4) + 1
    37. Column = Int(Rnd * 4) + 1
    38. GridPos = (Row - 1) * 4 + Column
    39. Loop While occupied(GridPos)
    40. occupied(GridPos) = True
    41. lblPos(GridPos) = I
    42. lblNo(I).Top = 1200 + (Row - 1) * 720
    43. lblNo(I).Left = 250 + (Column - 1) * 720
    44. Next I
    45. 'End rewrite
    46. Tries = 0
    47. Pairs = 0
    48. lblTries.Caption = 0
    49. End Sub
    50.  
    51. Private Sub Form_Load()
    52. frmGame.Hide
    53. frmDifficulty.Show
    54. highscore = 0
    55. End Sub
    56.  
    57. Sub PicCover_Click(Index As Integer)
    58. Static FirstCard As Integer, FirstCover As Integer
    59. Static SecondCard As Integer, SecondCover As Integer
    60. PicCover(Index).Visible = False
    61. Tries = Tries + 1
    62. DoEvents
    63. If Tries Mod 2 = 1 Then
    64. FirstCard = lblPos(Index)
    65. FirstCover = Index
    66. Else
    67. lblTries.Caption = Tries \ 2
    68. SecondCard = lblPos(Index)
    69. SecondCover = Index
    70. If FirstCard Mod 8 = SecondCard Mod 8 Then
    71. Pairs = Pairs + 1
    72. If Pairs = 8 Then
    73. MsgBox "Puzzle Solved..... Good Work"
    74. highscore = Pairs
    75. MsgBox (highscore)
    76. End If
    77. Else
    78. WasteTime
    79. PicCover(FirstCover).Visible = True
    80. PicCover(SecondCover).Visible = True
    81. End If
    82. End If
    83. End Sub

    I have sorted out the "High Score" problem! However, you are going have to find a simpler method for the "New Game" randomization. In it's current state it will cause the system to freeze whenever, it tries to produce the random numbers. I also noticed that you hard coded the letters in to the labels it would be better to assign them to a label pragmatically if you want to randomize them.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: The Memory Game

    Some suggestions:
    1. Using End statement is a bad idea. See this: http://www.vbforums.com/showthread.php?t=511766
    2. Your WasteTime() sub seems to be not a good idea. It will freeze the program.
    3. Try indenting your code statements. It will make it easier to read
    4. In memory game, only 2 slots should be visible on click. After that, if they are a match, display it permanently, otherwise, hide the two. What you have coded will display all items that you click on.


    Good luck...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: The Memory Game

    You know, this memory game reminds me of a familiar game show that used to air in the late-1980s. Ever hear of Classic Concentration?

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: The Memory Game

    The is the remodeled Beginner code taking in to account my changes and akhileshbc's suggestions.

    vb Code:
    1. Option Explicit
    2. Dim lblPos(1 To 16) As Integer
    3. Dim Tries As Integer
    4. Dim Pairs As Integer
    5. Dim highscore As Integer
    6.  
    7. Private Sub cmdExit_Click()
    8. Unload Me
    9. End Sub
    10.  
    11. Private Sub cmdMenu_Click()
    12. frmGame.Hide
    13. frmStart.Show
    14. End Sub
    15.  
    16. Private Sub cmdNew_Click(Index As Integer)
    17. Static occupied(1 To 16) As Integer
    18. Dim I As Integer, GridPos As Integer
    19. Dim Row As Integer, Column As Integer
    20. Randomize
    21. For I = 1 To 16
    22. occupied(I) = False
    23. Next I
    24. For I = 1 To 16
    25. PicCover(I).Visible = True
    26. Do
    27. Row = Int(Rnd * 4) + 1
    28. Column = Int(Rnd * 4) + 1
    29. GridPos = (Row - 1) * 4 + Column
    30. Loop While occupied(GridPos)
    31. occupied(GridPos) = True
    32. lblPos(GridPos) = I
    33. lblNo(I).Top = 1200 + (Row - 1) * 720
    34. lblNo(I).Left = 250 + (Column - 1) * 720
    35. Next I
    36. Tries = 0
    37. Pairs = 0
    38. lblTries.Caption = 0
    39. End Sub
    40.  
    41. Private Sub Form_Load()
    42. frmGame.Hide
    43. frmDifficulty.Show
    44. highscore = 0
    45. End Sub
    46.  
    47. Sub PicCover_Click(Index As Integer)
    48. Static FirstCard As Integer, FirstCover As Integer
    49. Static SecondCard As Integer, SecondCover As Integer
    50. PicCover(Index).Visible = False
    51. Tries = Tries + 1
    52. DoEvents
    53. If Tries Mod 2 = 1 Then
    54. FirstCard = lblPos(Index)
    55. FirstCover = Index
    56. Else
    57. lblTries.Caption = Tries \ 2
    58. SecondCard = lblPos(Index)
    59. SecondCover = Index
    60. If FirstCard Mod 8 = SecondCard Mod 8 Then
    61. Pairs = Pairs + 1
    62. If Pairs = 8 Then
    63. MsgBox "Puzzle Solved..... Good Work"
    64. highscore = Pairs
    65. MsgBox (highscore)
    66. End If
    67. Else
    68. PicCover(FirstCover).Visible = True
    69. PicCover(SecondCover).Visible = True
    70. End If
    71. End If
    72. End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: The Memory Game

    I have made a small game myself...

    Try it....

    Code:
    Option Explicit
    
    Dim strBoxContent(15)   As String
    Dim intPrevious         As Integer
    Dim intCurrent          As Integer
    Dim blnIsDisplayed(15)  As Boolean
    Dim isWaiting           As Boolean
    Dim intTotal            As Integer
    Dim intClicks           As Integer
    
    '~~~ To shuffle the array
    Private Function ShuffleArray(pvarArray As Variant)
        Dim i As Long
        Dim iMin As Long
        Dim iMax As Long
        Dim lngReplace As Long
        Dim varSwap As Variant
        
        iMin = LBound(pvarArray)
        iMax = UBound(pvarArray)
        For i = iMax To iMin + 1 Step -1
            lngReplace = Int((i - iMin + 1) * Rnd + iMin)
            varSwap = pvarArray(i)
            pvarArray(i) = pvarArray(lngReplace)
            pvarArray(lngReplace) = varSwap
        Next
    End Function
    
    Private Sub Form_Load()
        Dim i As Long
        
        '~~~ Loading the array with A to H. The array will contain 2 copies of each
        For i = 0 To 7
            strBoxContent(i) = Chr$(65 + i)
            strBoxContent(15 - i) = Chr$(65 + i)
        Next
        
        ShuffleArray strBoxContent
        
        For i = 0 To 15
            'lblBox(i).Caption = strBoxContent(i)
            blnIsDisplayed(i) = False
        Next
        
        isWaiting = False
        intPrevious = -1
        intTotal = 0
        intClicks = 0
    End Sub
    
    '~~~ On clicking the box
    Private Sub lblBox_Click(Index As Integer)
        If isWaiting = True Then Exit Sub
        If blnIsDisplayed(Index) = False Then
            lblBox(Index).Caption = strBoxContent(Index)
            If intPrevious = -1 Then
                intPrevious = Index
            Else
                If blnIsDisplayed(intPrevious) = True Then Exit Sub
                intCurrent = Index
    
                If strBoxContent(intPrevious) = strBoxContent(intCurrent) Then
    
                    blnIsDisplayed(intPrevious) = True
                    blnIsDisplayed(intCurrent) = True
                    intPrevious = -1
                    intCurrent = -1
                    intTotal = intTotal + 1
                    Me.Caption = "Successful grouping: " & intTotal
                    
                    If intTotal = 8 Then MsgBox "Congratz"
                    
                Else
                    HideSelection
                End If
            End If
        End If
    End Sub
    
    '~~~ This will hide the selections after a specific time
    Private Sub HideSelection()
        isWaiting = True
        Timer1.Enabled = True
        Timer1.Interval = 200
    End Sub
    
    Private Sub Timer1_Timer()
        intClicks = intClicks + 1
        lblClicks.Caption = "Clicks made: " & intClicks
        lblBox(intPrevious).Caption = "X"
        lblBox(intCurrent).Caption = "X"
        intPrevious = -1
        intCurrent = -1
        isWaiting = False
        Timer1.Enabled = False
    End Sub
    Edit:
    Added the code for displaying the no. of clicks you made.
    Attached Files Attached Files
    Last edited by akhileshbc; Aug 14th, 2010 at 03:48 AM. Reason: added more code

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  9. #9
    New Member
    Join Date
    Aug 2010
    Posts
    13

    Re: The Memory Game

    Nicholas Francis, Way to like copy me. how is yours now, im trying to do a picture array instead of a label array now, but i have a few new errors. see in you in software tomorow, double period im going to try and get as much of this done as possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width