Czech version
logolink

< Back to the list of lessons

Memory Game I (Analysis)

AlgortimyContent of the lesson:

  • Principle of Game
  • Main Steps of Program
  • Definitions of Data Types (cards, game, players)
  • Program with Defined Data Types

Principle of Game

Memory Game is a card game for two players (at least) which is aimed to memory and concentrations. It is played with a special set of cards which consists of odd number of cards (usually 64 or 36 cards). These cards create couples. Every player has to find as many couples as he can.

The predecessor of the Memory Game can be found in Japan - their traditional game with clams named Kai awase (it is not a card game but can be taken as the predecessor of the Memory Game). During this game the players have to search for two halves of clams inside a heap to be able to put them together. Then different images were drawn on the inner sides - like the Memory Game with clams.

Cards are shuffled and placed on a table (the front side has to be hidden). None of the players know the layout of cards. Players gradually turn any 2 cards and show them to the others. If a player chooses the same two cards he can remove them and continue. If he chooses different cards, he turns them back and places them on their positions on a table. The game is played as long as any card remains. The player with the highest number of removed couples is the winner of this game. [http://cs.wikipedia.org/wiki/Pexeso]

Main Steps of Program

  • Initializing game environment (setting card images and shuffling).
  • Initializing players (setting number of players, their names etc.).
  • The main algorithm - moves are repeated until game is over.

Definitions of Data Types (card, pairs, players)

You need to store this information during the gameplay:

  • Information about each card (which details are needed?).
  • Information about each player (which details are needed?).

Cards

Summarize the basic information about each card. There are usually 36 cards which create a square scheme. How to represent the cards? The data type array (with size of 6x6 items) is the best one.

Every card contains an image (couples contain the same image). You will need 18 different images. In which state can the card be? Every card can described using one of these states:

  • turned to face so you can see the image (we will call this state as "turned")
  • turned to back so you can see the background which is the same for every card ("unturned" state)
  • one of players have already removed this card so the card is no longer visible ("removed" state)

You need to know this information about each card:

  • The location of the card (in the 6x6 array).
  • Which image does it use (image 1-18).
  • The state of each card (turned, unturned, removed).

The question is: What data type should we choose to be able to store all these details for each card? Is there a data type which allows us to store 3 different details about each card?

We will use the data type called record which can contain any number of details for every item. Inside the karticka variable we will define these variables:

  • obrazek - variable storing the number of image from interval 1..18 - every image will be represented by a number. You can use the data type byte because there are only 18 images.
  • viditelna - variable of int data type. proměnná typu int. If it is 1, then the image of this card is visible (it is turned to face). If it is 0, the card is turned to back - players see the background image only.
  • odebrana - this variable is again of boolean data type. It is true in case that this card has already been removed by a player or false in case that it has not been removed yet.
Declaration of data type karticka
typedef struct {
int obrazek;
int viditelna;
int odebrana;
} karticka;

Memory Game

We might want to change the size of array in the future so it is better to define a constant n in the header of the program. This will make the process of changing size of Memory Game very easy (you would have to change only one variable). We will assume that the value of n is lower than equal to 6 in the rest of the lesson.

Declaration of constant representing the size of array
#define n 6

The whole game environment consists of 36 cards. We need a data structure which can hold all 36 cards - a 2D array is ideal.

Declaration of the p array of the type karticka
karticka p[n][n];

Players

 

he Memory Game is usually played by 2-4 players so you have to be able to store the information about any number of players during the game. Think about all details which are needed to be stored for each player:

  • Name - a unique identification of each player. Use the data type string (you can store up to 255 characters).
  • Score - the number of couples of cards which were removed by this player. Use the data type byte (score will 0-18).

You have to define an array again because you do not know the number of players. You will be able to store details about up to 4 players inside this array. Every item will be of the data type struct because you have to store 2 details about each player.

Declaration of the data type hrac
typedef struct {
char jmeno[255];
int skore;
} hrac;

Define a new constant maxhracu. This will allow you to change the size of the array to another value by changing this value only. We will assume that maxhracu is set to interval [2-4].

Declaration of the constant maxhracu to set the dimensions of array hraci
#define maxhracu 4

We will use a 1D array data type for storing the details about players.

Declaration of the variable ph of the data type hraci
hrac ph[maxhracu];

You have defined all needed data types so you can start writing the program now.

Program with Defined Data Types

Create a new console application in Delphi and insert all needed declarations to the source code (do not forget to select a program name which is different from all variables - you cannot use "pexeso" for example).

The first version of the program (data types only)

#include "stdafx.h"

#define n 6
#define maxhracu 4

typedef struct {
  int obrazek;
  int viditelna;
  int odebrana;
} karticka;

typedef struct {
  char jmeno[255];
  int skore;
} hrac;

int _tmain(int argc, _TCHAR* argv[])
{

  karticka p[n][n];
  hrac ph[maxhracu];

  fflush(stdin);
  getchar();
  return 0;
}

webdesign, xhtml, css, php - Mgr. Michal Mikláš