program slam;

var
   {poloha a smer pohybu 1. palky}
   x1,y1,ox1,oy1,a1,b1:integer;
   {poloha a smer pohybu 2. palky}
   x2,y2,ox2,oy2,a2,b2:integer;
   {poloha a smer pohybu micku}
   x,y,ox,oy,a,b:integer;
   f,g,i:integer;
   left,right:integer;
   key:char;

procedure area;
begin
 draw(0,15,0,0,1);
 draw(0,0,239,0,1);
 draw(239,0,239,15,1);
 draw(0,48,0,63,1);
 draw(0,63,239,63,1);
 draw(239,63,239,48,1);
end;

procedure ball(x,y,c:integer);
begin
 draw(x-2,y-1,x-2,y+1,c);
 draw(x+2,y-1,x+2,y+1,c);
 draw(x-1,y-2,x+1,y-2,c);
 draw(x-1,y+2,x+1,y+2,c);
end;

procedure stick(x,y,z,c:integer);
begin
 draw(x,y,x,y+z,c);
 draw(x+1,y,x+1,y+z,c);
end;

procedure testarea(x,y:integer; var a,b:integer);
begin
 if x<5 then a:=-a;
 if x>235 then a:=-a;
 if y<5 then b:=-b;
 if y>59 then b:=-b;
end;

procedure testgoal(x,y:integer; var f,l,r:integer);
begin
 f:=0;
 if x>235 then
  if (y<48) and (y>15) then 
   begin f:=1; l:=l+1; end;
 if x<5 then
  if (y<48) and (y>15) then 
   begin f:=1; r:=r+1; end; 
end;

procedure teststick(x,y,x1,y1,x2,y2:integer; var a,b:integer);
begin
 if (y>y1-2) and (y<y1+12) then
  if (x<x1+5) and (x>x1-4) then a:=-a;
 if (y>y2-2) and (y<y2+12) then
  if (x<x2+5) and (x>x2-4) then a:=-a;
end;

procedure outtest(var x,y:integer);
begin
 if x<3 then x:=3;
 if x>236 then x:=236;
 if y<3 then y:=3;
 if y>51 then y:=51;
end;

procedure speedtest(var a1,b1,a2,b2:integer);
begin
 if a1>2 then a1:=2;
 if a1<-2 then a1:=-2;
 if b1>2 then b1:=2;
 if b1<-2 then b1:=-2;
 if a2>2 then a2:=2;
 if a2<-2 then a2:=-2;
 if b2>2 then b2:=2;
 if b2<-2 then b2:=-2;
end;

begin
 setmode(7);
 writeln('         BaR design presents');
 writeln;
 writeln('               *S*L*A*M*');
 writeln('     [Q]                        [[]');
 writeln('  [Z]   [X]                  [,]   [.]');
 writeln('     [A]                        [;]');
 writeln;
 write('Number of goals (1-9):');
 key:=inkey;
 g:=ord(key)-48;
 left:=0; right:=0;
 for i:=1 to g do begin
  setmode(4);
  area;
  a:=-2; b:=2; x:=120; y:=32;
  ox:=x; oy:=y;
  x1:=10; y1:=28; x2:=228; y2:=25;
  ox1:=x1; oy1:=y1; ox2:=x2; oy2:=y2;
  a1:=0; b1:=0; a2:=0; b2:=0;
  repeat
   speedtest(a1,b1,a2,b2);
   x1:=x1+a1; y1:=y1+b1;
   x2:=x2+a2; y2:=y2+b2;
   outtest(x1,y1);
   outtest(x2,y2);

   stick(x1,y1,10,1);
   stick(ox1,oy1,10,0);
   stick(x1,y1,10,1);

   stick(x2,y2,10,1);
   stick(ox2,oy2,10,0);
   stick(x2,y2,10,1);

   x:=x+a; y:=y+b;
   testgoal(x,y,f,left,right);
   testarea(x,y,a,b);
   teststick(x,y,x1,y1,x2,y2,a,b);
   ball(x,y,1);
   ball(ox,oy,0);
   ball(x,y,1);
   ox:=x; oy:=y;
   ox1:=x1; oy1:=y1;
   ox2:=x2; oy2:=y2;
   if keypressed then begin
    key:=inkey;
    case key of
     'q': b1:=b1-1;
     'a': b1:=b1+1;
     'z': a1:=a1-1;
     'x': a1:=a1+1;
     '[': b2:=b2-1;
     ';': b2:=b2+1;
     ',': a2:=a2-1;
     '.': a2:=a2+1;
     'e': begin f:=1; i:=g; end;
    end;
   end;   
  until f<>0;
  setmode(7);
  writeln('Goooooooooooooooooooooooaaaaaaaaaal!!!');
  writeln;
  writeln('Left  player: ',left);
  writeln('Right player: ',right);
  repeat until keypressed;
  key:=inkey;
 end;
end.
