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 the boolean data type. If it is true, then the image of this card is visible (it is turned to face). If it is false, 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
karticka = record
obrazek:byte;
viditelna:boolean;
odebrana:boolean;
end;

Game Environment

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 data type pexeso
pexeso = array[1..n,1..n] of karticka;

It is possible that you might want to change the number of cards so you can define a constant in the program header and name it n. This allows you to change the number of cards inside the whole program by changing only one character. We will assume that n is set to 6 in the following steps.

Declaration of constant representing the size of array
const n=6;

To be able to use the defined data type pexeso inside our program, we have to declare a variable of this type. You can name it p for example.

Declaration of the variable p of data type pexeso
var p:pexeso;

Players

The 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 record because you have to store 2 details about each player.

Use the record data type for each player and define these variables inside it:

Declaration of the data type hrac
hrac = record
jmeno:string;
skore:byte;
end;

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
const maxhracu=4;

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

Declaration of the data type hraci
hraci=array[1..maxhracu]of hrac;

To be able to use this defined data type in our program you have to declare a variable of this type. Name it ph for example.

Declaration of the variable ph of the data type hraci
var ph:hraci;

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)

program pexeso3po;

{$APPTYPE CONSOLE}

uses
SysUtils;

const
n=6;
maxhracu=4;
type
karticka = record
obrazek:byte;
viditelna:boolean;
odebrana:boolean;
end;
hrac = record
jmeno:string;
skore:byte;
end;
hraci=array[1..maxhracu]of hrac;
pexeso=array[1..n,1..n]of karticka;

var p:pexeso;
ph:hraci;

begin
{ TODO -oUser -cConsole Main : Insert code here }

readln;
end.

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