Arduboy Memory Game #3: Randomized Game Board

In this dev log, I create the game board for my Arduboy Memory Game.

The game board is randomly generated using the random function in the Arduino library.

Selecting random positions for each game card

Since our game board consists of 4 x 6 game cards, each card can be positioned one of 24 positions.

We can use the Arduino random function to get a random number between 0 and 24 (excluding 24) for each of the game cards on the board.

Creating a new random seed

The Arduino random function will always return the same sequence of numbers, if we do not initialize the random generator with a new value when we create a new game board.

To fix this, we use the ‘micros’ function to create a new seed each time a game board is going to be created. The seed is then used to initialize a new random number generator using the ‘randomseed’ function.

Since the ‘micros’ will not always return the exactly same number even the first time the board is created, this solution will creates a new game board every time the game is started.

Position collisions

However, two cards might be assigned the same random number. The question is then how we will handle these collisions.

Example of randomly generated board
Example of randomly generated board

Handling position collisions

When a game card is assigned a position which is already occupied by an earlier assigned game card, we say that have a collision between the two game cards positions.

This problem can be solved in a several ways. For this project I’ve using hashing using linear probing as an inspiration.

Whenever a game card is assigned a position which is already occupied, we just try the next position on the game board. If this is also occupied we continue to the next position on the game board. We continue this way until we encounter the first free position on the game board.

Example. The cactus game card is assigned the position 4, which is occupied

An example is giving in the figure above.

The cactus game card is assigned the position 4 at random. However, this position has already been occupied by a palm tree game card. We then test the next position which is already occupied. We continue this until we find the first unoccupied position and then insert the cactus game card here.

If at any time we need to continue, but has reached the end of the game board, we just wrap around to the position 0. Because we need to insert 2 x 12 game cards and have 24 game positions, we know that there will always be at least one free position for each card we need to insert. This is important because we then know that the algorithm will always end.

By doing this twice for all 12 game cards will result in a randomly generated game board where each card is inserted at two different positions.

The code for this project can be found here on GitHub.

Next up is adding interaction like moving a cursor around and selecting game cards.

/Signe

Dev logs in this series:

  1. Getting Started
  2. Preparing Sprites
  3. Randomized Game Board (this)
  4. User Input And Cursor


Posted

in

, , ,

by

Tags:

Comments

3 responses to “Arduboy Memory Game #3: Randomized Game Board”