-4

C ++を使用してセルオートマトンをコーディングしようとしていますが、何らかの理由でプログラムを実行すると、ルールが正しく適用されていないように見えるか、まったく適用されていないようです。アドバイスをいただければ幸いです。

主なシミュレーション機能:

/* Simulate Array */
int beginSimulation(char parentArray[],char childArray[],int width, int ruleSet[]){

  //get the amount of generations the program produces
    int generationNum;
    cout << "Please enter how many generations you would like to simulate" << endl;
    cin >> generationNum;

  for(int times=0; times< generationNum; times++){  

    //loop for applying ruleset to each cell in array

    for(int i=0; i< width; i ++){

        char left = parentArray[i-1];
        char middle = parentArray[i];
        char right = parentArray[i+1];


    // if statement that compares the current cells and its neighbours 
    // with the rules in the ruleset to define the current generation.

        if(left == 'X' && middle == 'X' && right == 'X'){

          if(ruleSet[7] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[7];
        }

         else if(left == 'X' && middle == 'X' && middle == '~'){

           if(ruleSet[6] == 1){                 //do this for each rule should work
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[6];
        }

         else if (left == 'X' && middle == '~' && middle == 'X'){

          if(ruleSet[5] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[5];
         }

         else if (left == 'X' && middle == '~' && middle == '~'){

          if(ruleSet[4] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[4];
         }

         else if (left == '~' && middle == 'X' && middle == 'X'){

          if(ruleSet[3] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[3];
         }


         else if (left == '~' && middle == 'X' && middle == '~'){

          if(ruleSet[2] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[2];
         }

         else if (left == '~' && middle == '~' && middle == 'x'){

          if(ruleSet[1] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[1];
         }

         else if (left == '~' && middle == '~' && middle == '~'){
          childArray[i] = ruleSet[0];
          if(ruleSet[0] == 1){
            childArray[i] = 'X';
          } else {
            childArray[i] = '~';
          }
          childArray[i] = ruleSet[0];
         }

    }



      //for loop that iterates through the array and display all its elements
      for(int i = 0; i < width; i++)
      {

      cout << childArray[i];
      }

      cout<< endl;

      // loop to make the current generation the past generation for the next 
      //iteration of the code

      for(int c=0; c< width; c ++){
        parentArray[c] = childArray[c];
      }

  }
}

beginSimulation を使用する関数:

/* Initialize Array */
int initializeArrays(char parentArray[],char childArray[],int width, int ruleSet[]){
  //cout << "Please enter the size of the array" << endl;
  //cin >> width;

  cout << "Please enter the rule you would like to simulate" << endl;
  int userInput = 0; //initialises userInput variable to be passed
  cin >> userInput;     //into the insertItem function    

    for(int x=0; x<width; x++){

      if(x==(width/2)){
        parentArray[(width/2)] = 'X';
        continue;
      }
        cout << "";
        parentArray[x] = '~'; /* or whatever number you want */

    }
    /* parentArray[0..width-1] = "~~...~~X~...~~"
     *                                   ^
     *                                   \- at width/2
     */

    cout << parentArray << endl;

    for(int i=0; i<width; i++){
      childArray[i] = '~'; /* or whatever number you want */
      cout << "";
    }
    /* childArray[0...width - 1] = "~~...~~" */

    cout << childArray << endl;

    /* User input is bit mask to activate rules 0..7
     * e.g. input = 10 = 0x0A = 0b1010 => rule 1 and 3 activated */
    for (int z=7; z>(-1); z --){

      ruleSet[z] = userInput % 2;
      userInput = userInput/2;
     } 



     cout << ruleSet[0] << endl;
     cout << ruleSet[1] << endl;
     cout << ruleSet[2] << endl;
     cout << ruleSet[3] << endl;
     cout << ruleSet[4] << endl;
     cout << ruleSet[5] << endl;
     cout << ruleSet[6] << endl;
     cout << ruleSet[7] << endl;

    beginSimulation(parentArray, childArray, width, ruleSet);

    return 0;

}
4

2 に答える 2

0

まず、セルオートマトンを評価するときは、エッジ条件を適切に処理する必要があります。次のように記述すると、配列の両端が未定義の動作になります。

char left = parentArray[i-1];
char middle = parentArray[i];
char right = parentArray[i+1];

からまでi行くから0ですwidth-1。したがって、iis0の場合、配列のi-1インデックス-1になり、配列の先頭の前に逆方向にアクセスします。同様に、が に等しい場合、インデックスi+1は配列の末尾を越えて前方にアクセスします。iwidth-1

次に、ルールを使用して現在の値に基づいてセルの次の値を駆動しようとすると、次のステートメントによって無条件に上書きされます。次のようなステートメントがあります。

if(ruleSet[7] == 1){
    childArray[i] = 'X';
} else {
    childArray[i] = '~';
}
childArray[i] = ruleSet[7];

にあるものですぐに上書きするためruleSet[7]、 a が含まれているかどうかは関係ありません。他のすべてのルール処理に対してこれを行います。そうです、あなたのルールは結果にまったく影響を与えていません。1childAray[i]ruleSet[7]

于 2015-11-02T21:34:36.343 に答える