⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 元胞自动机运用于模式识别上的小程序
💻 CPP
字号:
//===========================================================================
//                                  A CELLULAR AUTOMATA WITH PATTERN MATCHING
//                                                           January 30, 2006
//
//===========================================================================

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

//===========================================================================
//                                                                  VARIABLES
//===========================================================================
#define EMPTY 0               // emptiness
#define SAND 1              // sand
#define CONTAINER 2                // container
#define SANDGENERATOR 3             // sand generator



int thisWorld[100][100], nextWorld[100][100];
int east, south;
int top, bottom, right, left;
int relativeE, relativeS;
int wrappedE, wrappedS;
int neighbors;
int maxSize = 100;
bool stop = false;
int iterations = 0;
int milliseconds = 50;
TColor color;
TColor cursorColor = clYellow;
int choice = SAND;

//===========================================================================
//                                                                  FUNCTIONS
//===========================================================================

//-------------------------------------------------------------------- jiggle
void jiggle (void) {
   for (int i = 5; i > 0; i--) {
      for (int j = 10; j > 0; j--) {
         Form1->Top += j;
         Form1->Left += j;
         Form1->Top -= j;
         Form1->Left -= j;
      }
   }
}

//------------------------------------------------------------ show thisWorld
void showThisWorld (void) {
   for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
         switch (thisWorld[i][j]) {
            case EMPTY: {
               color = clBlack;
               break;
               }
            case SAND: {
               color = clYellow;
               break;
               }
            case CONTAINER: {
               color = clAqua;
               break;
               }
            case SANDGENERATOR: {
               color = clFuchsia;
               break;
               }
         }
         Form1->PaintBox1->Canvas->Pen->Color = color;
         Form1->PaintBox1->Canvas->Brush->Color = color;
         Form1->PaintBox1->Canvas->Rectangle
            (i * 5, j * 5, i * 5 + 5, j * 5 + 5);
      }
   }
}

//--------------------------------------------------------------- updateDisplay
void updateDisplay (void) {
   for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
         if (thisWorld[i][j] != nextWorld[i][j]) {
            switch (nextWorld[i][j]) {
               case EMPTY: {
                  color = clBlack;
                  break;
                  }
               case SAND: {
                  color = clYellow;
                  break;
                  }
               case CONTAINER: {
                  color = clAqua;
                  break;
                  }
               case SANDGENERATOR: {
                  color = clFuchsia;
                  break;
                  }
            }
         Form1->PaintBox1->Canvas->Pen->Color = color;
         Form1->PaintBox1->Canvas->Brush->Color = color;
         Form1->PaintBox1->Canvas->Rectangle
            (i * 5, j * 5, i * 5 + 5, j * 5 + 5);
         }
      }
   }
}

//----------------------------------------------- copy nextWorld to thisWorld
void copyNextWorldToThisWorld (void) {
   for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
         thisWorld[i][j] = nextWorld[i][j];
      }
   }
}

//-------------------------------------------compute nextWorld from thisWorld
void computeNextWorldFromThisWorld(void) {
     for (east=0; east<100; east++) {
          for (south=0; south<100; south++) {

               // get valid neighborhood parameters
               top = south - 1;
               if (top < 0) top = maxSize - 1;
               bottom = south + 1;
               if (bottom >= maxSize) bottom = 0;
               left = east - 1;
               if (left < 0) left = maxSize - 1;
               right = east + 1;
               if (right >= maxSize) right = 0;

               switch (thisWorld[east][south]) {

                    // if the cell is EMPTY
                    case 0: {
                        if (thisWorld[east][top] == SANDGENERATOR) {
                              nextWorld[east][south] = SAND;
                        }
                        if (thisWorld[east][top] == SAND)
                           nextWorld[east][south] = SAND;
                        if (thisWorld[east][top] == SAND
                           && thisWorld[east][bottom] == SANDGENERATOR) {
                              nextWorld[east][south] = EMPTY;
                        }
                        break;
                        }
                    // if a cell is SAND
                    case 1: {
                        if (thisWorld[east][bottom] == EMPTY)
                           nextWorld[east][south] = EMPTY;
                        if (thisWorld[east][top] == SAND)
                           nextWorld[east][south] = SAND;
                        if (thisWorld[east][top] == SANDGENERATOR)
                           nextWorld[east][south] = EMPTY;
                        break;
                        }

                    // if the cell is CONTAINER
                    case 2: {
                         // don't change
                         nextWorld[east][south] = CONTAINER;
                         break;
                         }
                    // if a cell is SANDGENERATOR
                    case 3: {
                         // don't change
                         nextWorld[east][south] = SANDGENERATOR;
                         break;
                         }
               }
          }
     }
}

