Autor Beitrag
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 00:48 
Moin!

Aha :gruebel: zeig mal den ganzen Code... :les: Irgendwo nochwas geändert? :lupe:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 00:53 
also bewusst habe ich nichts verändert :/
ausblenden volle Höhe Delphi-Quelltext
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:
105:
106:
107:
108:
  public
    Feld: TFeld; // das ist der interne Speicher für die Zellen (=Shapes in der GUI)
    function LebtNachbar(x, y: Integer): Boolean;
    function AnzahlLebenderNachbarn(const x, y: Integer): Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  ShowMessageFmt('x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
  Result := Feld[x, y];
end;

function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;
  var
    dx, dy: Integer;
begin
  Result := 0;
  for dy := -1 to 1 do
    for dx := -1 to 1 do
      if not ((x=0and (y=0)) then
      if LebtNachbar(x +dx, y +dy) then
        Inc(Result); // das ist das Gleiche wie "Result := Result +1", nur kürzer ;)
end;

procedure TForm1.btnTestClick(Sender: TObject);
begin
  AnzahlLebenderNachbarn(00); // Test-Aufruf nur für linke obere Ecke (Zelle[0,0])
end;

procedure TForm1.btnSchreibenClick(Sender: TObject);
var  x, y, snr: Integer;
    Shape: TShape;
begin
   for y := 0 to N-1 do
       for x := 0 to N-1 do begin
           snr := x +y *N +1// Nr. des Shapes berechnen
           Shape := TShape(FindComponent('Shape' +IntToStr(snr))); // Komponente suchen
           if (feld[x,y]) then
              shape.brush.color := clblack
           else
             shape.brush.color := clwhite;
       end;

end;

procedure TForm1.EndeClick(Sender: TObject);
begin
  close;
end;


  procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  var
    ThisShape: TShape;
begin
  ThisShape := (Sender as TShape);
  if (ThisShape.Brush.Color = clWhite) then
    ThisShape.Brush.Color := clBlack
  else
    ThisShape.Brush.Color := clWhite;
end;




procedure TForm1.btnLesenClick(Sender: TObject);
var
    x, y, snr: Integer;
    Shape: TShape;
begin
    for y := 0 to N-1 do // von unten nach oben
        for x := 0 to N-1 do begin // von rechts nach links
            snr := x +y *N +1// Nr. des Shapes berechnen
            Shape := TShape(FindComponent('Shape' +IntToStr(snr))); // Komponente suchen
            if (Shape.Brush.Color = clBlack) then
               Feld[x, y] := True
            else
               Feld[x, y] := False; // Wert ermitteln
        end;

end;

procedure TForm1.btnRechnenclick(Sender: TObject);
 var x, y: Integer;
     Feldneu : TFeld;
begin
     for y := 0 to N-1 do
         for x := 0 to N-1 do begin
              if not (feld[x,y]) then
                   if   (AnzahlLebenderNachbarn(x, y) = 3)
                   then feldneu[x,y] := true
                   else feldneu[x,y] := false
              else if  (AnzahlLebenderNachbarn(x, y) in [23])
                  then feldneu[x,y] := true
                  else feldneu[x,y] := false;
         end;
     feld := feldneu
end;
end.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 00:56 
Moin!

Hm, sieht eigentlich gut aus. Dann kommentier mal diese Zeile testweise aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  ShowMessageFmt('x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
  //Result := Feld[x, y];  <-- testweise deaktivieren
end;

Mein Delphi hat da keine Probleme mit (was genau, sehen wir dann gleich schon), vielleicht hat Lazarus da aber schon eins... :? :nixweiss:

Geht´s dann wieder (kommen MessageBoxen)?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 01:00 
nein, die messageboxen bleiben verschwunden :/
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 01:01 
Moin!

OK, dann Projekt abspeichern, Lazarus beenden, wieder öffnen und das Projekt laden. Geht´s jetzt?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 01:04 
das bringt auch nichts :/ das geht nur wenn ich die eine zeile auskommentier:
if not ((x=0) and (y=0)) then
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 01:07 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
das bringt auch nichts :/ das geht nur wenn ich die eine zeile auskommentier:
Stimmt. :autsch: Da ist nämlich noch ein Fehler drin:
ausblenden Delphi-Quelltext
1:
if not ((dx = 0and (dy = 0)) then					

Mach dir nix draus, ich hab´s auch die ganze Zeit übersehen... :lol:

Jetzt sollte das aber wieder laufen. ;)

cu
Narses

PS: Da hast du´s sogar selbst geschrieben: ;)
user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
weil nicht ausgeschlossen ist, dass dx und dy gleichzeitig 0 sein können

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 01:11 
ok jetzt kommt:
-1.-1
0,-1
1,-1
-1,0
1,0
-1,1
0,1
1,1
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 01:12 
Moin!

Jap, das passt wieder. ;) Also weiter mit dem eigentlichen Problem: was davon ist ... problematisch? :lupe:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 01:13 
die -1, die müsste ja eigentlich der höchste wert auf der anderen seite vom feld sein
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 01:15 
Moin!

Richtig, also wenn die Werte < 0 sind, dann müssen wir... ja, was dann. Vorschläge? ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 01:16 
vielleicht stattdessen N oder N-1 nehmen?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 01:18 
Moin!

Gegenangebot: N addieren? ;)

