Think it works as expected now

master
Micke Nordin 8 years ago
parent 3c71286b6d
commit 4d642f3c6e

@ -40,6 +40,9 @@ long to_int(string str) {
// Get the string representation of a rule // Get the string representation of a rule
string get_rule(int rule, int bits = 9) { string get_rule(int rule, int bits = 9) {
if(bits < get_bits(rule) ) {
bits = get_bits(rule);
}
int length = pow(2, bits); int length = pow(2, bits);
string str = to_bin(rule); string str = to_bin(rule);
int strl = strlen(str.c_str()); int strl = strlen(str.c_str());
@ -94,21 +97,26 @@ int main(int argc, char* argv[] ) {
int initial[DIM][DIM]; int initial[DIM][DIM];
for(int i = 0; i < DIM; i++) { for(int i = 0; i < DIM; i++) {
for(int j = 0; j < DIM; j++) { for(int j = 0; j < DIM; j++) {
// Set up a random initial condition if(i == HALF && j == HALF ) {
initial[i][j] = rand() % 1; initial[i][j] = 1;
} else {
initial[i][j] = 0;
}
//initial[i][j] = rand() % 1;
} }
} }
// Loop all generations // Loop all generations
for(int i = 0; i < gens; i++) { for(int i = 0; i < gens; i++) {
// Clear the screen
cout << "\033[2J";
cout << "\033[0;0H";
cout << "Rule: " << rule << " generation: " << i << endl;
// Print the current state // Print the current state
print_arr(initial); print_arr(initial);
// Sleep 0.5 seconds // Sleep 0.5 seconds
usleep(500000); usleep(500000);
// Clear the screen
cout << "\033[2J";
cout << "\033[0;0H";
// The next state // The next state
int next[DIM][DIM]; int next[DIM][DIM];
@ -117,16 +125,17 @@ int main(int argc, char* argv[] ) {
char state[10]; char state[10];
// Loop the row // Loop the row
for(int k = 0; k < DIM; k++) { for(int k = 0; k < DIM; k++) {
state[0] = initial[ (j -1) % DIM ][ (k-1) % DIM ] + 48;
state[1] = initial[ (j -1) % DIM ][k] + 48;
state[2] = initial[ (j -1) % DIM ][ (k+1) % DIM ] + 48;
state[3] = initial[j][ (k-1) % DIM ] + 48;
state[4] = initial[j][k] + 48 ;
state[5] = initial[j][ (k+1) % DIM ] + 48;
state[6] = initial[ (j +1) % DIM ][ (k-1) % DIM ] + 48;
state[7] = initial[ (j +1) % DIM ][k] + 48;
state[8] = initial[ (j +1) % DIM ][ (k+1) % DIM ] + 48;
state[9] = '\0'; state[9] = '\0';
state[8] = initial[ (j -1) % DIM ][ (k-1) % DIM ] + 48;
state[7] = initial[ (j -1) % DIM ][k] + 48;
state[6] = initial[ (j -1) % DIM ][ (k+1) % DIM ] + 48;
state[5] = initial[j][ (k-1) % DIM ] + 48;
state[4] = initial[j][k] + 48 ;
state[4] = initial[j][ (k+1) % DIM ] + 48;
state[2] = initial[ (j +1) % DIM ][ (k-1) % DIM ] + 48;
state[1] = initial[ (j +1) % DIM ][k] + 48;
state[0] = initial[ (j +1) % DIM ][ (k+1) % DIM ] + 48;
next[j][k] = get_val_at_pos(to_int(state), rule); next[j][k] = get_val_at_pos(to_int(state), rule);
} }
} }

Loading…
Cancel
Save