unit Hangy;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Menus, ExtCtrls, StdCtrls;

type
  THangMan = class (TObject)
  private
    OK, Nr, Laenge, Max: integer;
    Fertig, Mimik: boolean;
    RateText, Geraten: String;
    Liste: TStringList;
    procedure SetZero;
  public
    procedure ClearHangMan; virtual;
    procedure DrawHangMan; virtual;
    procedure ShowHangMan; virtual;
    procedure GetKey (Key: char); virtual;
    procedure GetList (Datei: String); virtual;
    constructor Create (Datei: String);
    destructor Free; virtual;
  end;

type
  TForm1 = class(TForm)
    ReadyButton: TButton;
    NewButton: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    Image1: TImage;
    Label1: TLabel;
    MainMenu1: TMainMenu;
    Datei1: TMenuItem;
    Oeffnen1: TMenuItem;
    Ende1: TMenuItem;
    OpenDialog1: TOpenDialog;
    procedure NewButtonClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
    procedure ReadyButtonClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Oeffnen1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Ende1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

const TPfad = '.\';          // 'c:\delphi\buch\';
const BPfad = '.\bilder\';   // 'c:\delphi\buch\bilder\';
var
  HangMan: THangMan;
  Form1: TForm1;

implementation

{$R *.DFM}

constructor THangMan.Create (Datei: String);
begin
  Liste := TStringList.Create;
  SetZero;
  Mimik := true;
  try
  // noch nicht ok!
    If FileExists(Datei) Then
    begin
      Liste.LoadFromFile (Datei);
      Max := Liste.Count;
    end;
  except
    on EFOpenError do
    begin
      Form1.Panel1.Caption := 'Ohne Worte!';
      Max := 0;
    end;
  end;
end;

destructor THangMan.Free;
begin
  Liste.Clear;
  Liste.Free;
end;

procedure THangMan.SetZero;
begin
  Geraten := '';
  Nr := 0;
  OK := 0;
  Fertig := false;
  Form1.Image1.Canvas.Brush.Color := clGray;
  Form1.Image1.Canvas.Rectangle(0,0,140,200);
end;

procedure THangMan.ClearHangMan;
var i: integer;
begin
  SetZero;
  if Max > 0 then RateText := UpperCase(Liste.Strings[random(Max)])
  else RateText := UpperCase('Delphi');
  Laenge := Length(RateText);
  for i:=0 to Laenge-1 do
  begin
    Geraten := Geraten + '?';
  end;
  Form1.Panel1.Caption := Geraten;
  Form1.Label1.Caption := 'Buchstaben eintippen!';
end;

procedure THangMan.ShowHangMan;
var Mix: integer;
begin
  Mix := 10;
  if Mimik then Mix := 11;
  Nr := 0;
  while Nr < Mix do begin inc(Nr); DrawHangMan; end;
  Form1.Panel1.Caption := RateText;
  if RateText = '' then
    Form1.Label1.Caption := 'Da ist kein Wort'
  else
    Form1.Label1.Caption := 'ist das Wort';
  Mimik := not(Mimik);
end;

