Autor Beitrag
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Sa 14.03.15 18:57 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.btnRechnenclick(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 not (feld[x,y]) then
                   if   (AnzahlLebenderNachbarn(x, y) = 3)
                   then (Shape.Brush.Color := clBlack)
                   else (Shape.Brush.Color := clWhite)
              else if  (AnzahlLebenderNachbarn(x, y) in [23])
                   then (Shape.Brush.Color := clblack)
                   else (Shape.Brush.Color := clwhite);
         end;

end;


ich hab das feldneu jetzt zu feld wieder geändert, aber was ich noch ändern muss um es im "Schmierzettel" zu merken, statt direkt zu malen, weiß ich nicht. bedeutet das, dass ich die true/false werte ändern muss?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 19:11 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ich hab das feldneu jetzt zu feld wieder geändert
:zustimm:

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
was ich noch ändern muss um es im "Schmierzettel" zu merken, statt direkt zu malen, weiß ich nicht. bedeutet das, dass ich die true/false werte ändern muss?
Ja, genau. ;) Ich wollte die Lösung nach dem EVA-Prinzip (Eingabe-Verarbeitung-Ausgabe) abwickeln. Also etwa so:

btnLesen anklicken (Werte aus der GUI in das interne Feld lesen)
btnRechnen anklicken (nächste Generation berechnen / unsichtbar!)
btnSchreiben anklicken (Werte aus dem internen Feld wieder in die GUI schreiben)

Dehalb arbeiten wir mit den Werten True / False in internen Arrays, statt direkt die Farben zuzuweisen (so trennt man die Logik von der Darstellung). Statt also Farbwerte zu setzen, weist du den bool´schen Wert ab das FeldNeu zu. :idea:

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 19:20 
also muss mein quelltext folgendermaßen aussehen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
  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 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;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 20:10 
Moin!

Jawoll, so hab ich mir das vorgestellt. :zustimm: Aber: wir brauchen in der Prozedur doch gar keine Shapes mehr, also kann der betreffende Code wieder raus. :idea:

Und zum Abschluss müssen wir noch den Schmierzettel wieder "abschreiben" (weil die btnSchreiben-Prozedur ja von da die Werte holt). Hast du eine Idee, wie man das machen könnte?

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 20:24 
ein. also ehrlich gesagt, fällt mir dazu gar nichts ein :(
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 20:26 
Moin!

Nun, Feld ist eine Variable und FeldNeu auch. Wie weist man denn einer Variablen den Wert einer anderen zu? 8)

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 20:31 
vlt mit feld := feldneu ?

Moderiert von user profile iconNarses: Beiträge zusammengefasst

vlt mit feld[x,y] := feldneu[x,y] ?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 20:39 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
vlt mit feld := feldneu ?
Jap! Genau so, ganz einfach. ;) (der Vollständigkeit halber: dein anderer Vorschlag ist auch nicht falsch, aber dann brauchst du auch noch die doppelten Schleifen drum rum, und das ist ja nicht nötig)

Wenn du diese letzte Zeile noch ergänzt hast, dann zeig doch bitte nochmal den Code.

Du kannst jetzt auch schon testen: ein paar Felder schwarz machen, btnLesen anklicken, btnRechnen anklicken, btnSchreiben anklicken und dann - sind alle Felder weiß! :shock: (was korrekt ist) Aber warum? :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: Sa 14.03.15 20:45 
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:
function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;
begin
  Result := 0// das ist natürlich Blödsinn
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;
    feld := feldneu
end;

procedure TForm1.btnRechnenclick(Sender: TObject);
 var x, y: Integer;

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;

end;
end.


es wird alles weiß, weil in der funktion result := 0 steht, also sind alle nachbarn "tot" und deshalb sterben die zellen
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 20:56 
Moin!

Wir waren doch bei der btnRechnen-Prozedur, warum schreibst du den Kopiercode in die btnLesen-Prozedur? :nixweiss: Und wo hast du FeldNeu deklariert? :lupe:

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
es wird alles weiß, weil in der funktion result := 0 steht, also sind alle nachbarn "tot" und deshalb sterben die zellen
Top! :zustimm:

Was passiert denn, wenn du statt 0 eine 3 zurück lieferst? (Nur mal so als test)

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 21:02 
feldneu hab ich wie feld unter public declariert

den kopiercode hab ich jetzt auch zur btnrechnen gewechselt

mit result := 3 werden alle felder schwarz
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 21:14 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
feldneu hab ich wie feld unter public declariert
Brauchen wir denn FeldNeu "überall" in der Formularklasse? :gruebel: Ich denke nicht. :nixweiss: Also kann FeldNeu doch ruhig zu den anderen lokalen Variablen in btnRechnen, oder? ;)

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
den kopiercode hab ich jetzt auch zur btnrechnen gewechselt
:zustimm:

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
mit result := 3 werden alle felder schwarz
Das war zu erwarten. Warum? :lupe:

