Pages

1-Dimensional Cellular Automata (C++ Program)‏

Saturday, 30 November 2013
#include<iostream>
#include <string>

using namespace::std;

int main(){
    
    const char a = 'X';
    const char d = ' ';
    
    //explaination for the user
    
    cout<<"This program displays a one-dimensional cellular automaton.\n\n"
        "A cellular automaton displays successive iterations (in this case the next row) based on the previous "
        "according to predefined rules. Here is an example of this. \n\nPress enter to iterate and enter 's' to stop."
    "The initial rule is any cell with only one neighbor is an X (alive=a) in the next time step.\n\n"
    "Otherwise, it is blank."<<endl;
    
    cin.get();

    char row1[]={a,d,a,d,a,d,d,a,d,d,d,a,a,d,d,a,a,a,d,d,d,a,a,a,d,d,d,d,d,a,d,a,d,a,a,d,a,a,d,a,a,d,d,a,d,d,d,d,a,a,a,a,d,d,d,a,a,a,d,d,d,d,d,a,d,a,d,a,a,d,a,a,d,a,a,d,d,a,d,d,d,d};
    char row2[]={a,d,a,d,a,d,d,a,d,d,d,a,a,d,d,a,a,a,d,d,d,a,a,a,d,d,d,d,d,a,d,a,d,a,a,d,a,a,d,a,a,d,d,a,d,d,d,d,a,a,a,a,d,d,d,a,a,a,d,d,d,d,d,a,d,a,d,a,a,d,a,a,d,a,a,d,d,a,d,d,d,d};

    string currentRow=row1; // The current character array is assigned to a string.
    string nextRow=row2; //The next character array is assigned to a string.

    //two strings must be used. All cells change at the same time
    //They represent two discrete time steps. The references will be switched at the end of the loop
    //when the next row becomes the current row.

    cout<<currentRow;
    
    while(cin.get()!='s'){//This steps though upon carriage return unless the user selects 's' for stop
        
        for(int i=0; i<currentRow.length(); i++){//iterate over the row
            
            //set neighbors to zero for this cell
            int neighbors=0;

            for(int j=i-1; j<=i+1;j+=2){

        //iterate through neighboors (i+1,. Must avoid out of bounds. To do this we need the index of
                //to always be in the array. The left neighoor of the first cell is the last cell the right neighbor of the last cell             
        //is the first cell. The field is 'warped'. 


        // index:       [0]     [1]     [2]     [3]  
            //neighboors  [3] [1] [0] [2] [1] [3] [2] [0]

        //must reassign to a different variable because j is a loop condition. 
        //Manipulating j for the following calulation directly causes an infinite loop.

        int k=j;
        
        //this causes k to be the index of the neighbor cells of index i.
        k=(k+currentRow.length())%currentRow.length();

                if (currentRow[k]==a)//test if neighboor is alive (a is a const char)
                neighbors++;
        }


        //the possible values for neighbors are 0,1 and 2. Change this for different behavior.
        if (neighbors!=1)
        nextRow[i]=d;
        else
        nextRow[i]=a;
        
        }
        cout<<nextRow;

            string temp = currentRow;
            currentRow = nextRow;
            nextRow = temp;

        
        
    }
    
    cout<<"Thank you. If you are interested in cellular automata, look up John Conway's game of life."<<endl;

    return 0;
}