yeeeeeeeeeeeee

e

May 5th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. --[[
  2. Program created by Frekvens1, modified by ChatGPT
  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. Original: pastebin get si4sDXrY GameOfLife.lua
  11. --]]
  12.  
  13. local mon = peripheral.find("monitor")
  14. mon.setTextScale(0.5)
  15.  
  16. -- Define initial example grids
  17. local grid1 = {
  18. -- Medium exploder (Variant)
  19. [10] = {[10] = true,[11] = true},[11] = {[10] = true,[11] = true},[13] = {[10] = true,[11] = true,[12] = true},
  20. -- Small exploder (Variant)
  21. [50] = {[10] = true,[11] = true},[51] = {[10] = true,[11] = true},[53] = {[11] = true},[54] = {[11] = true},[55] = {[11] = true}
  22. }
  23.  
  24. local grid2 = {
  25. -- Gosper Glider Gun
  26. [10] = { [10] = true, [11] = true },[11] = { [10] = true, [11] = true },
  27. [18] = { [11] = true, [12] = true },[19] = { [10] = true, [12] = true },[20] = { [10] = true, [11] = true },
  28. [26] = { [12] = true, [13] = true, [14] = true },[27] = { [12] = true },[28] = { [13] = true },
  29. [32] = { [10] = true, [9] = true },[33] = { [10] = true, [8] = true },[34] = { [8] = true, [9] = true, [20] = true, [21] = true },
  30. [35] = { [20] = true, [22] = true },[36] = { [20] = true },[44] = { [8] = true, [9] = true },
  31. [45] = { [8] = true, [9] = true, [15] = true, [16] = true, [17] = true },[46] = { [15] = true},[47] = { [16] = true},
  32. }
  33.  
  34. -- Merge two grids into one
  35. local function mergeGrids(a, b)
  36. local result = {}
  37. for x, col in pairs(a) do
  38. result[x] = result[x] or {}
  39. for y in pairs(col) do
  40. result[x][y] = true
  41. end
  42. end
  43. for x, col in pairs(b) do
  44. result[x] = result[x] or {}
  45. for y in pairs(col) do
  46. result[x][y] = true
  47. end
  48. end
  49. return result
  50. end
  51.  
  52. -- Initial grid is the combination of both examples
  53. local grid = mergeGrids(grid1, grid2)
  54. local dead_cells = {}
  55. local new_cells = {}
  56.  
  57. function draw(redraw)
  58. if redraw then
  59. mon.setBackgroundColor(colors.black)
  60. mon.clear()
  61.  
  62. mon.setBackgroundColor(colors.white)
  63. for x in pairs(grid) do
  64. for y in pairs(grid[x]) do
  65. mon.setCursorPos(x*2, y)
  66. mon.write(" ") -- Draws all cells from scratch
  67. end
  68. end
  69. else -- Fast render
  70. mon.setBackgroundColor(colors.black)
  71. for x in pairs(dead_cells) do
  72. for y in pairs(dead_cells[x]) do
  73. mon.setCursorPos(x*2, y)
  74. mon.write(" ") -- Erase dead cells
  75. end
  76. end
  77.  
  78. mon.setBackgroundColor(colors.white)
  79. for x in pairs(new_cells) do
  80. for y in pairs(new_cells[x]) do
  81. mon.setCursorPos(x*2, y)
  82. mon.write(" ") -- Draw new cells
  83. end
  84. end
  85. end
  86.  
  87. mon.setBackgroundColor(colors.black)
  88. end
  89.  
  90. function nextGeneration()
  91. dead_cells = {}
  92. new_cells = {}
  93. local x_axis_pending_delete = {}
  94.  
  95. for x in pairs(grid) do
  96. local y_elements = 0
  97. for y in pairs(grid[x]) do
  98. y_elements = y_elements + 1
  99.  
  100. -- Check if cell should die
  101. local alive_neighbors = calculateCell(x, y)
  102. if not (alive_neighbors == 2 or alive_neighbors == 3) then
  103. dead_cells[x] = dead_cells[x] or {}
  104. dead_cells[x][y] = true
  105. end
  106.  
  107. -- Check dead neighbors for birth
  108. local cells = {
  109. { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
  110. { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
  111. }
  112. for _, cell in ipairs(cells) do
  113. local cx, cy = cell[1], cell[2]
  114. if not cellAlive(cx, cy) and calculateCell(cx, cy) == 3 then
  115. new_cells[cx] = new_cells[cx] or {}
  116. new_cells[cx][cy] = true
  117. end
  118. end
  119. end
  120.  
  121. if y_elements == 0 then
  122. table.insert(x_axis_pending_delete, x)
  123. end
  124. end
  125.  
  126. for x in pairs(dead_cells) do
  127. for y in pairs(dead_cells[x]) do
  128. grid[x][y] = nil
  129. end
  130. end
  131.  
  132. for _, x in ipairs(x_axis_pending_delete) do
  133. grid[x] = nil
  134. end
  135.  
  136. for x in pairs(new_cells) do
  137. for y in pairs(new_cells[x]) do
  138. grid[x] = grid[x] or {}
  139. grid[x][y] = true
  140. end
  141. end
  142. end
  143.  
  144. function calculateCell(x, y)
  145. local count = 0
  146. local neighbors = {
  147. { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
  148. { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
  149. }
  150. for _, cell in ipairs(neighbors) do
  151. if cellAlive(cell[1], cell[2]) then
  152. count = count + 1
  153. end
  154. end
  155. return count
  156. end
  157.  
  158. function cellAlive(x, y)
  159. return grid[x] and grid[x][y]
  160. end
  161.  
  162. function isGridEmpty()
  163. for x in pairs(grid) do
  164. for _ in pairs(grid[x]) do
  165. return false
  166. end
  167. end
  168. return true
  169. end
  170.  
  171. function resetGrid()
  172. grid = mergeGrids(grid1, grid2)
  173. draw(true)
  174. end
  175.  
  176. draw(true)
  177. while true do
  178. os.sleep(0)
  179. nextGeneration()
  180. draw(false)
  181.  
  182. if isGridEmpty() then
  183. os.sleep(1)
  184. resetGrid()
  185. end
  186. end
  187.  
Advertisement
Add Comment
Please, Sign In to add comment