// Huvudfil: darwin.cpp #include "allan.cpp" #define MAX 100 #define SPECIES 1 #define MUTATION 2 void Clear_shadow(), Setup(), Single_step(); struct Space { int grid[ MAX ][ MAX ], shadow[ 6 ][ MAX ][ MAX ]; } World; struct Species { float fitness; TColor col; } S[ 3 ]; int Side = 20; void Clear_shadow() { int layer, x, y; for( x = 0; x < Side; x++ ) for( y = 0; y < Side; y++ ) { for( layer = 0; layer < 6; layer++ ) World.shadow[ layer ][ x ][ y ] = 0; } } void Draw() { int space, x, y; Clear_area(); for( x = 0; x < Side; x++ ) for( y = 0; y < Side; y++ ) if( World.grid[ x ][ y ] > 0 ) { Filled_circle_2d( x + 0.1, y + 0.1, x + 0.9, y + 0.9, S[ World.grid[ x ][ y ] ].col, S[ World.grid[ x ][ y ] ].col ); } } void Fight( int x, int y ) { float sum = 0; int layer; for( layer = 1; layer <= 5; layer++ ) { sum += S[ World.shadow[ layer ][ x ][ y ] ].fitness; } if( sum > 0 ) { sum *= Random_float(); for( layer = 1; layer < 5; layer++ ) { sum -= S[ World.shadow[ layer ][ x ][ y ] ].fitness; if( sum < 0 ) break; } World.grid[ x ][ y ] = World.shadow[ layer ][ x ][ y ]; } } void Mutation() { int found, x, y; do { x = int( Side * Random_float() ); y = int( Side * Random_float() ); if( World.grid[ x ][ y ] > 0 ) { World.grid[ x ][ y ] = 2; S[ MUTATION ].fitness = 1 + StrToFloat( Form1 -> Edit1 -> Text ) / 100.0; found = true; } } while( found == false ); Draw(); } void Plant( int x, int y ) { if( ( x < Side - 1 ) ) { if( Random_float() < 0.4 ) World.shadow[ 1 ][ x + 1 ][ y ] = World.grid[ x ][ y ]; } if( ( x > 0 ) ) { if( Random_float() < 0.4 ) World.shadow[ 2 ][ x - 1 ][ y ] = World.grid[ x ][ y ]; } if( ( y < Side - 1 ) ) { if( Random_float() < 0.4 ) World.shadow[ 3 ][ x ][ y + 1 ] = World.grid[ x ][ y ]; } if( ( y > 0 ) ) { if( Random_float() < 0.4 ) World.shadow[ 4 ][ x ][ y - 1 ] = World.grid[ x ][ y ]; } if( Random_float() < 0.4 ) World.shadow[ 5 ][ x ][ y ] = World.grid[ x ][ y ]; } void Setup() { int layer, x, y; for( x = 0; x < Side; x++ ) for( y = 0; y < Side; y++ ) World.grid[ x ][ y ] = 0; Clear_shadow(); S[ 0 ].fitness = 0; S[ SPECIES ].fitness = 1.0; S[ SPECIES ].col = BLUE; S[ MUTATION ].col = YELLOW; Coordinates( 0.0, 0.0, Side, Side ); World.grid[ Side / 2 ][ Side / 2 ] = SPECIES; Draw(); } void Single_step() { int x, y; Clear_shadow(); for( x = 0; x < Side; x++ ) for( y = 0; y < Side; y++ ) if( World.grid[ x ][ y ] > 0 ) Plant( x, y ); for( x = 0; x < Side; x++ ) for( y = 0; y < Side; y++ ) { World.grid[ x ][ y ] = 0; Fight( x, y ); } Draw(); } void Steps() { int i, n; n = StrToInt( Form1 -> Edit1 -> Text ); for( i = 0; i < n; i++ ) { Single_step(); } } //Windowsfil: Unit1.cpp //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- #include "darwin.cpp" void __fastcall TForm1::Button1Click(TObject *Sender) { Steps(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { Setup(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { Mutation(); } //--------------------------------------------------------------------------- //Headerfil: Unit1.h //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include #include //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TLabel *Label1; TEdit *Edit1; TPaintBox *SimArea; TButton *Button2; TButton *Button3; TEdit *Edit2; TLabel *Label2; TLabel *Label3; TLabel *Label4; TLabel *Label5; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall Button3Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif