// Program that mimicks the finite-state cellular autonoma for Rule 30.
#include <iostream>
#include <cstdlib>
using namespace std;
void setupRow();
void printRow();
void updateRow();
int findNextCellState(int left_neighbor, int cell, int right_neighbor);
const int WIDTH=80;
int row[WIDTH];
int main()
{
setupRow();
printRow();
for (int i = 0; i < 40; i++)
{
updateRow();
printRow();
}
}
// The setupRow function creates an initial state for the cellular autonoma
void setupRow()
{
int row[ WIDTH ] = {};
row[39]=1;
}
// The printRow function outputs the state of the row to the user. For all
// cells that are 0, it outputs a space (" "), for all cells that are 1, it
// outputs a star ("*")
void printRow()
{
for ( int i=0; i < WIDTH; i++)
{
if ( i == 0 )
cout << " ";
else if ( i == 1 )
cout << "*"<< endl;
}
}
// The updateRow function updates the contents of the row after calculating
// the next states using the findNextCellState function
void updateRow()
{
int tempRow[WIDTH];
for ( int i =0; i < WIDTH; i++ )
{
tempRow[i] = row[i];
}
for ( int i = 0; i < WIDTH; i++)
{
int left = 0;
int cellt = 0;
int right = 0;
if ( i > 0 )
{
left = tempRow[i-1];
}
cellt = tempRow[i+1];
if (i < WIDTH - 1 )
{
right = tempRow[i+1];
}
row[i] = findNextCellState (left, cellt, right );
}
}
// The findNextCellState function implements the Rule 30 celular autonoma rules.
// The states for this are:
//
// 111 110 101 100 011 010 001 000
// 0 0 0 1 1 1 1 0
int findNextCellState(int left_neighbor, int cell, int right_neighbor)
{
if (0 == cell)
{
if (1 == (left_neighbor + right_neighbor))
{
return 1;
}
else
{
return 0;
}
}
else
{
if (1 == left_neighbor)
{
return 0;
}
else
{
return 1;
}
}
}
I posted this earlier, but it was in the wrong forum. Sorry
This post has been edited by macosxnerd101: 09 October 2010 - 03:36 AM
Reason for edit:: Added end code tag.

New Topic/Question
Reply




MultiQuote




|