program kocek;
uses crt, graph;
var ster,tryb,x,y,i,j: integer;
    klawisz: char;
    t: array[0..63,0..47] of boolean;  {matryca ruchu}
    const maxx=63;
          maxy=47;
function jopek(x,y:integer):integer;
begin
     x:=x*10;
     y:=y*10;
     setfillstyle(1,15);
     bar(x,y,x+9,y+9);
{     setfillstyle(1,12);
     bar(x+10,y+10,x+30,y+30);}
end;

function zamjop(x,y:integer):integer;
begin
     x:=x*10;
     y:=y*10;
     setfillstyle(1,0);
     bar(x,y,x+9,y+9);
{     setfillstyle(1,0);
     bar(x+10,y+10,x+30,y+30);}
end;

function przeszkoda(a,b,c,d:integer):integer;
var i,j: integer;
begin
     setfillstyle(1,blue);
     bar(a*10,b*10,c*10-1,d*10-1);
     for i:=a to c-1 do
     for j:=b to d-1 do
     t[i,j]:=true;
end;

Begin
for i:=0 to maxx do
for j:=0 to maxy do
t[i,j]:=false;

ster:=detect;
initgraph(ster,tryb,'c:\bp\bgi');
{Matryca ruchu: x (0 do 63), y (0 do 47)}

x:=0;      {poczatkowy punkt ruchu}
y:=0;

jopek(x,y);

przeszkoda(0,9,maxx div 2,10);
przeszkoda(maxx div 2,20,maxx,21);
przeszkoda(0,29,maxx div 2,30);
repeat
  klawisz:=readkey;
{}      if (klawisz='6') and not t[x+1,y] then
      begin
           zamjop(x,y);
           x:=x+1;
           if x=maxx then x:=0;
      end;
      if (klawisz='6') and t[x+1,y] then
      begin sound(200); delay(100); nosound; end;
{}      if (klawisz='4') and not t[x-1,y] then
      begin
           zamjop(x,y);
           x:=x-1;
           if x=-1 then x:=maxx-1;
      end;
      if (klawisz='4') and t[x-1,y] then
      begin sound(200); delay(100); nosound; end;
{}      if (klawisz='8') and not t[x,y-1] then
      begin
           zamjop(x,y);
           y:=y-1;
           if y=-1 then y:=maxy-1;
      end;
      if (klawisz='8') and t[x,y-1] then
      begin sound(200); delay(100); nosound; end;
{}      if (klawisz='2') and not t[x,y+1] then
      begin
           zamjop(x,y);
           y:=y+1;
           if y=maxy then y:=0;
      end;
      if (klawisz='2') and t[x,y+1] then
      begin sound(200); delay(100); nosound; end;
if klawisz='q' then exit;
jopek(x,y);
until klawisz='q';

END.