Revision: 34838
Updated Code
at October 29, 2010 00:34 by autofasurer
Updated Code
#include <stdio.h> #include <unistd.h> //for the sleep function #include "dec2bin.h" //function to get the binary equivalent of the rule #include <string.h> //for the string comparison int decimal; char binary[9]; char neighbors[4]; char bin1[4] = "111"; char bin2[4] = "110"; char bin3[4] = "101"; char bin4[4] = "100"; char bin5[4] = "011"; char bin6[4] = "010"; char bin7[4] = "001"; char bin8[4] = "000"; char singleLine[31], lineTemp[31]; int i, j, iMin, iPlus, stringTest; void applyRules(); /****************************************************************/ //Function to initiate the line to all 0, except the center one /****************************************************************/ void initLine(){ for (i = 0; i < 31; i++) { singleLine[i] = '0'; lineTemp[i] = '0'; } singleLine[15] = '1'; lineTemp[15] = '1'; } /****************************************************************/ //Function to print the line /****************************************************************/ void printLine(){ for (i = 0; i < 31; i++) { if (singleLine[i] == '0') { printf("__"); } else if (singleLine[i] == '1'){ printf("[]"); } } printf("\n"); applyRules(); } /****************************************************************/ //Function to apply the rules /****************************************************************/ void applyRules(){ for (i = 0; i < 31; i++) { if (i == 0){ iMin = 30; iPlus = i+1; } if (i == 30){ iPlus = 0; iMin = i-1; } else if (i != 0 && i != 30) { iMin = i-1; iPlus = i+1; } neighbors[0] = singleLine[iMin]; neighbors[1] = singleLine[i]; neighbors[2] = singleLine[iPlus]; if (strcmp(neighbors, bin1) == 0){ lineTemp[i] = binary[0]; } else if (strcmp(neighbors, bin2) == 0){ lineTemp[i] = binary[1]; } else if (strcmp(neighbors, bin3) == 0){ lineTemp[i] = binary[2]; } else if (strcmp(neighbors, bin4) == 0){ lineTemp[i] = binary[3]; } else if (strcmp(neighbors, bin5) == 0){ lineTemp[i] = binary[4]; } else if (strcmp(neighbors, bin6) == 0){ lineTemp[i] = binary[5]; } else if (strcmp(neighbors, bin7) == 0){ lineTemp[i] = binary[6]; } else if (strcmp(neighbors, bin8) == 0){ lineTemp[i] = binary[7]; } } for (j = 0; j < 31; j++) { singleLine[j] = lineTemp[j]; } usleep(50000); printLine(); } /****************************************************************/ /****************************************************************/ int main (int argc, const char * argv[]) { initLine(); printf("Which rule to use (0 - 255)?\n"); scanf("%i", &decimal); dec2bin(decimal, binary); printf("\n The binary value of %i is %s \n",decimal,binary); printLine(); return 0; }
Revision: 34837
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 29, 2010 00:29 by autofasurer
Initial Code
#include <stdio.h> #include <unistd.h> //for the sleep function #include "dec2bin.h" //function to get the binary equivalent of the rule #include "string.h" //for the string comparison int decimal; char binary[9]; char neighbors[4]; char bin1[4] = "111"; char bin2[4] = "110"; char bin3[4] = "101"; char bin4[4] = "100"; char bin5[4] = "011"; char bin6[4] = "010"; char bin7[4] = "001"; char bin8[4] = "000"; char singleLine[31], lineTemp[31]; int i, j, iMin, iPlus, stringTest; void applyRules(); /****************************************************************/ //Function to initiate the line to all 0, except the center one /****************************************************************/ void initLine(){ for (i = 0; i < 31; i++) { singleLine[i] = '0'; lineTemp[i] = '0'; } singleLine[15] = '1'; lineTemp[15] = '1'; } /****************************************************************/ //Function to print the line /****************************************************************/ void printLine(){ for (i = 0; i < 31; i++) { if (singleLine[i] == '0') { printf("__"); } else if (singleLine[i] == '1'){ printf("[]"); } } printf("\n"); applyRules(); } /****************************************************************/ //Function to apply the rules /****************************************************************/ void applyRules(){ for (i = 0; i < 31; i++) { if (i == 0){ iMin = 30; iPlus = i+1; } if (i == 30){ iPlus = 0; iMin = i-1; } else if (i != 0 && i != 30) { iMin = i-1; iPlus = i+1; } neighbors[0] = singleLine[iMin]; neighbors[1] = singleLine[i]; neighbors[2] = singleLine[iPlus]; if (strcmp(neighbors, bin1) == 0){ lineTemp[i] = binary[0]; } else if (strcmp(neighbors, bin2) == 0){ lineTemp[i] = binary[1]; } else if (strcmp(neighbors, bin3) == 0){ lineTemp[i] = binary[2]; } else if (strcmp(neighbors, bin4) == 0){ lineTemp[i] = binary[3]; } else if (strcmp(neighbors, bin5) == 0){ lineTemp[i] = binary[4]; } else if (strcmp(neighbors, bin6) == 0){ lineTemp[i] = binary[5]; } else if (strcmp(neighbors, bin7) == 0){ lineTemp[i] = binary[6]; } else if (strcmp(neighbors, bin8) == 0){ lineTemp[i] = binary[7]; } } for (j = 0; j < 31; j++) { singleLine[j] = lineTemp[j]; } usleep(50000); printLine(); } /****************************************************************/ /****************************************************************/ int main (int argc, const char * argv[]) { initLine(); printf("Which rule to use (0 - 255)?\n"); scanf("%i", &decimal); dec2bin(decimal, binary); printf("\n The binary value of %i is %s \n",decimal,binary); printLine(); return 0; }
Initial URL
www.autofasurer.net
Initial Description
Don't forget the dec2bin.h library
Initial Title
Elementary Cellular Automata for Terminal OS X
Initial Tags
Initial Language
C