unit uProg4;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TfTilitoli = class(TForm)
    pPanel: TPanel;
    bKever: TButton;
    bKilep: TButton;
    procedure bKilepClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure GombClick(Sender: TObject);
    procedure Keveres;
    procedure bKeverClick(Sender: TObject);
  private
     Gombok : Array[1..3,1..3] Of TButton;
     LyukX, LyukY : Integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fTilitoli: TfTilitoli;

implementation

{$R *.DFM}

procedure TfTilitoli.bKilepClick(Sender: TObject);
begin
Close;
end;

procedure TfTilitoli.FormCreate(Sender: TObject);
Var i,j:Integer;
begin
   For i:=1 To 3 Do
      For j:=1 To 3 Do
         Begin
         Gombok[i,j] := TButton.Create(Self);
         With Gombok[i,j] Do
            Begin
            Parent := pPanel;
            Left := (j-1)*(pPanel.Width Div 3);
            Top := (i-1)*(pPanel.Height Div 3);
            Width := pPanel.Width Div 3;
            Height := pPanel.Height Div 3;
            OnClick := GombClick;
            End;
         End;
   Keveres;
end;

Procedure TfTilitoli.Keveres;
Var i,j:Integer;
    SHalmaz : Set Of 0..8;
    Sz : Integer;
Begin
   SHalmaz := [];
   For i:=1 To 3 Do
      For j:=1 To 3 Do
         Begin
         Repeat
            Sz := Random(9);
         Until Not (Sz In SHalmaz);
         SHalmaz := SHalmaz + [Sz];
         Gombok[i,j].Caption := IntToStr(Sz);
         If Sz=0 Then
            Begin
               Gombok[i,j].Visible := False;
               LyukX := j;
               LyukY := i;
            End
         Else
               Gombok[i,j].Visible := True;
         End;
End;

procedure TfTilitoli.GombClick(Sender: TObject);
   Function Lyukszomszed (Gomb : TButton) : Boolean;
      Var Lyuk : TButton;
      Begin
          Lyuk := Gombok[LyukX,LyukY];
          LyukSzomszed := (( Gomb.Left = Lyuk.Left) And
                            (Abs(Gomb.Top - Lyuk.Top) = Gomb.Height) )
                          Or
                          (( Gomb.Top = Lyuk.Top) And
                            (Abs(Gomb.Left - Lyuk.Left) = Gomb.Width));


      End;
   Var Seged : Integer;
Begin

   If Lyukszomszed (Sender as TButton) Then
      Begin
      Seged := (Sender as TButton).Left;
      (Sender as TButton).Left := Gombok[LyukX,LyukY].Left;
      Gombok[LyukX,LyukY].Left := Seged;
      Seged := (Sender as TButton).Top;
      (Sender as TButton).Top := Gombok[LyukX,LyukY].Top;
      Gombok[LyukX,LyukY].Top := Seged;
      End;
End;

procedure TfTilitoli.bKeverClick(Sender: TObject);
begin
Keveres;
end;

end.
