title: Plotting Automata Grid Iterations in R
body: <p>I am an experienced R programmer, but beginner with programming automata scripts (in fact I just learned about them recently). I've produced a script that calculates (based on some minimal rules from the wikipedia page on Conway's game of life) the grid-like position for any given starting position. I am fairly confident that it's working at least as far as the data, but I can't figure out the best way to kind of animate or make these plots change in the viewer- in fact I have no idea what the best way to visualize a sort of NxM grid in this way.</p>
  <p><a href="https://github.com/snakewizardd/automata/blob/main/scripts/calc.R" rel="nofollow noreferrer">Script in Repo</a></p>
  <pre><code>library(dplyr)
  
  grid &lt;- matrix(0,nrow=8,ncol=8) %&gt;% as.data.frame()
  positions &lt;- c(1,2,3,4,5,6,7,8)
  
  colnames(grid) &lt;- positions
  
  input_row &lt;- 4
  input_col &lt;- 4
  
  grid[input_row-1,input_col] &lt;- 1
  grid[input_row,input_col+1] &lt;- 1
  grid[input_row+1,input_col+1] &lt;- 1
  grid[input_row+1,input_col] &lt;- 1
  grid[input_row+1,input_col-1] &lt;- 1
  
  
  capture_index &lt;- NULL
  
  for(cycle in 1:5){
  
  for(row in 1:nrow(grid)){
    
    for(col in 1:ncol(grid)){
      
      input_row &lt;- row
      input_col &lt;- col 
      
      neighbor_1 &lt;- grid[input_row-1,input_col-1] 
      neighbor_2 &lt;- grid[input_row-1,input_col] 
      neighbor_3 &lt;- grid[input_row-1,input_col + 1] 
      
      neighbor_4 &lt;- grid[input_row,input_col-1] 
      neighbor_5 &lt;- grid[input_row,input_col+1] 
      
      neighbor_6 &lt;- grid[input_row+1,input_col-1] 
      neighbor_7 &lt;- grid[input_row+1,input_col] 
      neighbor_8 &lt;- grid[input_row+1,input_col + 1] 
      
      
      neighbor_sum &lt;- sum(neighbor_1,neighbor_2,neighbor_3,
                          neighbor_4,neighbor_5,neighbor_6,
                          neighbor_7,neighbor_8)
      
      capture_index &lt;- rbind(capture_index,data.frame(row,col,
                                                      current_state = grid[row,col],
                                                      neighbor_sum))
      
    }
    
  }
  
  capture_index[is.na(capture_index)] &lt;- 0
  
  process &lt;- capture_index %&gt;% mutate(is_alive = current_state == 1) %&gt;%
    mutate(living_survive = ifelse(is_alive == TRUE &amp; neighbor_sum %in% c(2,3),
                                   'STAY ALIVE','NOTHING')) %&gt;%
    mutate(dead_born = ifelse(is_alive == FALSE &amp; neighbor_sum ==3,
           'BE BORN','NOTHING')) 
  
  process$neighbor_sum[is.na(process$neighbor_sum)] &lt;- 0
  process$dead_born[is.na(process$dead_born)] &lt;- 'NOTHING'
  
  #Next Steps
  process &lt;- process %&gt;% mutate(next_state = 
                  ifelse(is_alive == FALSE &amp; dead_born ==  'BE BORN',
                         1,
                  ifelse(is_alive == TRUE &amp; living_survive == 'STAY ALIVE',
                         1,0)))
          
  for(i in 1:nrow(process)){
    
    grid[process[i,'row'],process[i,'col']] &lt;- process[i,'next_state']
    
  }        
  
  }
  </code></pre>
  <p>For whatever # of cycles you choose, the final result will be a 'grid' object that reflects the new positioning based on rules. The current setting just iterates until they all reach some area in the bottom-right and collapse to a square- but of course this all changes with your initial set up and iteration strategy.</p>
  <p>It would be nice making this algorithm somewhat visual.</p>
  <p>Thanks in advance! This is my first time submitting a question or answer on stackoverflow- I hope it was okay and any feedback is appreciated!</p>