Finance

Charts

Statistics

Macros

Search

Operating the Game in Excel VBA

In the class module for Sheet1, you will find two module-level variables and the procedure Worksheet_SelectionChange(). These are used to handle user interaction with the game.

Dim row1 As Integer, col1 As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim row As Integer, col As Integer
    Dim startTime As Single
    ' Start and Info buttons
    If Target.Address = "$I$2:$K$2" Then
        StartGame
    ElseIf Target.Address = "$I$7:$K$7" Then
        MsgBox "Find 18 pairs of symbols." & vbCrLf & _
               "Select two cells one after another." & vbCrLf & _
               "You will see the symbol in each cell." & vbCrLf & _
               "One second after selecting the 2nd cell, both symbols are covered again.", , "Game Description"
    ' Memory game cells
    Else
        ' Determine row and column numbers from the cell address
        row = Val(Mid(Target.Address, 4, 1))
        col = Asc(Mid(Target.Address, 2, 1)) - 64
        ' If outside the game board, exit
        If row < 2 Or row > 7 Or col < 2 Or col > 7 Then Exit Sub
        ' If the symbol is already found (empty), exit
        If Symbols(row, col) = "" Then Exit Sub
        ' Second symbol selection
        If FirstSymbolVisible Then
            FirstSymbolVisible = False
            ' Show second symbol
            Cells(row, col).Value = Symbols(row, col)
            Cells(row, col).Interior.Color = xlNone
            ' Wait one second
            startTime = Timer
            Do While Timer < startTime + 1
                DoEvents
            Loop
            ' Clear both cells
            Cells(row, col).Value = ""
            Cells(row1, col1).Value = ""
            ' Check if pair found
            If Symbols(row, col) = Symbols(row1, col1) Then
                ' Remove pair from the game visually
                Cells(row, col).Interior.Color = xlNone
                Cells(row1, col1).Interior.Color = xlNone
                Symbols(row, col) = ""
                Symbols(row1, col1) = ""
                ' Increase found count
                PairsFound = PairsFound + 1
                ' Check for game end
                If PairsFound = 18 Then
                    GameStarted = False
                    MsgBox "Game Over", , "End"
                End If
            Else
                ' Hide symbols again by coloring gray
                Cells(row, col).Interior.Color = RGB(192, 192, 192)
                Cells(row1, col1).Interior.Color = RGB(192, 192, 192)
            End If
        ' First symbol selection
        Else
            FirstSymbolVisible = True
            Cells(row, col).Value = Symbols(row, col)
            Cells(row, col).Interior.Color = xlNone
            row1 = row
            col1 = col
        End If
    End If
End Sub

Explanation:

  • The module-level variables z1 and s1 store the row and column numbers of the first selected cell.
  • The event procedure Worksheet_SelectionChange() is triggered whenever the user clicks a new cell. Variables z and s extract the row and column numbers from the clicked cell’s address.
  • The variable Startzeit stores the time at which a waiting loop starts.
  • The Target object represents the selected range. Its .Address property returns the address string of the clicked cell or merged cell range.
  • If the user clicks the START button cell (address $I$2:$K$2), the Starten procedure is called to initialize the game.
  • If the user clicks the INFO button cell (address $I$7:$K$7), a message box with the game instructions is displayed.
  • For other clicked cells, the row (z) and column (s) are determined by extracting parts of the address string with the functions Mid(), Val(), and Asc().
    • Mid() extracts substrings.
    • Val() converts a string to an integer.
    • Asc() returns the ASCII code of a character; subtracting 64 converts column letters (A=1, B=2, etc.) to numeric indices.
  • If the clicked cell lies outside the game board (rows or columns outside 2 to 7), the procedure exits immediately.
  • If the symbol at the clicked cell has already been found (empty string), the procedure also exits. This enforces synchronization between the symbol array and the worksheet cells.
  • The procedure distinguishes whether the clicked cell is the first or second in a pair selection by checking the Boolean ErstesZeichenSichtbar.
  • When the second cell is selected:
    • The second symbol is shown.
    • The code waits for one second (using a loop checking the system timer).
    • Then, both cells are cleared.
    • If the symbols match, both cells are visually removed (background cleared), the symbol array entries cleared, and the count of found pairs incremented.
    • If all 18 pairs are found, the game ends, and the Gestartet flag is reset so a new game can be started.
    • If no match, both cells are covered again with the gray background.
  • When the first cell is selected:
    • Its symbol is revealed and background cleared.
    • The row and column indices are saved for later comparison.

Enjoy playing the game, exploring the code, and programming your own extensions! For example, instead of 18 pairs with no background color, you could use 6 pairs with 3 different background colors. Then the player must remember not only the symbol but also the background color it appeared on.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx