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

📄 unit1.cpp

📁 CELLULAR AUTOMATA ONE DIMENSIONAL with a twist
💻 CPP
字号:
//===========================================================================
//                                                          CELLULAR AUTOMATA
//                                               ONE DIMENSIONAL with a twist
//===========================================================================
// This CA has three states and uses either a 3 or 5 cell neighborhood!
//
// For all selected rules except "Sex" birth depends on the number
// of neighbors in a 3-cell neighborhood.
//
// Sometimes random changes are added.
//
// For the "Sex" rule, a birth only occurs if there is at least one
// opposite sex couple in a 5-cell neighborhood.  The child has a random sex.
//===========================================================================

#include <vcl.h>
#pragma hdrstop

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

//================================================================= VARIABLES
int generation = 0;
int individual = 0;
int parent[500], child[500];
int count;
int choice;
int num1s = 0;
int num2s = 0;
int left, right;
int farLeft, farRight;

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

//---------------------------------------------------------- Randomize Parent
void randomizeParent (void) {
   for (int i = 0; i < 500; i++) {
      parent[i] = random(3);
   }
}

//---------------------------------------------------------------- Initialize
void initialize (void) {
   Randomize();
   Form1->Refresh();
   generation = 0;
   Form1->EditGeneration->Text = generation;
   randomizeParent();
}

//----------------------------------------------------------------------- Run
void run (void) {
   initialize();
   for (generation = 0; generation <= 500; generation++) {
      Form1->EditGeneration->Text = "          " + IntToStr(generation);

                                                // calculate child generation
                                                   //  from parent generation
      for (int i = 0; i < 500; i++) {

                                   // if sex not chosen, then count neighbors
         if (choice != 5) {
            if (i == 0) count = parent[499] + parent[0] + parent[1];
            else if (i == 499) count = parent[498] + parent[499] + parent[0];
            else count = parent[i-1] + parent[i] + parent[i+1];
         }
                                // if sex chosen, then count neighbors by sex
         if (choice == 5) {
            num1s = 0;
            num2s = 0;

            if (i == 0) {                             // wrap around problems
               farLeft = 498; left = 499; right = 1; farRight = 2;
            }
            else if (i == 1) {
               farLeft = 499; left = 0; right = 2; farRight = 3;
            }
            else if (i == 498) {
               farLeft = 496; left = 497; right = 499; farRight = 0;
            }
            else if (i == 499) {
               farLeft = 497; left = 498; right = 0; farRight = 1;
            }
            else {
               farLeft = i - 2; left = i-1; right = i+1; farRight = i + 2;
            }

                                          // check a 5-cell wide neighborhood

            if (parent[farLeft] == 1) num1s++;                      // sum 1s
            if (parent[left] == 1) num1s++;
            if (parent[i] == 1) num1s++;
            if (parent[right] == 1) num1s++;
            if (parent[farRight] == 1) num1s++;

            if (parent[farLeft] == 2) num2s++;                      // sum 2s
            if (parent[left] == 2) num2s++;
            if (parent[i] == 2) num2s++;
            if (parent[right] == 2) num2s++;
            if (parent[farRight] == 2) num2s++;
         }
                                                 // apply selected birth rule
         switch (choice) {
            case 0: {                                               // Shrubs
               if (count == 1 || count == 3 || count == 5) child[i] = 0;
               if (count == 2 || count == 4 || count == 6) child[i] = 1;
               if (count == 0) child[i] = 2;
               break;
            }
            case 1: {                                             // Triblobs
               if (count == 0 || count == 5 || count == 6) child[i] = 0;
               if (count == 3 || count == 4) child[i] = 1;
               if (count == 1 || count == 2 ) child[i] = 2;
               break;
            }
            case 2: {                                               // Gravel
               if (count == 0 || count == 4 || count == 6) child[i] = 0;
               if (count == 2 || count == 5) child[i] = 1;
               if (count == 1 || count == 3) child[i] = 2;
               break;
            }
            case 3: {                                                // Roots
               if (count == 0 || count == 4 || count == 6) child[i] = 0;
               if (count == 2 || count == 3 || count == 5) child[i] = 1;
               if (count == 1 ) child[i] = 2;
               break;
            }
            case 4: {                                            // Dendrites
               if (count == 0 || count == 4 || count == 6) child[i] = 0;
               if (count == 3 || count == 5) child[i] = 1;
               if (count == 1 || count == 2 ) child[i] = 2;
               break;
            }
            case 5: {                                                  // Sex
               if (num1s > 0 && num2s > 0) {
                  child[i] = random(2) + 1;
               }
               else child[i] = 0;
               break;
            }
            case 6: {                                               // Faults
               if (count == 2 || count == 3 || count == 4) child[i] = 0;
               if (count == 1 || count == 5 || count == 6) child[i] = 1;
               if (count == 0) child[i] = 2;
               break;
            }
            case 7: {                                               // Fields
               if (count == 1 || count == 3) child[i] = 0;
               if (count == 2 || count == 5 || count == 6) child[i] = 1;
               if (count == 0 || count == 4) child[i] = 2;
               break;
            }
            case 8: {                                               // Thorns
               if (count == 0 || count == 2 || count == 5) child[i] = 0;
               if (count == 4 || count == 3) child[i] = 1;
               if (count == 1 || count == 6) child[i] = 2;
               break;
            }
            case 9: {                                              // Red Ice
               if (count == 0 || count == 2) child[i] = 0;
               if (count == 4 || count == 3) child[i] = 1;
               if (count == 1   || count == 5|| count == 6) child[i] = 2;
               if (random(30) == 0) child[i] = 0;
               break;
            }
            case 10: {                                          // Chondrules
               if (count == 1 || count == 2 || count == 6) child[i] = 0;
               if (count == 4 || count == 3 ) child[i] = 1;
               if (count == 0 || count == 5) child[i] = 2;
               break;
            }
            case 11: {                                          // Yellow Red
               if (count == 1 || count == 2 || count == 5) child[i] = 0;
               if (count == 4 || count == 3 ) child[i] = 1;
               if (count == 0 || count == 6) child[i] = 2;
               break;
            }
            case 12: {                                           // Elevators
               if (count == 0 || count == 5 || count == 1) child[i] = 0;
               if (count == 3 || count == 4) child[i] = 1;
               if (count == 6 || count == 2 ) child[i] = 2;
               if (random(500) == 0) child[i] = 0;
               break;
            }
         }
                                                     // draw child generation
         if (child[i] == 0) Form1->Canvas->Pixels[i][generation] = clBlack;
         if (child[i] == 1) Form1->Canvas->Pixels[i][generation] = clYellow;
         if (child[i] == 2) Form1->Canvas->Pixels[i][generation] = clRed;
      }
                                // copy child generation to parent generation
      for (int i = 0; i<500; i++) {
         parent[i] = child[i];
      }

   Application->ProcessMessages();
   }
}

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

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

//------------------------------------------------- Select Rule from ComboBox
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
   choice = ComboBox1->ItemIndex;
   Application->ProcessMessages();
   run();
}

//---------------------------------------------------------- That's All Folks

⌨️ 快捷键说明

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