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:
karticka = record
obrazek:byte;
viditelna:boolean;
odebrana:boolean;
end;
The whole game environment consists of 36 cards. We need a data structure which can hold all 36 cards - a 2D array is ideal.
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.
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.
var p:pexeso;
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:
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:
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].
const maxhracu=4;
We will use a 1D array data type for storing the details about players.
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.
var ph:hraci;
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).
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.