Класс TDomino.
TDomino = object
SideA,SideB:byte;
State:boolean;
end;
Класс TImage.
TImg = class (TImage)
private
State : boolean;
end;
А также класс формы, обрабатывающий интерфейс программы.
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label2: TLabel;
DirectoryListBox1: TDirectoryListBox;
Label3: TLabel;
Image1: TImage;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
Path : string;
end;
Процедура DrawIMGH, рисующая результаты вычисления на форме.
Procedure DrawIMGH();
var
i,a,b,j:byte;
begin
j:=1;
for I := 1 to 28 do
if sp1[i].State=true then
begin
a:=sp1[i].SideA;
b:=Sp1[i].SideB;
Img[j].Left:=left+2+img[i].Width;
left:=img[j].Left;
If (left>form1.Width) or (left+img[i].Width>form1.Width-8) then
begin
form1.Width:=form1.Width+3;
left:=0;
Img[j].Left:=left;
top:=top+img[j].Height+5;
end;
img[j].Top:=top; img[j].Picture.LoadFromFile(Form1.Path+'\IMG\Horizontal\'+
inttostr(A)+inttostr(B)+'.bmp');
img[j].State:=true;
inc(j);
end;
end;
Процедура Tform1.Button1Click запускает обработку .
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,count:byte;
begin
count:=0;
Label1.Caption:='00';
for i:=1 to 28 do
Img[i].Destroy;
for i:=1 to 28 do
Img2[i].Destroy;
CreateImgMass;
init;
curent.SideA:=0;
Curent.SideB:=0;
Curent.State:=true;
GetVariants;
For I:=1 to 28 do
If domino[i].State=false then
inc(count);
Label2.Caption:=inttostr(count);
end;
В процедуру GetVariants мы поместили алгоритм, обрабатывающий все процессы. Это самая большая процедура.
Procedure GetVariants();
Var
i,id,count:byte;
st:boolean;
begin
count:=1;
repeat
st:=false;
if Getnext(curent)=true then
begin
st:=true;
id:=rand(r);
next:=sp2[id];
//****************
for I := 2 to 28 do
if (domino[i].SideA=next.SideA)and(domino[i].SideB=next.SideB) then
domino[i].State:=true;
//******************
prev:=curent;
if prev.SideB<>next.SideA then
if prev.SideB=next.SideB then
begin
next.SideB:=next.SideA;
next.SideA:=prev.SideB;
end;
curent:=next;
sp1[count]:=next;
inc(count);
Form1.Label1.caption:=Form1.Label1.Caption+'->';
Form1.Label1.caption:=Form1.Label1.Caption+inttostr(curent.SideA)+inttostr(curent.SideB);
end;
until (st=false);
end;
И, наконец, функция GetNext(cur:TDomino):boolean, ищущая подходящие свободные костяшки.
Function GetNext(cur:TDomino):boolean;
Var
i ,j: byte;
begin
r:=0;
j:=1;
result:=false;
For I:=2 to 28 do
If Domino[i].State = false then
if (cur.SideB = Domino[i].SideB)
or (cur.SideB = Domino[i].SideA) then
begin
sp2[j]:=domino[i];
sp2[j].State:=true;
result:=true;
inc(r);
inc(j);
end;
end; |