program billard;
{Author: Stefan Ziegler, Wolfram Gerlach}
uses crt, graph, maus;

var i, gd, gm, xh, yh, lochzahl, einloch : integer;
    banden : byte;
    lochha : boolean;

procedure kugel(farbe, x, y : integer);
  begin
    setcolor(farbe);
    setfillstyle(1, farbe);
    pieslice(x, y, 0, 360, 4);
  END;

function kraft : byte;

  var krafts : string;
      kraftz, anz : integer;
      ch : char;
      xp, yp, xv, xv2 : word;
      tast : word;
      code : integer;

  begin
    {repeat
      anz:=0;
      krafts:='';
      setcolor(15);
      outtextxy(20, 425, 'Kraft: ');
      repeat
        ch:=readkey;
        if (ord(ch)<>13) then outtextxy(175+anz*18, 425, ch);
        if (ord(ch)<>13) then krafts:=krafts+ch;
        inc(anz);
      until (ord(ch)=13) or (ord(ch)=8);
      val(krafts, kraftz, code);
      setcolor(brown);
      for i:=0 to round(640/18) do outtextxy(15*i, 425, 'Û');
    until (code=0) and (kraftz<101);}
    delay(500);
    setcolor(15);
    outtextxy(20, 425, 'Kraft: 0 ');
    rectangle(150, 422, 350, 442);
    outtextxy(350, 425, '100 ');
    set_maus(150, 430);
    setcolor(15);
    setfillstyle(1, 15);
    kraftz:=0;
    tast:=0; xv2:=150;
    repeat
      xv:=xp;
      maus_status(xp, yp, tast);
      delay(10);
      setfillstyle(1, 6);
      if (xp>150) AND (xp<=350) then
        begin
          bar(150, 423, xv2, 441);
          setfillstyle(1, 15);
          bar(150, 423, xp, 441);
          if xv2<xp then kraftz:=kraftz+1 else dec(kraftz);
          xv2:=xp
        end;
    until tast>0;
    kraftz:=50;
    setfillstyle(1, 6);
    setcolor(6);
    bar(10, 422, 450, 442);
    kraft:=kraftz;
  end;

procedure winkelproz(kx, ky: integer; var winkel: real);

  var xp, yp : word;
      tast : word;
      winkrad : real;

  begin
    outtextxy(5, 425, 'Winkel mit Maus festlegen !');
    winkel:=0;
    set_maus(kx, ky);
    repeat
      maus_status(xp, yp, tast);
      delay(10);
    until tast>0;
    winkrad:=arctan((yp-ky)/(kx-xp));
    winkel:=winkrad;
    if kx>xp then winkel:=winkel+Pi;
    setcolor(6);
    for i:=0 to round(640/18) do outtextxy(15*i, 425, 'Û');
    setcolor(15);
  end;

procedure spiel(var xs, ys : integer; var loch : boolean);

  var ch : char;
      winkel : string;
      winkzah : real;
      code, anz, kraftz : integer;
      ri, riy : -1..1;

  begin
    loch:=false;
    kugel(15, xs, ys);
    settextstyle(0, 0, 2);
    setcolor(15);
    winkelproz(xh, yh, winkzah);
    kraftz:=kraft;
    anz:=0;
    hide_maus;
    repeat
      kugel(green, xs, ys);
      xs:=round(xs+cos(winkzah)*10);
      ys:=round(ys-sin(winkzah)*10);
      kugel(15, xs, ys);
      inc(anz);
      if (xs<22) and (ys<82) then loch:=true;
      if (xs>315) and (xs<325) and (ys<82) then loch:=true;
      if (xs>618) and (ys<82) then loch:=true;
      if (xs<22) and (ys>398) then loch:=true;
      if (xs>315) and (xs<325) and (ys>398) then loch:=true;
      if (xs>618) and (ys>398) then loch:=true;
      if ((xs>=620) or (xs<=20)) and (loch=false) then
        begin
          banden:=banden+1;
          winkzah:=Pi-winkzah;
        end;
      if ((ys<=80) or (ys>=400)) and (loch=false) then
        begin
          banden:=banden+1;
          winkzah:=2*Pi-winkzah;
        end;
      if loch=true then
        begin
          kugel(0, xs, ys);
          inc(banden, 2);
        end;
      delay(round(exp((1+(10/kraftz))*ln(anz))));
    until (anz=kraftz*2) or (loch=true);
    kugel(15, xs, ys);
    xh:=xs; yh:=ys;
    show_maus;
  end;

procedure mausstart;

  var ja : boolean;
      i  : 1..9;
      tasten : word;

  begin
    ja:=true;
    tasten:=3;
    init_maus(ja, tasten, ja);
    select_maus(1);
    show_maus;
    set_maus(320, 240);
  end;

begin
  gd:=9; gm:=2;
  randomize;
  clrscr;
  write('Anzahl der zu versenkenden Kugeln: '); readln(einloch);
  initgraph(gd, gm, 'd:\tp\bgi');
  setfillstyle(1, brown);
  setcolor(brown);
  bar(0, 0, 639, 479);
  setcolor(15);
  setfillstyle(1, green);
  bar(10, 70, 630, 410);
  setfillstyle(1, 15);
  setfillstyle(1, 0);
  setcolor(black);
  pieslice(10, 70, 0, 360, 9);
  pieslice(320, 70, 0, 360, 9);
  pieslice(630, 70, 0, 360, 9);
  pieslice(10, 410, 0, 360, 9);
  pieslice(320, 410, 0, 360, 9);
  pieslice(630, 410, 0, 360, 9);
  {pieslice(320, 240, 0, 360, 10);}
  settextstyle(2, 0, 3);
  lochha:=false;
  banden:=0;
  lochzahl:=0;
  mausstart;
  repeat
    setcolor(brown);
    outtextxy(7, 460, 'Û±°>¯ Kugel eingelocht!  2 Punkte ®<°±Û');
    setcolor(15);
    xh:=10+random(614);
    yh:=70+random(334);
    repeat
      spiel(xh, yh, lochha);
      setcolor(15);
      setfillstyle(1, green);
      bar(10, 70, 630, 410);
      setcolor(0); setfillstyle(1, 0);
      pieslice(10, 70, 0, 360, 9);
      pieslice(320, 70, 0, 360, 9);
      pieslice(630, 70, 0, 360, 9);
      pieslice(10, 410, 0, 360, 9);
      pieslice(320, 410, 0, 360, 9);
      pieslice(630, 410, 0, 360, 9);
    until lochha=true;
    outtextxy(7, 460, 'Û±°>¯ Kugel eingelocht!  2 Punkte ®<°±Û');
    readln;
    inc(lochzahl);
  until lochzahl=einloch;
  closegraph;
  clrscr;
  writeln('Punkte: ', banden);
  readln;
end.