As promised, here the whole code (notice how much comments are in, even to visually separate blocks):
unit UnitMain; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,TypInfo; //Constants Const MAX_PLAYERS = 10; //Types///////////////////////////////////////////////////////////////////////// type TSuit = (Hearts, Diamonds, Clubs, Spades); type TRank = (Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King); type TCard = record Suit : TSuit; Rank : TRank; end; type TPlayer = record Name : string; Cards : array of TCard; //Dynamic array of Cards . //We need to do : SetLength(Hand, imax+1); //in order to redefine the array legth and //then add a new element into that array end; type TTable = array of TPlayer; //Dynamic array of Players. type { TFormMain } TFormMain = class(TForm) ButtonStart: TButton; Memo: TMemo; procedure ButtonStartClick(Sender: TObject); private { private declarations } public { public declarations } end; var FormMain: TFormMain; GlobalInteger : integer; //A global Variable declaration implementation {$R *.lfm} { TFormMain } procedure DuplicateProcedure(v:integer); var LocalResult:integer; //Local variable begin v:=2*v; //Local variable scope ! LocalResult:=v; FormMain.Memo.Lines.Add('The procedure tells : The local result is : ' + inttostr(LocalResult)); end; function DuplicateFunction(v:integer):integer; //response var LocalResult:integer; //Local variable begin v:=2*v; //Local variable scope ! LocalResult:=v; result := LocalResult; end; procedure DuplicateProcedureGlobalVariable(); begin GlobalInteger:=2*GlobalInteger;; FormMain.Memo.Lines.Add('The procedure tells : The local result is : ' + inttostr(GlobalInteger)); end; procedure ShowCardTable(T:TTable); var i,j:integer; S1,S2:string; begin for i:=0 to Length(T) - 1 do begin FormMain.Memo.Lines.Add('The player at the position ' + inttostr(i+1) + ' is ' + T[i].Name + ' and has '+ inttostr(Length(T[i].Cards)) + ' cards : '); for j := 0 to Length(T[i].Cards) - 1 do begin S1 := GetEnumName(TypeInfo(TSuit),integer(T[i].Cards[j].Suit)); S2 := GetEnumName(TypeInfo(TRank),integer(T[i].Cards[j].Rank)); FormMain.Memo.Lines.Add('The card number ' + inttostr(j+1) + ' is ' + S1 + ' - ' + S2); end; end; end; //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //MAIN PROGRAM procedure TFormMain.ButtonStartClick(Sender: TObject); var LocalInteger : integer; //Local variables declarations S1,S2 : string; i : integer; Players : array [1..9] of string; //Static array UnlimitedPlayers : array of string; //Dynamic array //types BigTable : TTable; John,Mary,Smith : TPlayer; begin //Comments//////////////////////////////////////////////////////////////////// //Comment single line { Comment Multi line ... } //Constants/////////////////////////////////////////////////////////////////// //MAX_PLAYERS := 11; //THIS IS INCORRECT You can not assign a constant //Variables/////////////////////////////////////////////////////////////////// //Inizialization (assigning variables) LocalInteger := 0; S1 := 'String 1'; S2 := 'String 2'; //Print integer Memo.Lines.Add('Your local integer is : ' + inttostr(LocalInteger)); Memo.Lines.Add('Your constant value of max players is : ' + inttostr(MAX_PLAYERS)); //Strings concatenation Memo.Lines.Add(S1 + ' , ' + S2); //If then else//////////////////////////////////////////////////////////////// Randomize; i:=Random(100); if i<30 then begin Memo.Lines.Add('The number ' + inttostr(i) + ' is small'); end else if (i>=30) and (i<60) then begin Memo.Lines.Add('The number ' + inttostr(i) + ' is medium'); end else begin Memo.Lines.Add('The number ' + inttostr(i) + ' is big'); end; //Loops/////////////////////////////////////////////////////////////////////// //For Loop for i:=0 to 10 do begin LocalInteger:=LocalInteger + 1; end; Memo.Lines.Add('Incremented with for. Your integer is now : ' + inttostr(LocalInteger)); //Will be 11 for i:=10 downto 0 do begin LocalInteger:=LocalInteger - 1; end; Memo.Lines.Add('Decremented with for. Your integer is now : ' + inttostr(LocalInteger)); //Will be 0 //While loop While i <= 10 do begin LocalInteger:=LocalInteger + 1; i := i + 1; // Increment the number end; Memo.Lines.Add('Incremented with while. Your integer is now : ' + inttostr(LocalInteger)); //Will be 11 //Procedure & Function /////////////////////////////////////////////////////// //This is a procedure call . (I do not get a response) DuplicateProcedure(LocalInteger); //Will get 22 (11*2) //This is a function call. (I do get a response) Memo.Lines.Add('The function produced : ' + inttostr(DuplicateFunction(LocalInteger))); //Will get 22 (11*2) ////////////////////////////////////////////////////////////////////////////// //Scopes //Local Scope : As you can see the v and LocalResult local variable // which is located internally at the Duplicate Procedure/Function // dies after the end of the procedure / Function. DuplicateProcedure(LocalInteger); //Will get 22 (11*2) Memo.Lines.Add('The function produced : ' + inttostr(DuplicateFunction(LocalInteger)) ); //Will get 22 (11*2) Memo.Lines.Add('The local scope demonstration : ' + inttostr(LocalInteger)); //Will be 11 //Global Scope : As you can see in this case we do not pass a variable since it is a global variable // The global variable changed internally from the Procedure/Function // will keep it's value and after the procedure/function execution. GlobalInteger:=40; DuplicateProcedureGlobalVariable(); //Will get 80 Memo.Lines.Add('The global scope demonstration : ' + inttostr(GlobalInteger)); //80 will remain ////////////////////////////////////////////////////////////////////////////// //arrays //1) Fixed arrays //a) Assign an array of players for i:=1 to 9 do begin Players[i]:='Player number' + inttostr(i); end; //b) Read array Memo.Lines.Add(Players[2]); //2)Dynamic arrays //a) Assign a dynamic array SetLength(UnlimitedPlayers, Length(UnlimitedPlayers)+1 ); //Increase the array by one element UnlimitedPlayers[0]:= 'First player'; //The first element at a dynamic array is at index 0 SetLength(UnlimitedPlayers, Length(UnlimitedPlayers)+2 ); //Increase the array by two elements UnlimitedPlayers[1]:= 'Second player'; UnlimitedPlayers[2]:= 'Third player'; //b) Read array Memo.Lines.Add(UnlimitedPlayers[0]); Memo.Lines.Add(UnlimitedPlayers[1]); Memo.Lines.Add(UnlimitedPlayers[2]); //c) Remove last element SetLength(UnlimitedPlayers, Length(UnlimitedPlayers)-1 ); //d) Read array again Memo.Lines.Add(UnlimitedPlayers[0]); Memo.Lines.Add(UnlimitedPlayers[1]); //Memo.Lines.Add(UnlimitedPlayers[2]); // this element is deleted ////////////////////////////////////////////////////////////////////////////// //types //Assign the three players John.Name:='John Dangerous'; SetLength(John.Cards, Length(John.Cards)+4 ); //John has 4 cards John.Cards[0].Suit:=Spades; John.Cards[0].Rank:=Ten; John.Cards[1].Suit:=Hearts; John.Cards[1].Rank:=Ace; John.Cards[2].Suit:=Diamonds; John.Cards[2].Rank:=Two; John.Cards[3].Suit:=Diamonds; John.Cards[3].Rank:=Seven; Mary.Name:='Mary Lucky'; SetLength(Mary.Cards, Length(Mary.Cards)+2 ); //Mary has 2 cards Mary.Cards[0].Suit:=Clubs; Mary.Cards[0].Rank:=Nine; Mary.Cards[1].Suit:=Hearts; Mary.Cards[1].Rank:=Eight; Smith.Name:='Smith ugly'; SetLength(Smith.Cards, Length(Smith.Cards)+1 ); //Smith has 1 card Smith.Cards[0].Suit:=Clubs; Smith.Cards[0].Rank:=Six; //Read some of the players cards //Convert index value of type to string S1 := GetEnumName(TypeInfo(TSuit),integer(Mary.Cards[0].Suit)); S2 := GetEnumName(TypeInfo(TRank),integer(Mary.Cards[0].Rank)); Memo.Lines.Add('Mary first card is : ' + S1 + ' - ' + S2); S1 := GetEnumName(TypeInfo(TSuit),integer(John.Cards[2].Suit)); S2 := GetEnumName(TypeInfo(TRank),integer(John.Cards[2].Rank)); Memo.Lines.Add('John third card is : ' + S1 + ' - ' + S2); //John Mary and Smith are sitting on the BigTable table SetLength(BigTable, Length(BigTable)+3 ); //BigTable right now has 3 players BigTable[0]:=John; BigTable[1]:=Mary; BigTable[2]:=Smith; //Read smith the third player - first card S1 := GetEnumName(TypeInfo(TSuit),integer(BigTable[2].Cards[0].Suit)); S2 := GetEnumName(TypeInfo(TRank),integer(BigTable[2].Cards[0].Rank)); Memo.Lines.Add('Smith first card is : ' + S1 + ' - ' + S2); ShowCardTable(BigTable); end; end.
Try it and have fun.
See you on next article!