Einzelnen Beitrag anzeigen

xDIMAx

Registriert seit: 4. Feb 2012
5 Beiträge
 
#1

Magisches Quadrat - Delphi/Pascal

  Alt 1. Mai 2012, 20:47
Delphi-Version: 7
Hallo, ich soll ein "Magisches Quadrat" in Delphi 7 programmieren. Zum Großteil hab ich es schon fertig nur hab ich das Problem, dass ich es in 3x3, 5x5 und 7x7 programmieren soll, es aber nur in 3x3 funktioniert. Und das auch nur, wenn ich zuerst einmal 5x5 oder 7x7 generiert habe. Hoffe mir kann jemand helfen.

Aufgabenstellung:
http://s.gullipics.com/image/z/x/l/h...-rxkj/img.jpeg

Programm:
https://docs.google.com/open?id=0B_X...FhzY0ExTmVsd2M
Einfach STRG+S drücken.


Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Label1: TLabel;
    RadioGroup1: TRadioGroup;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  Gr: integer; //Größe des Quadrats
  Quadrat: array of array of integer;
  X, Y, X1, Y1, I: Integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  case RadioGroup1.ItemIndex of
    0: Gr:=2;
    1: Gr:=4;
    2: Gr:=6;
  else exit;
  end;
  for I:=0 to Gr do
    begin
      StringGrid1.Rows[I].Clear;
      StringGrid1.Cols[I].Clear;
    end;
  Memo1.Clear;
  StringGrid1.ColCount:=Gr+1;
  StringGrid1.RowCount:=Gr+1;
  SetLength(Quadrat, Gr*Gr, Gr*Gr);
  X:=Gr-trunc(Gr/2);
  Y:=Gr-trunc(Gr/2)+1;
  Quadrat[X, Y]:=1;
  StringGrid1.Cells[X, Y]:=IntToStr(Quadrat[X, Y]);
  Memo1.Lines.Add('Quadrat['+IntToStr(X)+','+IntToStr(Y)+'] '
                            +'= '+IntToStr(Quadrat[X, Y]));
  for I:=2 to Gr*Gr+2*Gr+1 do
    begin
      if StringGrid1.Cells[X+1, Y+1]='then
        begin
          X:=X+1;
          Y:=Y+1;
        end
      else
        begin
          X:=X+3;
          Y:=Y+2;
        end;
      if X>Gr then
        begin
          X:=X-Gr-1;
        end;
      if Y>Gr then
        begin
          Y:=Y-Gr-1;
        end;
      Quadrat[X, Y]:=I;
      StringGrid1.Cells[X, Y]:=IntToStr(Quadrat[X, Y]);
      Memo1.Lines.Add('Quadrat['+IntToStr(X)+','+IntToStr(Y)+'] '
                                +'= '+IntToStr(Quadrat[X, Y]));
    end;
end;

end.
  Mit Zitat antworten Zitat