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 15:46 
Moin!

Und, klappt´s jetzt (ich sag mal: nein :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 15:55 
hast recht, es geht nicht

es verändert sich eine generation lang und dann bleibt's stehn
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 15:56 
Moin!

Jo, so ist es. :nixweiss: Also: kannst den Button-Aufruf da wieder raus nehmen.

Wie funktioniert denn ein Timer? Haben wir irgendwo gesagt, was der machen soll? :lupe: 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: So 15.03.15 15:59 
nein, also so wirklich hab ich keine ahnung wie timer funktionieren :/
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 16:00 
Moin!

Dann mach doch mal einen Doppelklick auf der Timer-Komponenten im Formular... :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 16:01 
damit hab ich eine weitere prozedur geöffnet.
muss ich da jetzt reinschreiben, dass er den rechnenbutton ausführen soll?
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 16:07 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
muss ich da jetzt reinschreiben, dass er den rechnenbutton ausführen soll?
Jup. ;)

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: So 15.03.15 16:09 
es funktioniert *_*
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 16:35 
Moin!

:party: :dance2: :beer: :dance:


Wie sieht´s aus, rein von der Aufgabe her bist du fertig: Game of Life läuft. :zustimm:

Willst du noch ein bisschen optimieren? ;) Evtl. könnte man ja die Feldgröße unabhängig von manuell angelegten Shapes machen? (man kann die Shapes auch per Programmcode anlegen und muss dann nur noch N ändern, wenn man das Feld größer machen möchte :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: So 15.03.15 16:47 
joar, das hört sich gut an :)

kann man eigentlich das programm auch so ändern, dass der nutzer die regeln für geburt/tod ändern 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 17:06 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
joar, das hört sich gut an :)
Gut, dann schauen wir mal, was noch geht. ;)

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
kann man eigentlich das programm auch so ändern, dass der nutzer die regeln für geburt/tod ändern können?
Mann kann schon, die Frage ist eher: kann Frau auch? :zwinker: (soll heißen: ja, klar kann man, aber wenn du keine Idee hast wie, könnte es noch etwas schwer sein für dich, oder?)

Dann zeig doch bitte erstmal deinen kompletten Code, damit ich den aktuellen Status kenne. :zustimm:

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 17:21 
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:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls;

const
  N = 5;
type
  TFeld = array[0..N-10..N-1of Boolean;

  { TForm1 }

  TForm1 = class(TForm)
    ckAutomatik: TCheckBox;
    Ende: TButton;
    Shape1: TShape;
    Shape10: TShape;
    Shape11: TShape;
    Shape12: TShape;
    Shape13: TShape;
    Shape14: TShape;
    Shape15: TShape;
    Shape16: TShape;
    Shape17: TShape;
    Shape18: TShape;
    Shape19: TShape;
    Shape2: TShape;
    Shape20: TShape;
    Shape21: TShape;
    Shape22: TShape;
    Shape23: TShape;
    Shape24: TShape;
    Shape25: TShape;

    Timer: TTimer;
    procedure btnLesenClick(Sender: TObject);
    procedure btnRechnenClick(Sender: TObject);
    procedure btnSchreibenClick(Sender: TObject);
    procedure ckAutomatikChange(Sender: TObject);
    procedure EndeClick(Sender: TObject);
    procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TimerTimer(Sender: TObject);

  private

  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
  if (x < 0then
    x := x +N;
  if (y < 0then
    y := y +N;
   if (x > (N-1)) then
    x := x -N;
  if (y > (N-1)) then
    y := y -N;
  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 begin
     if not ((dx=0and (dy=0)) then
         if LebtNachbar(x +dx, y +dy) then
        Inc(Result);
    end;
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;
           Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
           if (feld[x,y]) then
              shape.brush.color := clblack
           else
             shape.brush.color := clwhite;
       end;

end;

procedure TForm1.ckAutomatikChange(Sender: TObject);
begin
    if ckAutomatik.Checked then begin // Haken reingeklickt
    btnLesenClick(Self); // Feld einlesen
    Timer.Enabled := True; // Timer einschalten
  end
  else // Haken rausgeklickt
    Timer.Enabled := False; // Timer ausschalten
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.TimerTimer(Sender: TObject);
begin
  btnrechnenClick(Self);
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;
            Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
            if (Shape.Brush.Color = clBlack) then
               Feld[x, y] := True
            else
               Feld[x, y] := False;
        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
      if Feld[x, y] then // lebende Zelle
        FeldNeu[x, y] := (AnzahlLebenderNachbarn(x, y) in [2..3]) // Überleben
      else // tote Zelle
        FeldNeu[x, y] := (AnzahlLebenderNachbarn(x, y) = 3); // Geburt
  Feld := FeldNeu;
  btnSchreibenClick(Self); // virtuell auf den Button klicken

end;
end.


das könnte gut sein :P
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 17:32 
Moin!

Soo, dann gehen wir erstmal hier noch dran:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  if (x < 0then
    x := x +N;
  if (y < 0then
    y := y +N;
   if (x > (N-1)) then
    x := x -N;
  if (y > (N-1)) then
    y := y -N;
  Result := Feld[x, y];
end;

Der markierte Teil "gefällt" mir noch nicht. :? (ist nicht falsch, nur ... unschön)

user profile iconBoldar hat schonmal drauf hingewiesen, es gibt die Modulo-Rechnung. Was war das nochmal? :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 18:35 
bei x mod y wird nur der rest bei der division von x durch y angegeben

irgendwie hab ich das gefühl, dass das programm bei 5 feldern macht was es will, eigentlich muss ja so ein balken aus drei feldern sich immer drehn aber der verschwindet einfach -.-
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 18:50 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
bei x mod y wird nur der rest bei der division von x durch y angegeben
Richtig. Wir haben da also ein x, und das soll nur Werte zwischen 0..(N-1) annehmen können. Wie müsste man das dann mit mod schreiben?

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
irgendwie hab ich das gefühl, dass das programm bei 5 feldern macht was es will, eigentlich muss ja so ein balken aus drei feldern sich immer drehn aber der verschwindet einfach -.-
Kontrollier mal die Reihenfolge der Shapes (also der Reihe nach eins im Formular anklicken und dann schauen, welche Nummer das hat - da könnte was schief gelaufen sein). :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: So 15.03.15 19:00 
ok, danke :) daran hat es wohl gelegen ..

ich vermute, dass das x MOD (N-1) sein müsste
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 19:11 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ok, danke :) daran hat es wohl gelegen ..
;)


user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ich vermute, dass das x MOD (N-1) sein müsste
Hm, Vermutungen... :gruebel:

Dann mal ein konkretes Beispiel: sagen wir mal, N = 5, dann haben wir für
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
x  | x mod N
------------
0  |
1  |
2  |
3  |
4  |
5  |
6  |
17 |

Mal bitte die Tabelle ausfüllen... :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 19:16 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
x  | x mod N
------------
0  |0
1  |1
2  |2
3  |3
4  |4
5  |5
6  |1
17 |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 19:21 
Moin!

Danke, ist aber leider (nicht ganz, aber genug) falsch. :?

Kommen wir also wieder zu unserem Test-Button (das kennst du ja schon, Button auf´s Formular klatschen, doppelklicken, loslegen):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.btnTestClick(Sender: TObject);
begin
  ShowMessageFmt('x=%d, N=%d, x mod N=%d', [050 mod 5]); // das ist praktisch die erste Zeile aus der Tabelle
end;

Damit mal bitte testen gehen... :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 19:27 
da kommt dann: x=0, N=5, x MOD N=0