unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure mriezka;
    procedure miesaj;
    procedure posun(x:integer);
    procedure vypis(a,b:integer;st:string);
    procedure Button2Click(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure kontrola;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  g:TCanvas;
  p:array[1..4,1..4] of integer;
  i,j,k,l,x1,y1:integer;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
halt
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
g:=image1.canvas;
mriezka;
miesaj;
end;

procedure TForm1.mriezka;
begin
  for i:=0 to 3 do
    for j:=0 to 3 do
     g.rectangle(i*50,j*50,i*50+50,j*50+50);
end;

procedure TForm1.miesaj;
var c,d:integer;
begin
 for i:=0 to 3 do
    for j:=0 to 3 do
       p[i+1,j+1]:=i*4+j+1;
x1:=4;
y1:=4;
 for k:=1 to 100 do
     begin
       l:=random(4)+1;
       posun(l);

     end;
c:=20;
d:=20;
edit2.text:=inttostr(p[2,1]);
 for i:=0 to 3 do
    for j:=0 to 3 do
    begin
       if p[i+1,j+1]=16 then vypis(i*50+c,j*50+d,' ')
      else  vypis(i*50+c,j*50+d,inttostr(p[i+1,j+1]));
     end;
end;

procedure TForm1.posun(x:integer);
var pom:integer;
begin
 case x of
   1: if x1-1>0 then begin
                      pom:=p[x1-1,y1];
                      p[x1-1,y1]:=p[x1,y1];
                      p[x1,y1]:=pom;
                      x1:=x1-1;
                      end;
   2: if x1+1<5 then begin
                      pom:=p[x1+1,y1];
                      p[x1+1,y1]:=p[x1,y1];
                      p[x1,y1]:=pom;
                      x1:=x1+1;
                      end;
   3: if y1-1>0 then begin
                      pom:=p[x1,y1-1];
                      p[x1,y1-1]:=p[x1,y1];
                      p[x1,y1]:=pom;
                      y1:=y1-1;
                      end;
   4: if y1+1<5 then begin
                      pom:=p[x1,y1+1];
                      p[x1,y1+1]:=p[x1,y1];
                      p[x1,y1]:=pom;
                      y1:=y1+1;
                      end;
end;
end;

procedure TForm1.vypis(a,b:integer;st:string);
begin
 g.TextOut(a,b,st );
 end;
procedure TForm1.Button2Click(Sender: TObject);
begin
mriezka;
 miesaj;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var c,d,e:integer;
begin
i:=x div 50;
j:=y div 50;
i:=i+1;
j:=j+1;
if (abs(i-x1)=1) xor (abs(j-y1)=1) then
begin
e:=i-x1;
if e<>0 then begin
              if e=1 then posun(2);
              if e=-1 then posun(1);
             end;
if e=0 then begin
              e:=j-y1;
              if e=1 then posun(4);
              if e=-1 then posun(3);
            end;
end;
mriezka;
c:=20;
d:=20;
 for i:=0 to 3 do
    for j:=0 to 3 do
    begin
         if p[i+1,j+1]=16 then vypis(i*50+c,j*50+d,' ')
      else  vypis(i*50+c,j*50+d,inttostr(p[i+1,j+1]));
    end;
kontrola;
end;
procedure Tform1.kontrola;
var f:integer;
begin
f:=0;
for i:=0 to 3 do
    for j:=0 to 3 do
          if  p[i+1,j+1]=i*4+j+1 then inc(f);
edit1.text:=inttostr(f);
 if f=16 then messagedlg('Vyhral si',mtinformation,[mbok],0) ;
    end;
end.