unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
const
MaxMemoryArray = 32;
type
TMemoryArray =
Array[1..MaxMemoryArray]
of Integer;
TForm1 =
class(TForm)
Button1: TButton;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
SpeedButton9: TSpeedButton;
SpeedButton10: TSpeedButton;
SpeedButton11: TSpeedButton;
SpeedButton12: TSpeedButton;
SpeedButton13: TSpeedButton;
SpeedButton14: TSpeedButton;
SpeedButton15: TSpeedButton;
SpeedButton16: TSpeedButton;
SpeedButton17: TSpeedButton;
SpeedButton18: TSpeedButton;
SpeedButton19: TSpeedButton;
SpeedButton20: TSpeedButton;
SpeedButton21: TSpeedButton;
SpeedButton22: TSpeedButton;
SpeedButton23: TSpeedButton;
SpeedButton24: TSpeedButton;
SpeedButton25: TSpeedButton;
SpeedButton26: TSpeedButton;
SpeedButton27: TSpeedButton;
SpeedButton28: TSpeedButton;
SpeedButton29: TSpeedButton;
SpeedButton30: TSpeedButton;
SpeedButton31: TSpeedButton;
SpeedButton32: TSpeedButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
FMemoryArray : TMemoryArray;
procedure FillArray;
procedure Scramble;
procedure SetSpeedButtons;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Randomize;
// Zufallsgenerator initialisieren
FillArray;
Scramble;
SetSpeedButtons;
end;
procedure TForm1.FillArray;
var
iCnt : Integer;
jCnt : Integer;
Ok : Boolean;
begin
{Die Schleife muss nur über das halbe Array laufen, da ja Paare im Array
stehen sollen.}
For iCnt:=Low(FMemoryArray)
to High(FMemoryArray)
div 2
do
begin
Repeat
Ok:=True;
FMemoryArray[iCnt]:=Random(100);
// liefere Zahlen im Bereich 0 bis 99
//Prüfe, ob die Zahl im Array vorhanden ist
For jCnt:=Low(FMemoryArray)
to iCnt-1
do
begin
If FMemoryArray[iCnt]=FMemoryArray[jCnt]
then
begin
Ok:=False;
// Wenn vorhanden, dann Ok:=False damit neue Zahl "gezogen wird"
Break;
end;
// If FMemoryArray[iCnt]=FMemoryArray[jCnt] then
end;
// For jCnt:=Low(FMemoryArray) to iCnt-1 do
Until Ok;
{Schreibe den "Partner" an die Stelle iCnt+High(FMemoryArray) div 2 im Array.
Dadurch wird das Paar erzeugt.}
FMemoryArray[iCnt+High(FMemoryArray)
div 2]:=FMemoryArray[iCnt];
end;
// For iCnt:=Low(FMemoryArray) to High(FMemoryArray) div 2 do
end;
procedure TForm1.Scramble;
var
iCnt : Integer;
jCnt : Integer;
Swap : Integer;
begin
For iCnt:=Low(FMemoryArray)
to High(FMemoryArray)
do
begin
jCnt:=Random(High(FMemoryArray))+1;
// wähle einen zufälligen Index in FMemoryArray
Swap:=FMemoryArray[iCnt];
FMemoryArray[iCnt]:=FMemoryArray[jCnt];
FMemoryArray[jCnt]:=Swap;
end;
end;
procedure TForm1.SetSpeedButtons;
var
iCnt : Integer;
SpeedButton : TSpeedButton;
begin
For iCnt:=Low(FMemoryArray)
to High(FMemoryArray)
do
begin
SpeedButton:=TSpeedButton(FindComponent('
SpeedButton'+IntToStr(iCnt)));
SpeedButton.Caption:=IntToStr(FMemoryArray[iCnt]);
end;
// For iCnt:=Low(FMemoryArray) to High(FMemoryArray) div 2 do
end;
end.