Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- AC21009 Cellular Automaton
- Nathan Mcnally & Joseph Wyatt
- 150010438 150012345
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include "main.h"
- // Set the value of the rule you would like displayed check out - - - - > http://mathworld.wolfram.com/ElementaryCellularAutomaton.html
- int dRULE = 182;
- int parent[100] = {0};
- int child [100] = {0};
- int rule[8] = {0};
- int generations = 50;
- char tile_filled = 'X';
- char tile_empty = ' ';
- int numParentValues = sizeof(parent) / sizeof(int); int numChildValues = sizeof(child) / sizeof(int);
- int main()
- {
- draw1D_ca();
- return 0;
- }
- // Takes in a decimal number converts to binary and splits into an array
- void setRule (int n){
- int counter, k, pos;
- int result[8] = {0};
- pos = 0;
- for (counter = 7; counter >= 0; counter--)
- {
- k = n >> counter;
- if (k & 1){
- rule[pos] = 1;
- pos ++;
- }else{
- rule[pos] = 0;
- pos ++;
- }
- }
- printf("\n");
- }
- void draw1D_ca (){
- parent[50] = 1;
- setRule(dRULE);
- int tl, tm, tr = 0;
- for(int y=0; y < generations; y++){
- for(int i = 0; i < numParentValues; i ++){
- // Instead of printing 1's and 0's prints either 'x' or '_' as it is easier to spot patterns
- if(parent[i] == 0){printf("%c", tile_empty);}else{printf("%c", tile_filled);}
- }
- for (int x=0; x<numParentValues; x++){
- if(x == 0){
- tl = 0;
- tm = parent[x];
- tr = parent[x+1];
- }else if (x == 30){
- tl = parent[x-1];
- tm = parent[x];
- tr = 0;
- }else{
- tl = parent[x-1];
- tm = parent[x];
- tr = parent[x+1];
- }
- // CHANGED LAYOUT FOR EASIER READING
- if(tl == 1 && tm == 1 && tr == 1){child[x] = rule[0];
- } else if(tl == 1 && tm == 1 && tr == 0){child[x] = rule[1];
- } else if(tl == 1 && tm == 0 && tr == 1){child[x] = rule[2];
- } else if(tl == 1 && tm == 0 && tr == 0){child[x] = rule[3];
- } else if(tl == 0 && tm == 1 && tr == 1){child[x] = rule[4];
- } else if(tl == 0 && tm == 1 && tr == 0){child[x] = rule[5];
- } else if(tl == 0 && tm == 0 && tr == 1){child[x] = rule[6];
- } else if(tl == 0 && tm == 0 && tr == 0){child[x] = rule[7];}
- }
- for(int i = 0; i < numParentValues; i ++)
- parent[i] = child[i];
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement