Frekvens1

[ComputerCraft] Conway's Game of Life Simulator

Feb 19th, 2020
1,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. --[[
  2. Program created by Frekvens1
  3.  
  4. 1. Any live cell with two or three neighbors survives.
  5. 2. Any dead cell with three live neighbors becomes a live cell.
  6. 3. All other live cells die in the next generation. Similarly, all other dead cells stay dead.
  7.  
  8. https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
  9.  
  10. pastebin get si4sDXrY GameOfLife.lua
  11. --]]
  12. local mon = peripheral.find("monitor")
  13. mon.setTextScale(0.5)
  14.  
  15. local grid1 = {
  16.     -- Medium exploder (Variant)
  17.     [10] = {[10] = true,[11] = true},[11] = {[10] = true,[11] = true},[13] = {[10] = true,[11] = true,[12] = true},
  18.     -- Small exploder (Variant)
  19.     [50] = {[10] = true,[11] = true},[51] = {[10] = true,[11] = true},[53] = {[11] = true},[54] = {[11] = true},[55] = {[11] = true}
  20. }
  21.  
  22. local grid2 = {
  23.     -- Gosper Glider Gun
  24.     [10] = { [10] = true, [11] = true },[11] = { [10] = true, [11] = true },
  25.     [18] = { [11] = true, [12] = true },[19] = { [10] = true, [12] = true },[20] = { [10] = true, [11] = true },
  26.     [26] = { [12] = true, [13] = true, [14] = true },[27] = { [12] = true },[28] = { [13] = true },
  27.     [32] = { [10] = true, [9] = true },[33] = { [10] = true, [8] = true },[34] = { [8] = true, [9] = true, [20] = true, [21] = true },
  28.     [35] = { [20] = true, [22] = true },[36] = { [20] = true },[44] = { [8] = true, [9] = true },
  29.     [45] = { [8] = true, [9] = true, [15] = true, [16] = true, [17] = true },[46] = { [15] = true},[47] = { [16] = true},
  30. }
  31.  
  32. local grid = grid1 -- Change this to either grid1 / grid2 for examples
  33. local dead_cells = {}
  34. local new_cells = {}
  35.  
  36. function draw(redraw)
  37.     if redraw then
  38.         mon.setBackgroundColor(colors.black)
  39.         mon.clear()
  40.        
  41.         mon.setBackgroundColor(colors.white)
  42.         for x in pairs(grid) do
  43.             for y in pairs(grid[x]) do
  44.                 mon.setCursorPos(x*2, y)
  45.                 mon.write("  ") -- Draws all cells from scratch (Intensive)
  46.             end
  47.         end
  48.     else -- Fast render
  49.         mon.setBackgroundColor(colors.black)
  50.         for x in pairs(dead_cells) do
  51.             for y in pairs(dead_cells[x]) do
  52.                 mon.setCursorPos(x*2, y)
  53.                 mon.write("  ") -- Removes dead cells
  54.             end
  55.         end
  56.        
  57.         mon.setBackgroundColor(colors.white)
  58.         for x in pairs(new_cells) do
  59.             for y in pairs(new_cells[x]) do
  60.                 mon.setCursorPos(x*2, y)
  61.                 mon.write("  ") -- Adds new cells
  62.             end
  63.         end
  64.     end
  65.    
  66.     mon.setBackgroundColor(colors.black)
  67. end
  68.  
  69.  
  70. function nextGeneration() -- Moves the simulation one more step
  71.     dead_cells = {}
  72.     new_cells = {}
  73.     local x_axis_pending_delete = {}
  74.    
  75.     for x in pairs(grid) do
  76.         local y_elements = 0
  77.         for y in pairs(grid[x]) do
  78.        
  79.             y_elements = y_elements + 1
  80.             -- Calculate if alive cell should stay alive
  81.             alive_cells = calculateCell(x, y)
  82.             if not(alive_cells == 2 or alive_cells == 3) then
  83.                 if dead_cells[x] == nil then dead_cells[x] = {} end
  84.                 if dead_cells[x][y] == nil then dead_cells[x][y] = true end
  85.             end
  86.  
  87.             -- Look for nearby cells to be reborn
  88.             local cells = {
  89.                 { x+1, y },
  90.                 { x-1, y },
  91.                 { x, y+1 },
  92.                 { x, y-1 },
  93.  
  94.                 { x+1, y+1 },
  95.                 { x+1, y-1 },
  96.                 { x-1, y+1 },
  97.                 { x-1, y-1 }
  98.             }
  99.  
  100.             for _, cell in pairs(cells) do
  101.                 if not(cellAlive(cell[1], cell[2])) then
  102.                     if (calculateCell(cell[1], cell[2]) == 3) then
  103.                         if (new_cells[cell[1]] == nil) then new_cells[cell[1]] = {} end
  104.                         if (new_cells[cell[1]][cell[2]] == nil) then new_cells[cell[1]][cell[2]] = true end
  105.                     end
  106.                 end
  107.             end
  108.         end
  109.        
  110.         if y_elements == 0 then -- X table is empty, why keep it?
  111.             table.insert(x_axis_pending_delete, x)
  112.         end
  113.     end
  114.        
  115.     for x in pairs(dead_cells) do
  116.         for y in pairs(dead_cells[x]) do
  117.             grid[x][y] = nil -- Remove the dead cells from the grid
  118.         end
  119.     end
  120.    
  121.     for _, x in pairs(x_axis_pending_delete) do
  122.         grid[x] = nil
  123.     end
  124.  
  125.     for x in pairs(new_cells) do
  126.         for y in pairs(new_cells[x]) do
  127.             if (grid[x] == nil) then grid[x] = {} end
  128.             if (grid[x][y] == nil) then grid[x][y] = true end -- Add the new cells to the grid
  129.         end
  130.     end
  131. end
  132.  
  133.  
  134. function calculateCell(x, y) -- Returns the number of living cells around a cell
  135.     local cells_alive = 0
  136.  
  137.     local cells = {
  138.         { x+1, y },
  139.         { x-1, y },
  140.         { x, y+1 },
  141.         { x, y-1 },
  142.  
  143.         { x+1, y+1 },
  144.         { x+1, y-1 },
  145.         { x-1, y+1 },
  146.         { x-1, y-1 }
  147.     }
  148.  
  149.     for _, cell in pairs(cells) do
  150.         if (cellAlive(cell[1], cell[2])) then
  151.             cells_alive = cells_alive + 1
  152.         end
  153.     end
  154.    
  155.     return cells_alive
  156.  
  157. end
  158.  
  159.  
  160. function cellAlive(x, y) -- Checks if the cell exists within the grid
  161.     if grid[x] == nil then return false end
  162.     if grid[x][y] == nil then return false end
  163.     return true
  164. end
  165.  
  166.  
  167. local function yield() -- Replace 'os.sleep(0)' with this for super speed
  168.     os.queueEvent( "sleep" )
  169.     coroutine.yield( "sleep" )
  170. end
  171.  
  172. draw(true) -- Draw whole grid
  173. while true do
  174.     os.sleep(0)
  175.     nextGeneration()
  176.     draw(false) -- Fast rendering
  177. end
Add Comment
Please, Sign In to add comment