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: Sa 14.03.15 23:18 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
meine neue funktion:
Das ist doch schon ganz OK. ;) Allerdings ziemlich umständlich. :? Möchtest du das ändern? Also weniger umständlich machen? :nixweiss: Dann zeige ich dir noch einen kleinen Trick... 8)

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
aber so ganz stimmt die noch nucht :(
Warum, gibt´s Fehlermeldungen? :lupe:

Boldar hat schon drauf hingewiesen (:beer:), da ist noch ein generelles, logisches Problem drin. Bist du schon drauf gekommen?

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: Sa 14.03.15 23:23 
also nicht funktionieren kann es meiner meinung nach nicht für die randfelder

und mod sind die Restklassen bei der division mit einer bestimmten zahl
aber was genau falsch ist, weiß ich nicht :/

ja, einfacher wäre es mir schon lieber :P

nein, fehlermeldungen treten nicht auf, aber die felder die es anmalt sind noch ziemlich wahllos irgendwie ._.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 23:31 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ja, einfacher wäre es mir schon lieber :P
OK, dann machen wir das. :zustimm: Man kann die umgebenden Felder auch mit zwei Schleifen "ablaufen", und zwar so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
for dx := -1 to 1 do
  for dy := -1 to 1 do
    if (Feld[x +dx, y +dy]) then
      Result := Result +1;

Das hat nur einen winzig kleinen "Haken", erkennst du ihn? :lupe:


user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
also nicht funktionieren kann es meiner meinung nach nicht für die randfelder
Jap, genau da ist das Problem. Da gehen wir gleich noch genauer drauf ein. :zustimm:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.

Für diesen Beitrag haben gedankt: Nini
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Sa 14.03.15 23:34 
nein, also so direkt fällt mir kein haken auf
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 23:45 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
nein, also so direkt fällt mir kein haken auf
OK, dann müssen wir einen kleinen Exkurs einlegen, kein Problem. :)

Zunächst mal führen wir noch eine Funktion ein, die müssen wir wieder oben in der Klasse "anmelden":
ausblenden Delphi-Quelltext
1:
2:
3:
4:
  public
    Feld: TFeld; // das ist der interne Speicher für die Zellen (=Shapes in der GUI)
    function AnzahlLebenderNachbarn(const x, y: Integer): Integer;
    function LebtNachbar(x, y: Integer): Boolean;

Der Sinn soll also sein, die Prüfung, ob eine bestimmte Zelle "lebendig" ist, auszulagern. Sonst wird der Code schnell unübersichtlich. :idea: Aber, wir machen da noch nicht den endgültigen Code rein, sondern erstmal was zum testen:
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];
end;

Wenn du das übernommen hast, dann müssen wir noch die Zähler-Funktion anpassen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
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 LebtNachbar(x +dx, y +dy) then
        Inc(Result); // das ist das Gleiche wie "Result := Result +1", nur kürzer ;)
end;

ACHTUNG: Mit diesem Code nicht auf den btnRechnen klicken, sonst musst du zig-tausend Messageboxen wegklicken! :shock:

Statt dessen machst du noch einen neuen Button auf das Formular: btnTest, und da schreibst du rein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.btnTestClick(Sender: TObject);
begin
  AnzahlLebenderNachbarn(11); // Test-Aufruf nur für die Zelle[1,1]
end;

Jetzt das Projekt starten und einmal auf den neuen Test-Button klicken: Wieviele Messageboxen kommen und was steht drin?

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:02 
es kommt eine message boxx und darin steht "x=0,y=0"
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:03 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
es kommt eine message boxx und darin steht "x=0,y=0"
Es wird nur genau eine MessageBox angezeigt?! :lupe: Da sollten ein paar mehr kommen... :gruebel: Zeig mal bitte deinen gesamten Code... :les:

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:04 
user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
es kommt eine message boxx und darin steht "x=0,y=0"

oh, danack kommen ja noch welche :P
und danach x=1, y=0
x=2, y=0
x=0, y=1
x=1, y=1
x=2 y=1
x=0 y=2
x=1 y=2
x=2 y=2

und dann schließen sich die boxen
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:06 
Moin!

Ah, schon besser. :P Und, wieviele sind es denn nun gewesen? :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:11 
es waren 9
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:12 
Moin!

OK, kannst du dann bitte mal die Nachbarn der Zelle[1,1] aufzählen? (oder als Abkürzung sagen, wieviele Nachbarn diese Zelle hat?)

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:15 
0,0
0,1
0,2
1,0
1,2
2,0
2,1
2,2
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:16 
Moin!

Was fällt dir im Gegensatz zu den MessageBoxen auf? ;)

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:20 
eeehhhm, keine ahnung, vlt, dass ich die andersrum sotiert habe? und das halt die 1,1 nicht dazu gehört, weil die die zelle selbst ist
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:21 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
und das halt die 1,1 nicht dazu gehört, weil die die zelle selbst ist
Hast ja doch Ahnung, genau das ist es! :zustimm: Und jetzt die Preisfrage: warum taucht denn bei der Lösung mit den beiden Schleifen die Zelle selbst auf (die ist ja kein Nachbar)?! :gruebel:

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:31 
weil nicht ausgeschlossen ist, dass dx und dy gleichzeitig 0 sein können
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:32 
Moin!

Perfekt! :zustimm: Dann ändere das doch mal eben (und zeig dann den Code der Funktion bitte)! ;)

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:40 
so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
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;
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:43 
Moin!

Grandios! :zustimm: Genau so.

Dann ändere doch mal bitte den Testaufruf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.btnTestClick(Sender: TObject);
begin
  AnzahlLebenderNachbarn(00); // Test-Aufruf nur für linke obere Ecke (Zelle[0,0])
end;

Welche Werte werden dir nun nach einem Klick auf btnTest in den MessageBoxen angezeigt? Und gibt es da vielleicht schon ... Auffälligkeiten? :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:47 
bei mir passiert gar nichts mehr, wenn ich auf btntest gehe