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

📄 unit1.cpp

📁 1-Dimensional Cellular Automata Generator
💻 CPP
字号:
//====================== 1-Dimensional Cellular Automata Generator
//
// This version allows user to specify a third state (2) which is used to
// randomly generate the next generation from the three possible states
//
// mods:  there's gotta be a better way for users to input the child states
//             try providing a 'decimal' edit box input option and
//             then converting the input to binary for the state map
//        allow user to choose colors
//        track state changes and if none vary color along some path
//        new 'states' -- eternal life, limbo, death sink (disease?)
//              (random occurences or additional choices with specified
//               'life spans',...)
//        step button
//        pause button
//        fix the 'temporary' fix to the 2-neighborhood problem
//        add error catching routines for edit box entries
//
// ============ D. Niebuhr
// ============ April 19, 2003
//---------------------------------------------------------------------------


#include <vcl.h>
#pragma hdrstop


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


// ========================================================== Declarations
int generation = 0;
const MAXGEN = 600;
int parent[500], child[500];
int state[8];           // index is number of possible states
const NUMCELLS=500;
bool slow = false, step = false;
int k;


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


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


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


// -----------------------------------------------------------------     Run
void run(void)
{
    int parentState;    // parent ngbrhd. treated as binary no. and converted
                        // to decimal
    initialize();
    //   --------------  read the child state from the form edit boxes
    state[0] =  StrToInt(Form1->Edit2->Text);
    state[1] =  StrToInt(Form1->Edit3->Text);
    state[2] =  StrToInt(Form1->Edit4->Text);
    state[3] =  StrToInt(Form1->Edit5->Text);
    state[4] =  StrToInt(Form1->Edit6->Text);
    state[5] =  StrToInt(Form1->Edit7->Text);
    state[6] =  StrToInt(Form1->Edit8->Text);
    state[7] =  StrToInt(Form1->Edit9->Text);
    for (generation=0; generation<MAXGEN; generation++)
    {
        Form1->EditGeneration->Text = "    " + IntToStr(generation);
        if (slow==true)
        {
            for (int j=0; j<500000; j++) k=10000*j/2000;
        }
        for (int i=0; i<NUMCELLS; i++)
        {
            if (i==0) parentState = parent[NUMCELLS-1]*4+2*parent[0]+parent[1];
            else if (i==NUMCELLS-1) parentState = parent[NUMCELLS-2]*4+
                                2*parent[NUMCELLS-1]+parent[0];
            else parentState = parent[i-1]*4+2*parent[i]+parent[i+1];
            //  -------------- if parent's state was 2 randomly choose child
            //                  from the three possible states
            // but first a temporary fix to get around the problem of 'counting'
            // the neighbors when one or both are 2's.
            if (parentState>7) parentState=parentState-1;
            if (parentState>7) parentState=parentState-4;
            if (parent[i]==2) child[i]=random(3);
            else child[i] = state[parentState];


            // -------- 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<NUMCELLS; i++) parent[i] = child[i];
        Application->ProcessMessages();
    }
}


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


// ------------------------------------------------------ On Run Button Click
void __fastcall TForm1::ButtonRunClick(TObject *Sender)
{


    run();
}
//-----------------------------------------------------  On Slow Button click
void __fastcall TForm1::ButtonRunSlowClick(TObject *Sender)
{
    slow = true;
    run();
    slow=false;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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