我正在尝试使用 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;
}