procedure THangMan.DrawHangMan;
begin
  Form1.Image1.Canvas.Pen.Width := 3;
  Form1.Image1.Canvas.Brush.Color := clSilver;
  case Nr of
    1:
    begin
      Form1.Image1.Canvas.MoveTo (30,10);
      Form1.Image1.Canvas.LineTo (30,200);
    end;
    2:
    begin
      Form1.Image1.Canvas.MoveTo (30,10);
      Form1.Image1.Canvas.LineTo (95,10);
    end;
    3:
    begin
      Form1.Image1.Canvas.MoveTo (95,10);
      Form1.Image1.Canvas.LineTo (95,30);
    end;
    4:
    begin
      Form1.Image1.Canvas.Brush.Color := clRed;
      Form1.Image1.Canvas.Ellipse (80,30,110,65);
    end;
    5:
    begin
      Form1.Image1.Canvas.Ellipse (80,65,110,130);
    end;
    6:
    begin
      Form1.Image1.Canvas.MoveTo (90,130);
      Form1.Image1.Canvas.LineTo (90,170);
    end;
    7:
    begin
      Form1.Image1.Canvas.MoveTo (100,130);
      Form1.Image1.Canvas.LineTo (100,170);
    end;
    8:
    begin
      Form1.Image1.Canvas.Ellipse (75,165,95,175);
    end;
    9:
    begin
      Form1.Image1.Canvas.Ellipse (95,165,115,175);
    end;
    10:
    begin
      Form1.Image1.Canvas.Brush.Color := clAqua;
      Form1.Image1.Canvas.Ellipse (80,30,110,65);
      Form1.Image1.Canvas.Brush.Color := clYellow;
      Form1.Image1.Canvas.Ellipse (86,38,95,48);
      Form1.Image1.Canvas.Ellipse (95,38,104,48);
      Form1.Image1.Canvas.Arc (86,54,104,60,104,57,86,57);
    end;
    11:
    begin
      Form1.Image1.Canvas.Brush.Color := clFuchsia;
      Form1.Image1.Canvas.Ellipse (80,30,110,65);
      Form1.Image1.Canvas.Brush.Color := clLime;
      Form1.Image1.Canvas.Ellipse (86,38,95,48);
      Form1.Image1.Canvas.Ellipse (95,38,104,48);
      Form1.Image1.Canvas.Arc (86,45,104,60,86,53,104,53);
    end;
  end;
end;

procedure THangMan.GetKey (Key: char);
var Taste: String; Enthalten: boolean; i: integer;
begin
  Taste := UpperCase(Key);
  Enthalten := false;
  if not(Fertig) then
  begin
    {Jeden Buchstaben einzeln überprüfen}
    for i:=1 to Laenge do
    begin
      if (Taste = copy (RateText,i,1)) then
      begin
        if (Taste <> copy (Geraten,i,1)) then
        begin
          inc (OK);
          Geraten[i] := Key;
          Geraten := UpperCase(Geraten);
        end;
        Enthalten := true;
      end;
    end;
    {Wenn Key enthalten, anzeigen}
    if Enthalten then
      Form1.Panel1.Caption := Geraten
    {sonst Hangman weiterzeichnen}
    else
    begin
      inc (Nr);
      if (Nr < 11) then DrawHangMan;
    end;
    {Wenn alle Buchstaben geraten, OK}
    if (OK >= Laenge) and (Nr < 10) then
    begin
      if RateText = '' then
        Form1.Label1.Caption := 'Klick auf NEU!'
      else
        Form1.Label1.Caption := 'OK';
      SetZero;
      Fertig := true;
      Form1.Image1.Picture.LoadFromFile (BPfad+'HangMan.bmp');
    end
    {Wenn Hangman vollständig, TOT}
    else if (Nr >= 10) and (not Fertig) then
    begin
      Form1.Panel1.Caption := RateText;
      Form1.Label1.Caption := 'TOT';
    end;
  end;
end;

procedure THangMan.GetList (Datei: String);
begin
  SetZero;
  try
  begin
    Liste.LoadFromFile (Datei);
    Max := Liste.Count;
  end;
  except
    Form1.Panel1.Caption := 'Ohne Worte!';
    Max := 0;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  HangMan := THangMan.Create (TPfad+'HangMan.txt');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  {HangMan.Free;}
end;

procedure TForm1.NewButtonClick(Sender: TObject);
begin
  HangMan.ClearHangMan;
end;

procedure TForm1.ReadyButtonClick(Sender: TObject);
begin
  Form1.Image1.Canvas.Brush.Color := clGray;
  Form1.Image1.Canvas.Rectangle(0,0,140,200);
  HangMan.ShowHangMan;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
  HangMan.GetKey (Key);
end;

procedure TForm1.Oeffnen1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    HangMan.GetList (OpenDialog1.FileName);
end;

procedure TForm1.Ende1Click(Sender: TObject);
begin
  Close;
end;

end.
