//=========================================================================== // Nick's GOL //=========================================================================== //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #define DEAD 0 #define ALIVE 1 #define RANDOM 2 //--------------------------------------------------------------------------- //=========================================================================== // VARIALBES //=========================================================================== int thisWorld[100][100]; int nextWorld[100][100]; int x, y; int rX, rY; int wX, wY; bool stop = true; int iterations = 0; int neighbors; int milliseconds; int choice; bool blinkership[17][31] = { //rows, columns 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,1,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1, 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1, 0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1, 0,0,1,1,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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FUNCTIONS //=========================================================================== void showThisWorld (void) { for (x = 0; x < 100; x++) { for (y = 0; y < 100; y++) { if (thisWorld[x][y] == DEAD) { Form1->PaintBox1->Canvas->Brush->Color = clRed; Form1->PaintBox1->Canvas->Pen->Color = clRed; } if (thisWorld[x][y] == ALIVE) { Form1->PaintBox1->Canvas->Brush->Color = clLime; Form1->PaintBox1->Canvas->Pen->Color = clLime; } Form1->PaintBox1->Canvas-> Rectangle(x * 5, y * 5, x * 5 + 5, y * 5 + 5); } } } void showChanges (void) { for (x = 0; x < 100; x++) { for (y = 0; y < 100; y++) { if (thisWorld[x][y] != nextWorld[x][y]) { if (nextWorld[x][y] == DEAD) { Form1->PaintBox1->Canvas->Brush->Color = clRed; Form1->PaintBox1->Canvas->Pen->Color = clRed; } if (nextWorld[x][y] == ALIVE) { Form1->PaintBox1->Canvas->Brush->Color = clLime; Form1->PaintBox1->Canvas->Pen->Color = clLime; } Form1->PaintBox1->Canvas-> Rectangle(x * 5, y * 5, x * 5 + 5, y * 5 + 5); } } } } int mv (int questionable) { int valid; valid = questionable; if (questionable >= 100) { valid = questionable - 100; } if (questionable < 0) { valid = questionable + 100; } return valid; } void reset (int choice) { for (x = 0; x < 100; x++) { for (y = 0; y < 100; y++) { if (choice == RANDOM) { thisWorld[x][y] = random(2); } if (choice == DEAD) { thisWorld[x][y] = DEAD; } } } iterations = 0; } void countNeighbors (void) { neighbors = 0; for (rX = x - 1; rX <= x + 1; rX++) { for (rY = y - 1; rY <= y + 1; rY++) { if (rX == x && rY == y) continue; // don't count self if ((thisWorld[mv(rX)][mv(rY)] == ALIVE)) { neighbors++; } } } } void computeNextWorld (void) { for (x = 0; x < 100; x++) { for (y = 0; y < 100; y++) { countNeighbors(); // if DEAD if (thisWorld[x][y] == DEAD) { if (neighbors == 3) nextWorld[x][y] = ALIVE; else nextWorld[x][y] = DEAD; } // if ALIVE if (thisWorld[x][y] == ALIVE) { if (neighbors == 2 || neighbors == 3) nextWorld[x][y] = ALIVE; else nextWorld[x][y] = DEAD; } } } } void copyNextWorldToThisWorld (void) { for (x = 0; x < 100; x++) { for (y = 0; y < 100; y++) { thisWorld[x][y] = nextWorld[x][y]; } } } void step (void) { iterations++; computeNextWorld(); showChanges(); copyNextWorldToThisWorld(); //showThisWorld(); Form1->EditIterations->Text = iterations; Sleep(milliseconds); } void run (void) { while (stop == false) { step(); Application->ProcessMessages(); } } //=========================================================================== // EVENT HANDLERS //=========================================================================== __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { randomize(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonResetRandomClick(TObject *Sender) { reset(RANDOM); showThisWorld(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonResetDeadClick(TObject *Sender) { reset(DEAD); showThisWorld(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonRunClick(TObject *Sender) { stop = false; run(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonStepClick(TObject *Sender) { stop = true; step(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonStopClick(TObject *Sender) { stop = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { int x, y; x = X/5; y = Y/5; if (Button == 0) { // left thisWorld[x][y] = ! thisWorld[x][y]; showThisWorld(); } if (Button == 1) { // right if (choice == 0) { // blinkership for (int i = 0; i < 17; i++) { for (int j = 0; j < 31; j++) { if (blinkership[i][j] == ALIVE) { thisWorld[mv(x + j)][mv(y + i)] = ALIVE; } else thisWorld[mv(x + j)][mv(y + i)] = DEAD; } } } if (choice == 1) { // gospergun for (int i = 0; i < 9; i++) { for (int j = 0; j < 37; j++) { if (gospergun[i][j] == ALIVE) { thisWorld[mv(x + j)][mv(y + i)] = ALIVE; } else thisWorld[mv(x + j)][mv(y + i)] = DEAD; } } } } showThisWorld(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { reset(RANDOM); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Paint(TObject *Sender) { showThisWorld(); } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject *Sender) { milliseconds = TrackBar1->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox1Change(TObject *Sender) { choice = ComboBox1->ItemIndex; } //---------------------------------------------------------------------------