program Hilbert;

USES GRAPH;

VAR
  GRDRIVER : INTEGER;
  GRMODE   : INTEGER;
  ERRCODE  : INTEGER;
  X,Y,H,I,N: INTEGER;

PROCEDURE GRAPHINIT;
BEGIN
  GRDRIVER := DETECT;
  INITGRAPH(GRDRIVER,GRMODE,'C:\TP\BGI');
  ERRCODE := GRAPHRESULT;
  if ERRCODE <> GROK THEN
    BEGIN
    WRITELN('Grafikus hiba:',
            GRAPHERRORMSG(ERRCODE));
    HALT(1);
    END;
END;

PROCEDURE PLOT;
BEGIN
    LINETO(X,Y);
END;

PROCEDURE B(I:INTEGER); FORWARD;
PROCEDURE C(I:INTEGER); FORWARD;
PROCEDURE D(I:INTEGER); FORWARD;

PROCEDURE A(I:INTEGER);
BEGIN
   IF I > 0 THEN
        BEGIN
        D(I-1); X := X - H; PLOT;
        A(I-1); Y := Y + H; PLOT;
        A(I-1); X := X + H; PLOT;
        B(I-1);
        END;
END;

PROCEDURE B(I:INTEGER);
BEGIN
   IF I > 0 THEN
        BEGIN
        C(I-1); Y := Y - H; PLOT;
        B(I-1); X := X + H; PLOT;
        B(I-1); Y := Y + H; PLOT;
        A(I-1);
        END;
END;

PROCEDURE C(I:INTEGER);
BEGIN
   IF I > 0 THEN
        BEGIN
        B(I-1); X := X + H; PLOT;
        C(I-1); Y := Y - H; PLOT;
        C(I-1); X := X - H; PLOT;
        D(I-1);
        END;
END;

PROCEDURE D(I:INTEGER);
BEGIN
   IF I > 0 THEN
        BEGIN
        A(I-1); Y := Y + H; PLOT;
        D(I-1); X := X - H; PLOT;
        D(I-1); Y := Y - H; PLOT;
        C(I-1);
        END;
END;

BEGIN
   REPEAT
      WRITE('H nyadrendû Hilber g”rb‚t szeretn‚l? ( 1-7 ):');
      READLN(N);
   UNTIL ( N=0 ) OR ( ( N >= 1 ) AND ( N <= 7 )) ;
   IF N=0 THEN HALT(1);
   GRAPHINIT;
   H := GETMAXY;
   X := GETMAXX DIV 2;
   Y := GETMAXY DIV 2;
   I := 0;
   WHILE ( I < N ) DO
      BEGIN
      H := H DIV 2;
      X := X + ( H DIV 2 ); Y := Y - ( H DIV 2 );
      I := I + 1;
      END;
   MOVETO(X,Y);
   A(N);
   READLN;
   CLOSEGRAPH;
END.