Advertisement
Madmouse

Cellular automata :D

Nov 26th, 2015
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // THE SCOTCH-WARE LICENSE (Revision 0):
  3. // <aaronryool/gmail.com> wrote this file. As long as you retain this notice you
  4. // can do whatever you want with this stuff. If we meet some day, and you think
  5. // this stuff is worth it, you can buy me a shot of scotch in return
  6. ////////////////////////////////////////////////////////////////////////////////
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <signal.h>
  12.  
  13. typedef enum { false, true } bool;
  14.  
  15. #define BGBLACK  "\033[40m"
  16. #define BGRED    "\033[41m"
  17. #define BGWHITE  "\033[47m"
  18. #define RESET    "\033[0m"
  19.  
  20. #define CLEAR           "\033[2J"
  21. #define HIDE_CURSOR     "\e[?25l"
  22. #define SHOW_CURSOR     "\e[?25h"
  23.  
  24. #define CURSOR(x, y) printf("\033[%i;%iH\n", y, x)
  25.  
  26. void __attribute__((noreturn)) usage(char** argv)
  27. {
  28.     printf("%s <#frames>\n", argv[0]);
  29.     exit(1);
  30. }
  31.  
  32. void init(int argc, char** argv, char** envp)
  33. {
  34.     if(argc != 2)
  35.         usage(argv);
  36.     printf("%s", HIDE_CURSOR);
  37.     signal(SIGINT, exit);
  38. }
  39.  
  40. void fini()
  41. {
  42.     printf("%s", SHOW_CURSOR);
  43. }
  44.  
  45. __attribute__((section(".init_array"))) typeof(init)* __init = init;
  46. __attribute__((section(".fini_array"))) typeof(fini)* __fini = fini;
  47.  
  48. int grid_check_neighbors(bool* grid, int width, int height, int x, int y)
  49. {
  50. #define SIZE (width * height)
  51. #define STATE(x, y, limit_comp) (int) (((x) * width + (y)) limit_comp ? grid[(x) * width + (y)] : false)
  52.     return STATE(x-1, y-1,>=0) + STATE(x-1,y,>=0) + STATE(x-1,y+1,>=0) + STATE(x,y-1,>=0) +
  53.         STATE(x,y+1,<SIZE) + STATE(x+1,y-1,<SIZE) + STATE(x+1,y,<SIZE) + STATE(x+1,y+1,<SIZE);
  54. }
  55.  
  56.  
  57. void grid_step(bool* grid, int width, int height)
  58. {
  59. #define SIZE (width * height)
  60.     unsigned int i, n;
  61.     bool t[SIZE];
  62.     memcpy(t, grid, SIZE*sizeof(bool));
  63.     for(i = 0;i < SIZE;i++)
  64.     {
  65.         n = grid_check_neighbors(grid, width, height, i / width, i % width);
  66.         if(!grid[i] && n == 3)
  67.                 t[i] = true;
  68.         else if(n < 2 || n > 3)
  69.                 t[i] = false;
  70.     }
  71.     memcpy(grid, t, SIZE*sizeof(bool));
  72. }
  73.  
  74. int main(int argc, char** argv, char** envp)
  75. {
  76. #define SIZE (width * height)
  77.     const int width = 25,
  78.         height = 25;
  79.     int i, c;
  80.     unsigned int frames = atoi(argv[1]);
  81.    
  82.     if(! frames) usage(argv);
  83.    
  84.     bool grid[] = {
  85.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  86.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  87.         0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  88.         0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  89.         0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  90.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  91.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,
  92.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,
  93.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,
  94.         0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
  95.         0,0,0,0,1,0,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
  96.         0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,
  97.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
  98.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
  99.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,
  100.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  101.         0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  102.         0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
  103.         0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,
  104.         0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,
  105.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  106.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  107.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  108.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  109.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  110.     };
  111.    
  112.     printf("%s", CLEAR);
  113.     for(i = 0;i < frames;i++)
  114.     {
  115.         CURSOR(0, 0);
  116.         for(c = 0;c < SIZE;c++)
  117.         {
  118.             printf(grid[c] ? "  %s" : "  %s", grid[c] ? BGRED : c % 2 ? BGBLACK : BGWHITE);
  119.             if((c + 1) % width == 0)
  120.                 printf("%s\n", RESET);
  121.         }
  122.         grid_step(grid, width, height);
  123.         usleep(1000*100);
  124.     }
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement