Shuffling 20 Cards in GB Studio 4


Here is how I implemented card-shuffling in GB Studio 4.x.

The Basic Idea

For my purposes, a simple card-matching game, I only need to know what order the cards are in within the deck. A simple way to achieve this is to have an array in memory that stores the card values.

At the time of this implementation GB Studio does not expose array operations in either GBVM or visual scripting, but we can achieve the same thing useing GBVM Indirection.

Allocating the Deck

The first step is designating a place to store the deck in memory.

There are several ways to do this, but I chose to name 20 consecutive variables in the editor.

I did this so that the compiler wouldn’t change their locations on me; this was practical because I had a small deck of only 20 cards.

A screenshot of the GB Studio editor showing twenty variables in a row, all named according to the pattern ‘Card ,’.

(The naming pattern here, Card _,_, encodes the layout I’ll be using in this game, but a simpler naming scheme of Card __ is certainly viable.)

Storing the Deck

The second step is initializing the deck.

I used a simple visual script to populate the array. This only needs to happen once.

In most cases it’s easiest to do this via a loop since most decks will need a unique occurence of set consecutive values. But in my case I needed specific varying numbers of repeat values, so I added each value one-by-one.

A screenshot of the GB Studio editor showing a group of operations that assign numerical IDs to the sequence of deck variables.

The most important thing to grasp here is that we are storing card IDs. It’s the job of the rest of the game to know what card each number represents.

Another wrinkle to keep track of is that the array does not begin at index 0 or 1.

Instead, it begins at the address of the first named Card variable. I access that location using the equivalent offset into global variable memory, index 4.

This offset is the default name of the variable in GB Studio. “Variable 28”, for example, would be at offset 28.

The Shuffling Algorithm

Finally, the actual shuffling.

Thankfully, researchers have already worked out how best to do this sort of shuffling in a limited digital system.

Taking their lead, I implemented the Fisher-Yates/Knuth shuffling algorithm, modified for GB Studio 4.x

This algorithm is about as basic as shuffling can get: you merely pick cards at random and place them one after another.

To do this in place, that is using only one array, we just have to swap the position of the current card with one of the cards that hasn’t yet been shuffled:

A screenshot of the GB Studio editor showing how to implement a simple shuffling routine.

(Recall that the deck consists of 20 cards, stored in the global variable heap from index 4 to 24.)

Notice that I coded most of this in visual scripting except for the actual swap. For my purposes this was the simplest way to do things, but you could put everything in GBVM if you wanted.

For the swap itself I use the postfix calculator to pop and push the values pointed to by i and j onto/off of the VM’s stack. This technique was suggested by toxa.

This is the same algorithm in pseudo-C:

 for (byte i = high_index;
      i > low_index; i--)
 {
     j = random(from low_index to i);
     push(array[i]);
     push(array[j]);
     array[i] = pop();
     array[j] = pop();
 }

You can see this in the context of a simple sample game at my Memory GBS4 repo.

Files

Memory GBS4 1.0 ROM 128 kB
4 days ago
Memory GBS4 1.0 Pocket 128 kB
4 days ago

Get Memory: GBS4 Edition

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.