program joc;
uses crt;
var a:array[1..4,1..4]of integer;
    i,j,k,l,b:integer;
function finish:boolean;
var m,n,v:integer;
    q:boolean;
begin
  v:=0;
  q:=true;
  for m:=1 to 4 do for n:=1 to 4 do
    begin
      v:=v+1;
      if a[m,n]<>v then q:= false;
    end;
  if q then finish:=true
       else finish:=false;
end;
begin
  clrscr;b:=16;
  gotoxy(5,25);
  write('Apasati "q" pentru iesire');
  for i:=1 to 4 do for j:=1 to 4 do begin a[i,j]:=b; b:=b-1; end;
  for i:=1 to 4 do for j:=1 to 4 do if a[i,j]=16 then begin k:=i;l:=j; end;
  for i:=1 to 4 do
    for j:=1 to 4 do
      begin
        gotoxy(5+3*i,5+2*j);
        write(a[i,j]);
      end;
  gotoxy(8,7);
  write('  ');
  i:=k;j:=l;
  repeat
  case readkey of
  'w': if j<4 then j:=j+1;
  'x': if j>1 then j:=j-1;
  'd': if i>1 then i:=i-1;
  'a': if i<4 then i:=i+1;
  'q': exit;
  end;
  a[k,l]:=a[i,j];
  gotoxy(5+3*k,5+2*l);write('  ');
  gotoxy(5+3*k,5+2*l);write(a[k,l]);
  a[i,j]:=16;
  gotoxy(5+3*i,5+2*j);write('  ');
  k:=i;l:=j;
  until finish;
  writeln;
  writeln('Felicitari ! ai terminat jocul');
  writeln('Apasa o tasta ca sa revii la program');
  readkey;
end.