Einzelnen Beitrag anzeigen

biby90
(Gast)

n/a Beiträge
 
#106

AW: tic tac toe.... mal wieder:) versteh aufgabestellung nicht ganz

  Alt 25. Aug 2011, 10:28
jop hab ich ja alles gemacht.... ich geb dir mal den quelltext.... also den kopletten^^ vielleicht findest du ja auch den Fehler weshalb die funktion nur die Zeilen von oben nach unten vergleicht^^
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    CloseButton: TButton;
    NewGameButton: TButton;
    Label10: TLabel;
    Label7: TLabel;
    procedure MakeArray;
    procedure ChangePlayer;
    function IsFieldEmpty(AIndex:string): Boolean;
    function CheckForWin(AIndex:array of string): Boolean;
    function WriteToField(GPlayer:Integer):String;
    procedure Label1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure Label9Click(Sender: TObject);
    procedure Label8Click(Sender: TObject);
    procedure Label3Click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure Label6Click(Sender: TObject);
    procedure NewGameButtonClick(Sender: TObject);
    procedure CloseButtonClick(Sender: TObject);
    procedure Label7Click(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;


var
  Form1: TForm1;
  GPlayer: Integer;
  GBuffer: array[1..9] of string;

implementation

{$R *.dfm}

procedure TForm1.ChangePlayer;
begin
  if GPlayer = 1 then
  begin
    GPlayer := 2;
  end else
    GPlayer := 1;
end;

function TForm1.CheckForWin(AIndex: array of string): Boolean;
begin
  result := false;

  begin
    if (AIndex[1] = AIndex[2]) and (AIndex[2] = AIndex[3]) and
      (IsFieldEmpty(AIndex[1]) = false) then
    begin
      result := true;
    end;
    if (AIndex[4] = AIndex[5]) and (AIndex[5] = AIndex[6]) and
      (IsFieldEmpty(AIndex[4]) = false) then
    begin
      result := true;
    end;
    if (AIndex[7] = AIndex[8]) and (AIndex[8] = AIndex[9]) and
      (IsFieldEmpty(AIndex[7]) = false) then
    begin
      result := true;
    end;
    if (AIndex[1] = AIndex[4]) and (AIndex[4] = AIndex[7]) and
      (IsFieldEmpty(AIndex[1]) = false) then
    begin
      result := true;
    end;
    if (AIndex[2] = AIndex[5]) and (AIndex[5] = AIndex[8]) and
      (IsFieldEmpty(AIndex[2]) = false) then
    begin
      result := true;
    end;
    if (AIndex[3] = AIndex[6]) and (AIndex[6] = AIndex[9]) and
      (IsFieldEmpty(AIndex[3]) = false) then
    begin
      result := true;
    end;
    if (AIndex[1] = AIndex[5]) and (AIndex[5] = AIndex[9]) and
      (IsFieldEmpty(AIndex[1]) = false) then
    begin
      result := true;
    end;
    if (AIndex[3] = AIndex[5]) and (AIndex[5] = AIndex[7]) and
      (IsFieldEmpty(AIndex[3]) = false) then
    begin
      result := true;
    end;
  end;
end;

procedure TForm1.CloseButtonClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  GPlayer:=1;
end;

function TForm1.IsFieldEmpty(AIndex:string): Boolean;
begin
  result := false;

  if AIndex = 'then
  begin
    result := true;
  end;
end;


procedure TForm1.Label1Click(Sender: TObject);
begin
 if IsFieldEmpty(Label1.Caption) then
  begin
    Label1.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label1.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label2Click(Sender: TObject);
begin
 if IsFieldEmpty(Label2.Caption) then
  begin
    Label2.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label2.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label3Click(Sender: TObject);
begin
  if IsFieldEmpty(Label3.Caption) then
  begin
    Label3.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label3.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label4Click(Sender: TObject);
begin
 if IsFieldEmpty(Label4.Caption) then
  begin
    Label4.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label4.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label5Click(Sender: TObject);
begin
  if IsFieldEmpty(Label5.Caption) then
  begin
    Label5.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label5.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label6Click(Sender: TObject);
begin
  if IsFieldEmpty(Label6.Caption) then
  begin
    Label6.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label6.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label7Click(Sender: TObject);
begin
  if IsFieldEmpty(Label7.Caption) then
  begin
    Label7.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label7.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label8Click(Sender: TObject);
begin
  if IsFieldEmpty(Label8.Caption) then
  begin
    Label8.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label8.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.Label9Click(Sender: TObject);
begin
  if IsFieldEmpty(Label9.Caption) then
  begin
    Label9.Caption := WriteToField(GPlayer);
    MakeArray;
    if CheckForWin(GBuffer) then
    begin
      Label9.Caption := '';
      ShowMessage('Spieler ' + IntToStr(GPlayer)+' hat gewonnen!');
      GPlayer := 1;
    end else
    begin
      ChangePlayer;
    end;
  end;
end;

procedure TForm1.MakeArray;
begin
 GBuffer[1] := Label1.Caption;
 GBuffer[2] := Label2.Caption;
 GBuffer[3] := Label3.Caption;
 GBuffer[4] := Label4.Caption;
 GBuffer[5] := Label5.Caption;
 GBuffer[6] := Label6.Caption;
 GBuffer[7] := Label7.Caption;
 GBuffer[8] := Label8.Caption;
 GBuffer[9] := Label9.Caption;
end;

procedure TForm1.NewGameButtonClick(Sender: TObject);
begin
  Label1.Caption := '';
  Label2.Caption := '';
  Label3.Caption := '';
  Label3.Caption := '';
  Label4.Caption := '';
  Label5.Caption := '';
  Label6.Caption := '';
  Label7.Caption := '';
  Label8.Caption := '';
  Label9.Caption := '';
  GPlayer := 1;
end;

function TForm1.WriteToField(GPlayer: Integer): String;
begin
  if GPlayer = 1 then
  begin
    result := 'X';
  end else
  begin
    result := 'O';
  end;
end;

end.
Ich hoffe dass Du da dzrchsteigst^^