Content of the lesson:
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]
You need to store this information during the gameplay:
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:
You need to know this information about each card:
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:
typedef struct {
int obrazek;
int viditelna;
int odebrana;
} karticka;
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.
#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.
karticka p[n][n];
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:
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.
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].
#define maxhracu 4
We will use a 1D array data type for storing the details about players.
hrac ph[maxhracu];
You have defined all needed data types so you can start writing the program now.
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).
#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;
}