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:

https://www.scirra.com/tutorials/280/creating-a-memory-match-game/page-3

Part 2

Part 3: Shuffling the Cards

(Using a structure variable)

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.11 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' (Add… - Repeat on the top panel).

In the loop we will use a scene variable CurrentDeckCard to tell at which card we are now. (Right click on the name 'Game' in the Project manager, choose 'Modify initial variables', click on the plus sign and enter the new variable CurrentDeckCard = 0). If we don't create it before using it, the GD will automatically create it for us with initial value = 0.

Number of repetitions: Count(card)
Conditions: No conditions
Action:
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:

Action:
Variables - Value of a variable
Variable: CurrentDeckCard (press on the button next to text entry field to choose its name from the list)
Modification's sign: +
Value: 1

The expression Count(card) gives a number of objects 'card' which we have, in our case it will be = 12. Deck[VariableString(CurrentDeckCard)] creates a child variables Deck.0 = 0, Deck.1 = 1…Deck.11 = 11 because the value of CurrentDeckCard in the loop will go from 0 to 11.

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-11. Now we need to connect values in the array with the value of CardFrame of each card.

To do that find a sub-event 'For each object card' under 'At the beginning of the scene' and in the action change

Value: Variable(Deck[card.VariableString(CardID)])

In this loop we assign a value from each child of structure variable Deck to variable CardFrame of each of our cards, and we use card's variable CardID to choose the proper child variable of the structure Deck.

So, card with CardID = 0 will have CardFrame = Deck.0 = 0,
card with CardID = 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' above the loop 'For each card' (we need to shuffle our array Deck before we connect the values in it with CardFrame of each card.)

Numbers of times: 50 (you can experiment with this number)
Conditions: No conditions

Actions:

  1. Randomly select the first child variable.

Action:
Variables - Value of a variable
Variable: Slot1
Modification's sign: =
Value: Random(Count(card)-1)

The function Random will give random numbers from 0 to Count(card)-1 which in our case = 12-1 = 11.

  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.

Action:
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.

Action:
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 you remember what each part of it does.

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 wright: '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 disable the shuffling when we'll test our game.

And we can add another comment before the second event where we check if the card is clicked.

It'll be: “Mouse event”.

Our events shall look like this:

The source file: https://drive.google.com/file/d/0B4iAICUekZv0UEl6MVNmeV9lYnc/view?usp=sharing

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

Part 4