Advertisement
Guest User

1D Cellular Automaton /r/programbattles

a guest
Nov 6th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3. #define LIVING '#'
  4. #define DEAD '_'
  5. #define TEMPDEAD 'R'
  6. #define TEMPLIVING 'T'
  7. #define BOUND '|'
  8. #define SIZE 22
  9. int mod (int a, int b)
  10. {
  11.    if(b < 0)
  12.      return mod(-a, -b);
  13.    int result = a % b;
  14.    if(result < 0)
  15.      result+=b;
  16.    return result;
  17. }
  18. char *setCell(char grid [], int index, char state)
  19. {
  20.     grid[mod(index,SIZE)]=state;
  21. }
  22. char getCell(char grid [], int index)
  23. {
  24.     return grid[mod(index,SIZE)];
  25. }
  26. char *initializeGrid(char grid [])
  27. {
  28.     for(int i = 0; i < SIZE; i++)
  29.     {
  30.         grid[i] = DEAD;
  31.     }
  32.     setCell(grid,2,LIVING);
  33.     setCell(grid,3,LIVING);
  34.     setCell(grid,4,LIVING);
  35.     setCell(grid,6,LIVING);
  36.     setCell(grid,7,LIVING);
  37.     setCell(grid,9,LIVING);
  38.     setCell(grid,11,LIVING);
  39.     setCell(grid,13,LIVING);
  40.     setCell(grid,15,LIVING);
  41.     setCell(grid,18,LIVING);
  42.     setCell(grid,0,BOUND);
  43.     setCell(grid,SIZE-1,BOUND);
  44. }
  45.  
  46. void endProgram()
  47. {
  48.     cout << "Press enter to continue..." << endl;
  49.     string input = "";
  50.     getline(cin,input);//keeps console open
  51. }
  52. bool checkifgameover(char grid [], char lastgrid [], int gen)
  53. {
  54.     if(gen>1)
  55.     {
  56.         for(int i = 0; i < SIZE; i++)
  57.         {
  58.             if(grid[i]!=lastgrid[i])
  59.             {
  60.                 return false;
  61.             }
  62.         }
  63.         return true;
  64.     }
  65. }
  66. char *updateGrid(char grid [])
  67. {
  68.     for(int i = 0; i < SIZE; i++)
  69.     {
  70.         if(getCell(grid,i)==LIVING)
  71.         {
  72.             if(getCell(grid,i+1) !=LIVING & getCell(grid,i+1) !=BOUND)
  73.             {
  74.                 if(getCell(grid,i+2) ==LIVING)
  75.                 {
  76.                     setCell(grid,i+1,TEMPLIVING);//WHERE DOES THE POLLEN GO?
  77.                 }
  78.             }
  79.             if((getCell(grid,i-1)!=LIVING & getCell(grid,i-1)!=TEMPDEAD ) & (getCell(grid,i+1)!=LIVING & getCell(grid,i-1)!=TEMPDEAD))
  80.             {
  81.                 setCell(grid,i,TEMPDEAD);//DEATH BY LONELINESS
  82.             }
  83.             if((getCell(grid,i-1)==LIVING | getCell(grid,i-1)==TEMPDEAD) & (getCell(grid,i+1)==LIVING | getCell(grid,i+1)==TEMPDEAD))
  84.             {
  85.                 setCell(grid,i,TEMPDEAD);//DEATH BY OVERPOPULATION
  86.             }
  87.         }
  88.     }
  89.     for(int i = 0; i < SIZE; i++)
  90.     {
  91.         if(getCell(grid,i)==TEMPLIVING)//So no logic is acted on Living cells until after next generation
  92.         {
  93.             setCell(grid,i,LIVING);
  94.         }
  95.         if(getCell(grid,i)==TEMPDEAD)
  96.         {
  97.             setCell(grid,i,DEAD);
  98.         }
  99.     }
  100. }
  101. int main()
  102. {
  103.     char output [SIZE];
  104.     char lastoutput [SIZE];
  105.     initializeGrid(output);
  106.     int generation = 0;
  107.     while(!checkifgameover(output,lastoutput,generation))
  108.     {
  109.         for(int i = 0; i < SIZE; i++)
  110.         {
  111.             lastoutput[i] = output[i];
  112.         }
  113.         generation++;
  114.         cout<< "Current Gen:" << generation;
  115.         for(int i = 0; i < SIZE; i++)
  116.         {
  117.             cout <<  output[i];
  118.         }
  119.         cout << endl;
  120.         updateGrid(output);
  121.     }
  122.     endProgram();
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement