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

📄 unit1.cpp

📁 CELLULAR AUTOMATA ONE DIMENSIONAL
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      for (int i = 0; i < 47; i++) {
                                                           // count neighbors
         if (i == 0) count = parent[46] + parent[i] + parent[i+1];
         else if (i == 46) count = parent[i-1] + parent[i] + parent[0];
         else count = parent[i-1] + parent[i] + parent[i+1];

                                                                // apply rule
         child[i] = 0;
         switch (choice) {

            case 0: {
               if (count == 0) child[i] = 1;
               break;
            }

            case 1: {
               if (count == 1) child[i] = 1;
               break;
            }

            case 2: {
               if (count == 2) child[i] = 1;
               break;
            }

            case 3: {
               if (count == 3) child[i] = 1;
               break;
            }

            case 4: {
               if (count == 0 || count == 1) child[i] = 1;
               break;
            }

            case 5: {
               if (count == 0 || count == 2) child[i] = 1;
               break;
            }

            case 6: {
               if (count == 0 || count == 3) child[i] = 1;
               break;
            }

            case 7: {
               if (count == 1 || count == 2) child[i] = 1;
               break;
            }

            case 8: {
               if (count == 1 || count == 3) child[i] = 1;
               break;
            }

            case 9: {
               if (count == 2 || count == 3) child[i] = 1;
               break;
            }

            case 10: {
               if (count == 0 || count == 1 || count == 2) child[i] = 1;
               break;
            }

            case 11: {
               if (count == 0 || count == 1 || count == 3) child[i] = 1;
               break;
            }

            case 12: {
               if (count == 0 || count == 2 || count == 3) child[i] = 1;
               break;
            }

            case 13: {
               if (count == 1 || count == 2 || count == 3) child[i] = 1;
               break;
            }

            case 14: {
               if (count == 0 || count == 1 || count == 2 || count == 3)
                  child[i] = 1;
               break;
            }
         }

                                                     // draw child generation

      }
      for (int i = 0; i < 47; i++) {
         if (child[i]) {
            if (i % interval == 0) {
               Form1->PaintBox1->Canvas->Brush->Color = clYellow;
               Form1->PaintBox1->Canvas->Pen->Color = clYellow;
               key = i + 35;
               message.data[0] = 0x90;                                // key on
               message.data[1] = key;
               midiOutShortMsg(device, message.word);
            }   
            else {
               Form1->PaintBox1->Canvas->Brush->Color = clSilver;
               Form1->PaintBox1->Canvas->Pen->Color = clSilver;
            }
         }
         else {
            if (i % interval == 0) {
               Form1->PaintBox1->Canvas->Brush->Color = clBlack;
               Form1->PaintBox1->Canvas->Pen->Color = clBlack;
            }
            else {
               Form1->PaintBox1->Canvas->Brush->Color = clGray;
               Form1->PaintBox1->Canvas->Pen->Color = clGray;
            }
         }
         Form1->PaintBox1->Canvas->Rectangle(i * 9.8, generation * 9.8,
            i * 9.8 + 9.8, generation * 9.8 + 9.8);
      }
                                                  // child generation becomes
                                                    // next parent generation
      for (int i = 0; i<47; i++) {
         parent[i] = child[i];
      }
      Sleep(200);
   Application->ProcessMessages();
   }
}


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

//------------------------------------------------------------ On Form Create
void __fastcall TForm1::FormCreate(TObject *Sender)
{
   message.data[0] = 0x90;
   //message.data[1] = 60;
   message.data[2] = 50;             // volume
   //message.data[3] = 0;
   midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL);
}

//------------------------------------------------------- Keyboard Mouse Down
void __fastcall TForm1::ImageKeyboardMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   if (Form1->RadioGroupKeyStrike->ItemIndex == 0) {
      x = X;
      y = Y;
      selectKeyFromImage();
   }
}

//------------------------------------------------------- Keyboard Mouse Move
void __fastcall TForm1::ImageKeyboardMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
   if (Form1->RadioGroupKeyStrike->ItemIndex == 1) {
      x = X;
      y = Y;
      selectKeyFromImage();
   }
}

//------------------------------------------------------- Instrument ComboBox
void __fastcall TForm1::ComboBoxPatchChange(TObject *Sender)
{
   instrument = ComboBoxPatch->ItemIndex;
   if (instrument > 95) {
      instrument += 24;                                    // special effects
   }
   message.data[0] = 0xC0;                               // choose instrument
   message.data[1] = instrument;
   midiOutShortMsg(device, message.data[0]);
   midiOutShortMsg(device, message.data[1]);
   midiOutSetVolume(device, 0xFFFF);
   message.data[0] = 0x90;                                         // note on
   //message.data[1] = 60;                                      // key number
   midiOutShortMsg(device, message.data[0]);
   //midiOutShortMsg(device, message.data[1]);
}

//------------------------------------------------------------ Tempo TrackBar Loudness TrackBar Play Scale Button
void __fastcall TForm1::ButtonPlayScaleClick(TObject *Sender)
{
   playScale();
}

//---------------------------------------------------------- Play Tune Button
void __fastcall TForm1::ButtonPlayTuneClick(TObject *Sender)
{
   playTune();
}

//------------------------------------------------------ Play Random 1 Button
void __fastcall TForm1::ButtonPlayRandom1Click(TObject *Sender)
{
   playRandom1();
}

//------------------------------------------------------ Play Random 2 Button
void __fastcall TForm1::ButtonRandom2Click(TObject *Sender)
{
   playRandom2();
}

//--------------------------------------------------------- Play Quiet Button
void __fastcall TForm1::ButtonQuietClick(TObject *Sender)
{
   for (int i = 35; i < 82; i++) {
   message.data[0] = 0x80;                                        // note off
   message.data[1] = i;                                       // for each key
   midiOutShortMsg(device, message.data[0]);
   midiOutShortMsg(device, message.data[1]);
   }
}

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

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

void __fastcall TForm1::ComboBoxNeighborsChange(TObject *Sender)
{
   choice = ComboBoxNeighbors->ItemIndex;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TrackBarLoudnessChange(TObject *Sender)
{
   loudness = TrackBarLoudness->Position;
   message.data[2] = loudness;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TrackBarIntervalChange(TObject *Sender)
{
   interval = TrackBarInterval->Position;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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