//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" #define DEAD 0 #define ALIVE 1 #define RANDOM 2 #define INVERSE 3 TForm2 *Form2; //================================================================= VARIABLES bool stop = false; int thisWorld[100][100]; int nextWorld[100][100]; int e, s; int re, rs, we, ws; //================================================================= FUNCTIONS void showThisWorld (void) { for (e = 0; e < 100; e++) { for (s = 0; s < 100; s++) { if (thisWorld[e][s] == DEAD) { Form2->PaintBox1->Canvas->Pen->Color = clRed; Form2->PaintBox1->Canvas->Brush->Color = clRed; Form2->PaintBox1->Canvas->Rectangle (e * 5, s * 5, e * 5 + 5, s * 5 + 5); } if (thisWorld[e][s] == ALIVE) { Form2->PaintBox1->Canvas->Pen->Color = clLime; Form2->PaintBox1->Canvas->Brush->Color = clLime; Form2->PaintBox1->Canvas->Rectangle (e * 5, s * 5, e * 5 + 5, s * 5 + 5); } } } } int neighbors (void) { int number = 0; for (re = e - 1; re <= e + 1; re++) { if (re == -1) we = 99; else if (re == 100) we = 0; else we = re; for (rs = s - 1; rs <= s + 1; rs++) { if (rs == -1) ws = 99; else if (rs == 100) ws = 0; else ws = rs; if (we == e && ws == s) continue; if (thisWorld[we][ws] == ALIVE) number++; } } return number; } void computeNextWorld (void) { for (e = 0; e < 100; e++) { for (s = 0; s < 100; s++) { if (thisWorld[e][s] == DEAD) { if (neighbors() == 3) { nextWorld[e][s] = ALIVE; } else nextWorld[e][s] = DEAD; } if (thisWorld[e][s] == ALIVE) { if (neighbors() == 2 || neighbors() == 3) { nextWorld[e][s] = ALIVE; } else nextWorld[e][s] = DEAD; } } } } void copyNextWorldToThisWorld (void) { for (e = 0; e < 100; e++) { for (s = 0; s < 100; s++) { thisWorld[e][s] = nextWorld[e][s]; } } } void step (void) { computeNextWorld(); copyNextWorldToThisWorld(); } void run (void) { while (stop == false) { step(); showThisWorld(); Application->ProcessMessages(); } } void reset (int choice) { for (e = 0; e < 100; e++) { for (s = 0; s < 100; s++) { if (choice == RANDOM) { thisWorld[e][s] = random(2); } if (choice == DEAD) { thisWorld[e][s] = DEAD; } if (choice == INVERSE) { thisWorld[e][s] = !thisWorld[e][s]; } } } } //============================================================ EVENT HANDLERS //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonRunClick(TObject *Sender) { stop = false; run(); } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonStepClick(TObject *Sender) { stop = true; step(); } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonStopClick(TObject *Sender) { stop = true; } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonRandomClick(TObject *Sender) { reset(RANDOM); } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonDeadClick(TObject *Sender) { reset(DEAD); } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonInverseClick(TObject *Sender) { reset(INVERSE); } //--------------------------------------------------------------------------- void __fastcall TForm2::ButtonShowThisWorldClick(TObject *Sender) { showThisWorld(); } //---------------------------------------------------------------------------