Was passiert, wenn du 2 zurücklieferst? Und speziell: was passiert, wenn du als Ausgangsgeneration die 4 Felder in der Mitte schwarz machst und einen weißen Rand aussen drum rum lässt?

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 21:18 
ja, stimmt, ich hab das jetzt umgeschreiben, also nur lokal deklariert

weil ja bei drei lebenden zellen eine neue zelle geboren wirde/die zelle schwarz wird, werden alle schwarz

bei zwei bleibt alles wie es ist
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 21:22 
Moin!

Fein, dann haben wir die Regeln für das Game of Life schon fertig umgesetzt (ja, von "automatisch ablaufen" sind wir noch etwas entfernt, aber das kommt auch noch). :zustimm:

Jetzt zu dem Detail-Problem: wie kriegen wir die Anzahl der lebenden Nachbarn einer beliebigen Zelle raus (und damit den Code in die Funktion rein)? :gruebel: Mach doch mal einen Vorschlag, ruhig erstmal wieder in Umgangssprache oder, wenn du magst, auch gleich in Pseudo-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: Sa 14.03.15 21:29 
vielleicht geht ja sowas dass man von den den nachbarn x-1 bis x+1 und y-1 bis y+1 die wahrheitswerte prüft und dann zählt oder so
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 21:33 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
vielleicht geht ja sowas dass man von den den nachbarn x-1 bis x+1 und y-1 bis y+1 die wahrheitswerte prüft und dann zählt oder so
Top! :zustimm: Genau so hab ich das in meinem Test-Programm hierfür auch gemacht. ;)

Versuch doch mal diese Ablaufbeschreibung, die ja nun sehr kurz/grob ist, zu verfeinern, also z.B. in eine von mir aus umgangssprachlich formulierte Arbeitsanweisung zu übersetzen. Du kannst dich ja daran orientieren, wie wir über alle Felder mit den beiden Schleifen laufen, vielleicht kann man das ja so ähnlich machen? :idea:

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 21:39 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
 

begin
 result := 0;
 for x = 0 to N-1 do
             for y = 0 to N-1 do begin
                 if feld[x-1,y-1] = true then result:=result+1;
                 if feld[x-1,y] = true then result:=result+1;
                 if feld[x-1,y+1] = true then result:=result+1;
                 if feld[x,y-1] = true then result:=result+1;
                 if feld[x,y+1] = true then result:=result+1;
                 if feld[x+1,y-1] = true then result:=result+1;
                 if feld[x+1,y] = true then result:=result+1;
                 if feld[x+1,y+1] = true then result:=result+1;
            end;
end;


kann das so funktionieren?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.03.15 21:46 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
kann das so funktionieren?
Ist nicht komplett verkehrt, aber an unserem Ansatz vorbei. :nixweiss:

Nochmal zur Erinnerung:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;
begin
  Result := 0// das ist natürlich Blödsinn
end;

Wir haben diese Funktion deklariert. Die soll als Ergebnis (was der Wert von Result ist) die Anzahl der lebenden Zellen um die Zelle[x,y] liefern (das ist der Sinn der beiden Parameter dieser Funktion: um welche Zelle geht´s denn). :idea:

Wenn du allerdings 8 einzelne Abfragen verwenden möchtest (was nicht falsch ist, nur umständlich), dann kannst du die Schleifen weglassen. :nixweiss:

Dann versuch nochmal die Funktion zu vervollständigen und zeig deinen Code (nur die Funktion, wie oben, nur mit deinem "Inhalt").

cu
Narses

PS: Ich bin jetzt gleich bis ca. 22 Uhr (oder etwas später) offline.

PPS: Wie war das noch mit "= True"? :zwinker:

_________________
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 21:54 
meine neue funktion:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;

begin
 result := 0;

 if (feld[x-1,y-1]) then result:=result+1;
 if (feld[x-1,y]) then result:=result+1;
 if (feld[x-1,y+1]) then result:=result+1;
 if (feld[x,y-1]) then result:=result+1;
 if (feld[x,y+1]) then result:=result+1;
 if (feld[x+1,y-1]) then result:=result+1;
 if (feld[x+1,y]) then result:=result+1;
 if (feld[x+1,y+1]) then result:=result+1;


end;


aber so ganz stimmt die noch nucht :(
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Sa 14.03.15 22:34 
Das sieht doch schon ganz gut aus. Ich will mich hier jetzt nicht allzusehr einmischen, und Narses wird das dir dann schon erläutern, aber du kannst ja jetzt schonmal drüber nachdenken, in welchen Fällen (also welche Werte für x/y) die Funktion nicht funktionieren kann und wie man das abfangen könnte (Stichwort Modulo, sagt dir das etwas aus der Mathematik?).

Für diesen Beitrag haben gedankt: Narses