From 4d642f3c6e66b1b8d9046c47392e8826ffbf307f Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 30 Oct 2016 01:39:50 +0200 Subject: [PATCH] Think it works as expected now --- ca.cpp | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/ca.cpp b/ca.cpp index 8141cb1..7ddbe15 100644 --- a/ca.cpp +++ b/ca.cpp @@ -40,6 +40,9 @@ long to_int(string str) { // Get the string representation of a rule string get_rule(int rule, int bits = 9) { + if(bits < get_bits(rule) ) { + bits = get_bits(rule); + } int length = pow(2, bits); string str = to_bin(rule); int strl = strlen(str.c_str()); @@ -94,21 +97,26 @@ int main(int argc, char* argv[] ) { int initial[DIM][DIM]; for(int i = 0; i < DIM; i++) { for(int j = 0; j < DIM; j++) { - // Set up a random initial condition - initial[i][j] = rand() % 1; + if(i == HALF && j == HALF ) { + initial[i][j] = 1; + } else { + initial[i][j] = 0; + } + //initial[i][j] = rand() % 1; } } // Loop all generations 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_arr(initial); // Sleep 0.5 seconds usleep(500000); - // Clear the screen - cout << "\033[2J"; - cout << "\033[0;0H"; // The next state int next[DIM][DIM]; @@ -117,16 +125,17 @@ int main(int argc, char* argv[] ) { char state[10]; // Loop the row 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[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); } }