Elementary Cellular Automata for Terminal OS X


/ Published in: C
Save to your folder(s)

Don't forget the dec2bin.h library


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <unistd.h> //for the sleep function
  3. #include "dec2bin.h" //function to get the binary equivalent of the rule
  4. #include <string.h> //for the string comparison
  5.  
  6. int decimal;
  7. char binary[9];
  8. char neighbors[4];
  9. char bin1[4] = "111";
  10. char bin2[4] = "110";
  11. char bin3[4] = "101";
  12. char bin4[4] = "100";
  13. char bin5[4] = "011";
  14. char bin6[4] = "010";
  15. char bin7[4] = "001";
  16. char bin8[4] = "000";
  17. char singleLine[31], lineTemp[31];
  18. int i, j, iMin, iPlus, stringTest;
  19. void applyRules();
  20.  
  21. /****************************************************************/
  22. //Function to initiate the line to all 0, except the center one
  23. /****************************************************************/
  24.  
  25. void initLine(){
  26. for (i = 0; i < 31; i++) {
  27. singleLine[i] = '0';
  28. lineTemp[i] = '0';
  29. }
  30. singleLine[15] = '1';
  31. lineTemp[15] = '1';
  32. }
  33.  
  34. /****************************************************************/
  35. //Function to print the line
  36. /****************************************************************/
  37.  
  38. void printLine(){
  39. for (i = 0; i < 31; i++) {
  40. if (singleLine[i] == '0') {
  41. printf("__");
  42. }
  43. else if (singleLine[i] == '1'){
  44. printf("[]");
  45. }
  46. }
  47. printf("\n");
  48. applyRules();
  49. }
  50. /****************************************************************/
  51. //Function to apply the rules
  52. /****************************************************************/
  53.  
  54. void applyRules(){
  55. for (i = 0; i < 31; i++) {
  56.  
  57. if (i == 0){
  58. iMin = 30;
  59. iPlus = i+1;
  60. }
  61. if (i == 30){
  62. iPlus = 0;
  63. iMin = i-1;
  64. }
  65. else if (i != 0 && i != 30) {
  66. iMin = i-1;
  67. iPlus = i+1;
  68. }
  69. neighbors[0] = singleLine[iMin];
  70. neighbors[1] = singleLine[i];
  71. neighbors[2] = singleLine[iPlus];
  72.  
  73. if (strcmp(neighbors, bin1) == 0){
  74. lineTemp[i] = binary[0];
  75. }
  76. else if (strcmp(neighbors, bin2) == 0){
  77. lineTemp[i] = binary[1];
  78. }
  79. else if (strcmp(neighbors, bin3) == 0){
  80. lineTemp[i] = binary[2];
  81. }
  82. else if (strcmp(neighbors, bin4) == 0){
  83. lineTemp[i] = binary[3];
  84. }
  85. else if (strcmp(neighbors, bin5) == 0){
  86. lineTemp[i] = binary[4];
  87. }
  88. else if (strcmp(neighbors, bin6) == 0){
  89. lineTemp[i] = binary[5];
  90. }
  91. else if (strcmp(neighbors, bin7) == 0){
  92. lineTemp[i] = binary[6];
  93. }
  94. else if (strcmp(neighbors, bin8) == 0){
  95. lineTemp[i] = binary[7];
  96. }
  97.  
  98. }
  99. for (j = 0; j < 31; j++) {
  100. singleLine[j] = lineTemp[j];
  101. }
  102. usleep(50000);
  103. printLine();
  104. }
  105. /****************************************************************/
  106. /****************************************************************/
  107.  
  108.  
  109. int main (int argc, const char * argv[]) {
  110. initLine();
  111. printf("Which rule to use (0 - 255)?\n");
  112. scanf("%i", &decimal);
  113. dec2bin(decimal, binary);
  114. printf("\n The binary value of %i is %s \n",decimal,binary);
  115. printLine();
  116. return 0;
  117. }

URL: www.autofasurer.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.