Verstanden? -> Codevorschlag zeigen.
Nicht verstanden? -> Wo hapert´s?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 10:12 
vielleicht einfach mit zwei weiteren if schleifen if y+dy=-1 then y+dy:=y+dy+N
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 12:33 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
vielleicht einfach mit zwei weiteren if schleifen
Zunächst mal zur Begrifflichkeit:

if: Fallunterscheidung (wenn-dann) :idea:
forwhilerepeat: Schleifen (wiederhole irgendwas) :think:

Bitte nicht "if-schleifen" in Gegenwart von Informatikern sagen, die kriegen dann so ein schmerzhaftes Ziehen in der linken Gehirnhälfte und anschließend Ausschlag am ganzen Körper... :lol:

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
if y+dy=-1 then y+dy:=y+dy+N
Hm, damit kann ich jetzt wenig anfangen... :gruebel:

Vielleicht zeigst du erstmal die Stelle im Code, an der wir ansetzen müssen, um das Problem zu lösen. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 12:52 
ohh, ok, werde ich mir merken ;)

ich würde das vielleicht so machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;
  var
    dx, dy: Integer;
begin
  Result := 0;
  for dy := -1 to 1 do
    for dx := -1 to 1 do
     if not ((dx=0and (dy=0)) then begin
        if (dx=-1then  begin
          dx:=dx+N;
           if (dy=-1then  begin
           dy:=dy+N;
         if LebtNachbar(x +dx, y +dy) then
        Inc(Result); // das ist das Gleiche wie "Result := Result +1", nur kürzer ;)
     end;
           end;
        end;
        end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 13:19 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ohh, ok, werde ich mir merken ;)
:zustimm:

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ich würde das vielleicht so machen:
Hm, zum einen macht das den Code recht unübersichtlich. Also alleine unter diesem Gesichtspunkte finde ich das keine gute Stelle. :nixweiss: Mit den beiden Schleifen (dx und dy) gehen wir alle Nachbar durch. Wenn du nun da eingreifst, hätte das ja zur Folge, dass nicht alle oder andere Nachbarn berücksichtigt würden. :gruebel: Also auch vom logischen Gesichtspunkt vielleicht keine glückliche Stelle. :? (zur Klarstellung: grundsätzlich geht das auch hier, empfehle ich dir aber nicht)

Wo könnte man es denn evtl. besser unterbringen? :lupe:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 13:28 
mir fällt leider keine andere stelle ein :(
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 13:29 
Moin!

Wir haben doch noch eine weitere Funktion eingeführt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  Result := Feld[x, y];
end;

Was meinst du, nur für diese eine Zeile? :zwinker:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 13:48 
stimmt das dann so?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  if x = -1 then x := x+N;
  if y = -1 then y := y+N;
  ShowMessageFmt('x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
  Result := Feld[x, y];
end;