r/visualbasic • • Jun 17 '17

Memory Game ( Concentration )

Hello, I'm in need of help with trying to make a Memory game, 4x4 with score and guesses. If anyone knows a guide on how to make one or can help me please let me know. thanks! :)

5 Upvotes

7 comments sorted by

2

u/ViperSRT3g Application Specialist Jun 17 '17

What planning have you done so far? Do you have any idea where to start on this project?

1

u/xKusznir Jun 17 '17

I've made the design, just really confused on how and where to start with the coding. http://imgur.com/a/6QCrT

  • Thanks for the reply

2

u/ViperSRT3g Application Specialist Jun 17 '17

Oh goodie, this will be fun explaining (But bear with me, I don't have VS installed on my laptop so I can merely give you pseudocode for the time being)

Do you understand the concept behind arrays? They are crucial to being able to manage your data as you're working with a minimum of 16 different values, and 8 pairs of identical values. And before we can go onto how you would make everything work together, you would have to understand arrays.

1

u/xKusznir Jun 17 '17

I understand basic arrays, As long as its not super complicated i should be fine haha

2

u/ViperSRT3g Application Specialist Jun 17 '17

Okie dokie, so the basis behind this is you will have a multidimensional array that keeps track of your card values and where they are located in the grid as you would want to randomize them.

Dim CardArray(15, 1) As Long 'This holds 16 pairs of values, one is the unique identifier of the index, and the other for randomizing

This would create an array for all 16 cards, and allow you to assign two values to each one, the card value, and the random number. Here's some example code that would iterate through this and generate unique value pairs for the entire array, while also generating randomized values for each index's second value.

Private Sub RandomizeCards()
    Dim Counter As Long
    For Counter = 0 To UBound(CardArray, 1)
        CardArray(Counter, 0) = 2 * (Counter - (Counter Mod 2)) 'Generates pairs of identical numbers
        CardArray(Counter, 1) = Random(0, 1000)                 'Stores a randomly generated number for randomization later
        Debug.Print CardArray(Counter, 0) & ", " & CardArray(Counter, 1)
    Next Counter
End Sub

Public Function Random(Lowerbound As Integer, Upperbound As Integer) As Integer
    Randomize
    Random = Int(Rnd * Upperbound) + Lowerbound
End Function

The code below now sorts your array by using the randomized values. After the array is sorted, you will essentially have "shuffled" your cards.

Private Sub SortArray()
    Dim Counter As Long
    Dim Sorted As Boolean
    Do While Sorted = False
        Sorted = True
        For Counter = 0 To (UBound(CardArray, 1) - 1)
            If CardArray(Counter, 1) > CardArray(Counter + 1, 1) Then
                Dim TempCard As Byte
                Dim TempRandom As Long
                TempCard = CardArray(Counter, 0)
                TempRandom = CardArray(Counter, 1)
                CardArray(Counter, 0) = CardArray(Counter + 1, 0)
                CardArray(Counter, 1) = CardArray(Counter + 1, 1)
                CardArray(Counter + 1, 0) = TempCard
                CardArray(Counter + 1, 1) = TempRandom
                Sorted = False
            End If
        Next Counter
    Loop
End Sub

Since you're using .NET, you can also use the Array.Sort method, but I figured I'd throw in a manual sort to show you one way of how to achieve that. Be sure you understand all of what the code is doing so far before we continue on.

1

u/imguralbumbot Jun 17 '17

Hi, I'm a bot for linking direct images of albums with only 1 image

https://i.imgur.com/n8lt5l0.png

Source | Why? | Creator | ignoreme | deletthis

1

u/xKusznir Jun 17 '17

I'm really new to coding