Finance

Charts

Statistics

Macros

Search

Starting the Game in Excel VBA

In Module1, you will find several variables and an array declared with workbook-wide scope, along with the procedure Starten(). These serve to initialize the game and control its state.

Variables:

Public Symbols(2 To 7, 2 To 7) As String
Public Started As Boolean
Public FirstSymbolVisible As Boolean
Public Found As Integer

Explanation:

  • These variables and the array are declared at the module (workbook) level because they need to be accessed by multiple procedures in different modules or different calls of the same procedure.
  • The two-dimensional array Zeichen has 6 rows and 6 columns (indices 2 to 7 for both dimensions). It holds the 18 pairs of matching symbols distributed randomly on the game board.
  • The Boolean variable Gestartet prevents the game from being started more than once if it is already running.
  • The Boolean variable ErstesZeichenSichtbar indicates whether the first symbol is currently visible. This helps determine if the user has just clicked the first or the second cell.
  • The Integer variable Gefunden counts how many pairs have been found so far. When it reaches 18, the game is over.

The procedure Starten():

' Global variables
Dim Symbols(1 To 8, 1 To 8) As String
Dim Started As Boolean
Dim FirstSymbolVisible As Boolean
Dim Found As Integer
Sub StartGame()
    Dim row As Integer, col As Integer
    Dim i As Integer
    ' Variables for shuffling
    Dim row1 As Integer, col1 As Integer
    Dim row2 As Integer, col2 As Integer
    Dim temp As String
    ' Prevent restarting if already started
    If Started Then Exit Sub
    Started = True
    ' Fill array with pairs of characters (A to R, each appears twice)
    row = 2
    col = 2
    For i = 1 To 18
        Symbols(row, col) = Chr(i + 64)         ' ASCII code for letters A to R
        Symbols(row, col + 1) = Chr(i + 64)
        col = col + 2
        If col > 7 Then
            row = row + 1
            col = 2
        End If
    Next i
    ' Initialize random number generator
    Randomize
    ' Shuffle array elements by swapping pairs 180 times
    For i = 1 To 180
        row1 = Int(Rnd * 6 + 2)
        col1 = Int(Rnd * 6 + 2)
        row2 = Int(Rnd * 6 + 2)
        col2 = Int(Rnd * 6 + 2)
        temp = Symbols(row1, col1)
        Symbols(row1, col1) = Symbols(row2, col2)
        Symbols(row2, col2) = temp
    Next i
    ' Cover all game board cells and fill with gray background
    For row = 2 To 7
        For col = 2 To 7
            Cells(row, col).Value = ""
            Cells(row, col).Interior.Color = RGB(192, 192, 192)
        Next col
    Next row
    ' Set initial states
    FirstSymbolVisible = False
    Found = 0
End Sub

Explanation:

  • The integer variables z, s, and i are used for filling the two-dimensional array.
  • The variables z1, s1, z2, and s2 plus the string variable Tausch are used for swapping elements during the shuffle.
  • The Boolean variable Gestartet is checked to prevent restarting the game if it is already running; if it’s the first start, it is set to True.
  • The array is filled row-wise: first two elements with the letter « A » (ASCII 65), then two elements with « B » (ASCII 66), and so on. After placing three pairs in one row, it moves to the next row.
  • The Randomize statement initializes the random number generator.
  • The shuffle process swaps pairs of elements 180 times at random positions to thoroughly mix the array contents.
  • All cells of the game board are cleared and filled with a gray background color.
  • Initial values are set: no symbol is visible yet, and the number of found pairs is zero.
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