1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
| unit TürmevonHanoi;
interface
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Label1: TLabel; ListBox1: TListBox; ListBox2: TListBox; ListBox3: TListBox; ListBox4: TListBox; edit1: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin Listbox1.Clear; Listbox2.Clear; Listbox3.Clear; Listbox4.Clear; for i := 1 to strtoint(edit1.text) do begin Listbox1.Items.Add(inttostr(i)); end; end;
procedure TForm1.Button2Click(Sender: TObject); procedure verschieben(a,b:Integer); var temp :String; begin case a of 1: begin temp := Listbox1.Items[0]; Listbox1.Items.Delete(0); end; 2: begin temp := Listbox2.Items[0]; Listbox2.Items.Delete(0); end; 3: begin temp := Listbox3.Items[0]; Listbox3.Items.Delete(0); end; end; case b of 1: begin Listbox1.Items.Insert(0,temp); end; 2: begin Listbox2.Items.Insert(0,temp); end; 3: begin Listbox3.Items.Insert(0,temp); end; end; ListBox4.Items.Add('Scheibe ' + temp + ' von ' + inttostr(a) + ' --> ' + inttostr(b)); end;
function laenge(a:Integer):Integer; begin case a of 1: Result := Listbox1.Items.Count; 2: Result := Listbox2.Items.Count; 3: Result := Listbox3.Items.Count; end; end;
procedure hanoi(anzahl, a, b, c:Integer); begin if(anzahl = 1) then begin verschieben(a, b); end else begin hanoi(anzahl - 1, a, c, b); verschieben(a, b); hanoi(anzahl - 1, c, b, a); end; end; begin if(laenge(1) > 0) then hanoi(laenge(1), 1, 3, 2) else ShowMessage('Fehler: Auf dem linken Stab liegen keine Scheiben!!'); Label1.Caption := inttostr(Listbox4.Items.Count) + ' Züge'; end; end. |