Creating a Memory Match Game in GDevelop

by komencanto

This tutorial is based on the tutorial “Creating a Memory Match Game in Construct 2” by kittiewan:

Part 2

Part 3: Shuffling the Cards

In this part we'll create a structure variable (an array) to hold face frame numbers of the cards. Then we'll use an easy technick to randomize values of our structure variable by swapping the values of randomly chosen pairs of its child variables.

I. Creating a structure variable (an array)

First we need to create a structure variable Deck and fill it's child variables Deck.0-Deck.23 with values of CardFrame of our cards.

Go to Events and under 'At the beginning of the scene' add a new empty sub-event. Then under it add a loop 'Repeat'. In the loop we will use a scene variable CurrentDeckCard to tell at which card we are now. If we didn't create it before, the GD will automatically create it for us with initial value = 0 when we begin to use it .

Number of repetitions: GlobalVariable(gNumberCards)
Conditions: No conditions
Variables - Value of a variable
Variable: Deck[VariableString(CurrentDeckCard)]
Modification's sign: =
Value: Variable(CurrentDeckCard)

Now add the second action to increase the variable CurrentDeckCard by 1 every pass of the loop:

Variables - Value of a variable
Variable: CurrentDeckCard
Modification's sign: +
Value: 1

In this actions Deck[VariableString(CurrentDeckCard)] creates a child variables Deck.0 = 0, Deck.1 = 1…Deck.23 = 23 because the value of CurrentDeckCard in the loop will go from 0 to 23.

II. Connecting the values in the array with object's variable of each card

We've created our array Deck and filled it with numbers 0-23. Now we need to connect values in the array with the value of CardFrame of each card.

To do that in the last action of a sub-event under 'At the beginning of the scene' which says 'Do= Variable(CurrentTableauCard) to variable CardFrame of card' change

Value: Variable(Deck[VariableString(CurrentTableauCard)])

So, a card 0 will have CardFrame = Deck.0 = 0,
a card 1 will have CardFrame = Deck.1 = 1 and so on.

If we run our program now there will be no difference - we'll again get pairs of cards with the same pictures placed side by side (like frames in the Animation 1) because in the array the frame numbers are not yet shuffled.

III. Randomizing the array Deck - shuffling our cards

Now we need to shuffle our cards. To do this we need to shuffle the values in the childs of our structure variable Deck. We'll do it by choosing randomly two childs of Deck and swapping their values.

To do this we'll need to create a couple of scene variables which will hold the names of child variables and the values stored in them.

Right click on the name of our scene Games in the Project manager window.

Choose 'Modify initial variables'.

Add these variables:
Slot1 = 0
Slot2 = 0
Card1 = 0
Card2 = 0

Slot1 and Slot2 will store names of chosen childs of Deck.
Card1 and Card2 will store the values which were in those child variables which will be swapped.

Now, add another loop 'Repeat' after one where we created our structure variable Deck. (It will be the second sub-event of the 'At the beginning of the scene' event and it will be executed before we lay out our cards.)

Numbers of time: 50 (you can experiment with this number)
Conditions: No conditions
1. Randomly select the first child variable.

Variables - Value of a variable
Variable: Slot1
Modification's sign: =
Value: Random(GlobalVariable(gNumberCards)-1)

The function Random will give random numbers from 0 to gNumberCards-1 which in our case = 24-1 = 23.

  1. Randomly select the second child variable

Copy the previous action and change the variable to Slot2.

  1. Get a value from the first child variable and put it into variable Card1.

Variables - Value of a variable
Variable: Card1
Modification's sign: =
Value: Variable(Deck[VariableString(Slot1)])

  1. Do the same with the second child variable and variable Card2.

Copy the previous action and change

Variable: Card2
Value: Variable(Deck[VariableString(Slot2)])

  1. Putting the value in Card1 into the second child which name is stored in the variable Slot2.

Variables - Value of a variable
Variable: Deck[VariableString(Slot2)]
Modification's sign: =
Value: Variable(Card1)

  1. Do the same with the Card2 and the first child variable kept in Slot1.

Copy the previous action and change

Variable: Deck[VariableString(Slot1)]
Value: Variable(Card2)

Now if we run the program you'll see that our cards are shuffled.

IV. Organizing events, adding comments.

Let's organize a little our events.

First, drag the event 'At the beginning of the scene' to the top of our Event sheet. It will do no difference on how our program runs but it will be easier to read it and to know what part of our events run first.

Second, let add some comments to our program to help us remember what each part of our program do.

Find the sub-event 'Repeat 50 times' under 'At the beginning of the scene'.

Select the event before it and add a comment (button 'Add a comment' on the top panel).

In the comment type: 'Shuffling the cards. Can be disabled for testing purposes'.

The comment will be added above the event 'Repeat 50 times' where we shuffle our cards.

It will help us find this place easily when we'll need to temporarily deactivate the shuffling when we'll test our game. (To deactivate this event right click on it and choose De/activate.)

Now let's add another comment right after this event where we laying out our cards.

The comment will be: 'Laying out the cards.'

And before the event when we check if the mouse coursor is on the card add another comment: 'The Mouse event'. (You'll need to drag it down a little to move right before that event).

The source file of this part of the project:

In the next part we will work on the game logic.

Part 4