Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. /*
  2.       AC21009 Cellular Automaton
  3.     Nathan Mcnally & Joseph Wyatt
  4.        150010438      150012345
  5. */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <stdbool.h>
  10. #include "main.h"
  11.  
  12. // Set the value of the rule you would like displayed check out - - - - > http://mathworld.wolfram.com/ElementaryCellularAutomaton.html
  13. int dRULE = 182;
  14.  
  15. int parent[100]  = {0};
  16. int child [100]  = {0};
  17. int rule[8]     = {0};  
  18. int generations = 50;  
  19.  
  20. char tile_filled = 'X';
  21. char tile_empty  = ' ';
  22.  
  23. int numParentValues = sizeof(parent) / sizeof(int); int numChildValues = sizeof(child) / sizeof(int);
  24.  
  25. int main()
  26. {
  27.     draw1D_ca();
  28.     return 0;
  29. }
  30.  
  31. // Takes in a decimal number converts to binary and splits into an array
  32. void setRule (int n){
  33.   int counter, k, pos;
  34.   int result[8] = {0};
  35.   pos = 0;
  36.   for (counter = 7; counter >= 0; counter--)
  37.   {
  38.     k = n >> counter;
  39.  
  40.     if (k & 1){
  41.       rule[pos] = 1;  
  42.       pos ++;      
  43.     }else{
  44.       rule[pos] = 0;
  45.       pos ++;
  46.     }
  47.   }
  48.  
  49.   printf("\n");
  50. }
  51.  
  52.  
  53. void draw1D_ca (){
  54.     parent[50] = 1;
  55.     setRule(dRULE);
  56.     int tl, tm, tr = 0;
  57.  
  58.     for(int y=0; y < generations; y++){
  59.         for(int i = 0; i < numParentValues; i ++){
  60.             // Instead of printing 1's and 0's prints either 'x' or '_' as it is easier to spot patterns
  61.             if(parent[i] == 0){printf("%c", tile_empty);}else{printf("%c", tile_filled);}
  62.         }
  63.  
  64.         for (int x=0; x<numParentValues; x++){
  65.            
  66.             if(x == 0){
  67.                 tl = 0;
  68.                 tm = parent[x];
  69.                 tr = parent[x+1];
  70.             }else if (x == 30){
  71.                 tl = parent[x-1];
  72.                 tm = parent[x];
  73.                 tr = 0;
  74.             }else{
  75.                 tl = parent[x-1];
  76.                 tm = parent[x];
  77.                 tr = parent[x+1];
  78.             }
  79.  
  80.             // CHANGED LAYOUT FOR EASIER READING
  81.                    if(tl == 1 && tm == 1 && tr == 1){child[x] = rule[0];
  82.             } else if(tl == 1 && tm == 1 && tr == 0){child[x] = rule[1];
  83.             } else if(tl == 1 && tm == 0 && tr == 1){child[x] = rule[2];
  84.             } else if(tl == 1 && tm == 0 && tr == 0){child[x] = rule[3];
  85.             } else if(tl == 0 && tm == 1 && tr == 1){child[x] = rule[4];
  86.             } else if(tl == 0 && tm == 1 && tr == 0){child[x] = rule[5];
  87.             } else if(tl == 0 && tm == 0 && tr == 1){child[x] = rule[6];
  88.             } else if(tl == 0 && tm == 0 && tr == 0){child[x] = rule[7];}
  89.         }
  90.  
  91.         for(int i = 0; i < numParentValues; i ++)
  92.             parent[i] = child[i];
  93.  
  94.         printf("\n");
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement