program points;

uses crt,graph,drivers;

var
  xCor, yCor           : integer;
  xMax, xMaxHalf, yMax : integer;
  Gm,Gd                : integer;
  pixel                : word;
  seed                 : word;
  width                : word;
  Zero,One,Two         : Word;
  Iterations           : LongInt;
  ch                   : char;

begin
  Gd := Detect;
  InitGraph(Gd,Gm,'');
  if graphresult <> grOK then Halt(1);
  SetTextStyle(DefaultFont,HorizDir,1);
  xMax     := GetMaxX;
  xMaxHalf := GetMaxX div 2;
  yMax     := GetMaxY;
  pixel    := GetMaxColor;
  Randomize;
  yCor := Random(yMax);
  Width := trunc(xMax * (yCor / yMax));
  xCor := Random(Width) + (xMax - Width) div 2 ;
  Iterations := 0; Zero := 0; One := 0; Two := 0;

  Repeat
    PutPixel(xCor,yCor,Pixel);
    Inc(Iterations);
    Case Random(3) of
      0 : Begin
            xCor := xCor div 2;
            yCor := yCor div 2;
            Inc(Zero);
          End;
      1 : Begin
            xCor := xCor + ((xMax - xCor) div 2);
            yCor := yCor div 2;
            Inc(One);
          End;
      2 : Begin
            If xCor > xMaxHalf then
              xCor := xCor - ((xCor - xMaxHalf) div 2)
            Else
              xCor := xCor + ((xMaxHalf - xCor) div 2);
            yCor := yCor + ((yMax - yCor) div 2);
            Inc(Two);
          End;
    End;
  Until KeyPressed;
  ch := readkey;

  CloseGraph;
  Writeln('Iterations=',Iterations,' Zero=',Zero,' One=',One,' Two=',Two);
End.