//---------------------------------------------------------------------- step
void step (void) {
   iterations++;
   computeNextWorldFromThisWorld();
   updateDisplay();
   copyNextWorldToThisWorld();
   Form1->EditIterations->Text = iterations;
   Sleep(milliseconds);
}

//----------------------------------------------------------------------- run
void run (void) {
   while (stop == false) {
      step();
      Application->ProcessMessages();
   }
}

//--------------------------------------------------------------------- reset
void reset (int choice) {
   iterations = 0;
   jiggle();
   for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
         if (choice == 2) {
            thisWorld[i][j] = random(2);
         }
         if (choice == 0) {
            thisWorld[i][j] = 0;
         }
         if (choice == 1) {
            thisWorld[i][j] = 1;
         }
      }
   }
   showThisWorld();
}

//===========================================================================
//                                                             EVENT HANDLERS
//===========================================================================

//------------------------------------------------------- PaintBox Mouse Down
void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   int i = X/5;
   int j = Y/5;
   thisWorld[i][j] = choice;
    Form1->PaintBox1->Canvas->Pen->Color = cursorColor;
    Form1->PaintBox1->Canvas->Brush->Color = cursorColor;
    Form1->PaintBox1->Canvas->Rectangle
         (i * 5, j * 5, i * 5 + 5, j * 5 + 5);
}

//---------------------------------------------------------------- Run Button
void __fastcall TForm1::ButtonRunClick(TObject *Sender)
{
   stop = false;
   run();
}

//--------------------------------------------------------------- Step Button
void __fastcall TForm1::ButtonStepClick(TObject *Sender)
{
   stop = true;
   step();
}

//--------------------------------------------------------------- Stop Button
void __fastcall TForm1::ButtonStopClick(TObject *Sender)
{
   stop = true;
}

//---------------------------------------------------------------- Form Paint
void __fastcall TForm1::FormPaint(TObject *Sender)
{
   showThisWorld();
}

//----------------------------------------------------------------- Form Show
void __fastcall TForm1::FormShow(TObject *Sender)
{
   randomize();
   reset(0);
}

//------------------------------------------------------------- Random Button
void __fastcall TForm1::ButtonRandomClick(TObject *Sender)
{
   reset(2);
}

//--------------------------------------------------------------- Dead Button
void __fastcall TForm1::ButtonDeadClick(TObject *Sender)
{
   reset(0);
}

//-------------------------------------------------------------- Alive Button
void __fastcall TForm1::ButtonAliveClick(TObject *Sender)
{
   reset(1);
}

//---------------------------------------------------------- Track Bar Change
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
   milliseconds = Form1->TrackBar1->Position;
}

//------------------------------------------------------- PaintBox Mouse Move
void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
   int i = X/5;
   int j = Y/5;
   Form1->EditCell->Text = thisWorld[i][j];
}

//----------------------------------------------------- ShapeEmpty Mouse Down
void __fastcall TForm1::ShapeEmptyMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   choice = EMPTY;
   cursorColor = clBlack;
   Form1->GroupBoxCursorColor->Color = cursorColor;
}

//------------------------------------------------------ ShapeSand Mouse Down
void __fastcall TForm1::ShapeSandMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   choice = SAND;
   cursorColor = clYellow;
   Form1->GroupBoxCursorColor->Color = cursorColor;
}

//------------------------------------------------- ShapeContainer Mouse Down
void __fastcall TForm1::ShapeContainerMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   choice = CONTAINER;
   cursorColor = clAqua;
   Form1->GroupBoxCursorColor->Color = cursorColor;
}

//-------------------------------------------- ShapeSandGenerator Mouse Down
void __fastcall TForm1::ShapeSandGeneratorMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   choice = SANDGENERATOR;
   cursorColor = clFuchsia;
   Form1->GroupBoxCursorColor->Color = cursorColor;
}

//----------------------------------------------------------------------- End

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -