Advertisement
yeeeeeeeeeeeee

eeeeeeeeeeeeeee

May 5th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. mon.setTextScale(0.5)
  3.  
  4. -- Combine grids 1 and 2
  5. local grid1 = {
  6. [10] = {[10] = true,[11] = true},[11] = {[10] = true,[11] = true},[13] = {[10] = true,[11] = true,[12] = true},
  7. [50] = {[10] = true,[11] = true},[51] = {[10] = true,[11] = true},[53] = {[11] = true},[54] = {[11] = true},[55] = {[11] = true}
  8. }
  9.  
  10. local grid2 = {
  11. [10] = { [10] = true, [11] = true },[11] = { [10] = true, [11] = true },
  12. [18] = { [11] = true, [12] = true },[19] = { [10] = true, [12] = true },[20] = { [10] = true, [11] = true },
  13. [26] = { [12] = true, [13] = true, [14] = true },[27] = { [12] = true },[28] = { [13] = true },
  14. [32] = { [10] = true, [9] = true },[33] = { [10] = true, [8] = true },[34] = { [8] = true, [9] = true, [20] = true, [21] = true },
  15. [35] = { [20] = true, [22] = true },[36] = { [20] = true },[44] = { [8] = true, [9] = true },
  16. [45] = { [8] = true, [9] = true, [15] = true, [16] = true, [17] = true },[46] = { [15] = true},[47] = { [16] = true},
  17. }
  18.  
  19. local function mergeGrids(a, b)
  20. local result = {}
  21. for x, col in pairs(a) do
  22. result[x] = result[x] or {}
  23. for y in pairs(col) do
  24. result[x][y] = true
  25. end
  26. end
  27. for x, col in pairs(b) do
  28. result[x] = result[x] or {}
  29. for y in pairs(col) do
  30. result[x][y] = true
  31. end
  32. end
  33. return result
  34. end
  35.  
  36. local initialGrid = mergeGrids(grid1, grid2)
  37. local grid = {}
  38. local dead_cells = {}
  39. local new_cells = {}
  40.  
  41. function draw(redraw)
  42. if redraw then
  43. mon.setBackgroundColor(colors.black)
  44. mon.clear()
  45. for x in pairs(grid) do
  46. for y in pairs(grid[x]) do
  47. mon.setCursorPos(x, y)
  48. mon.setBackgroundColor(colors.white)
  49. mon.write(" ")
  50. end
  51. end
  52. else
  53. for x in pairs(dead_cells) do
  54. for y in pairs(dead_cells[x]) do
  55. mon.setCursorPos(x, y)
  56. mon.setBackgroundColor(colors.black)
  57. mon.write(" ")
  58. end
  59. end
  60. for x in pairs(new_cells) do
  61. for y in pairs(new_cells[x]) do
  62. mon.setCursorPos(x, y)
  63. mon.setBackgroundColor(colors.white)
  64. mon.write(" ")
  65. end
  66. end
  67. end
  68. mon.setBackgroundColor(colors.black)
  69. end
  70.  
  71. function nextGeneration()
  72. dead_cells = {}
  73. new_cells = {}
  74. local toDelete = {}
  75.  
  76. for x in pairs(grid) do
  77. local y_elements = 0
  78. for y in pairs(grid[x]) do
  79. y_elements = y_elements + 1
  80.  
  81. local alive_neighbors = calculateCell(x, y)
  82. if not (alive_neighbors == 2 or alive_neighbors == 3) then
  83. dead_cells[x] = dead_cells[x] or {}
  84. dead_cells[x][y] = true
  85. end
  86.  
  87. local neighbors = {
  88. { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
  89. { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
  90. }
  91. for _, cell in ipairs(neighbors) do
  92. local cx, cy = cell[1], cell[2]
  93. if not cellAlive(cx, cy) and calculateCell(cx, cy) == 3 then
  94. new_cells[cx] = new_cells[cx] or {}
  95. new_cells[cx][cy] = true
  96. end
  97. end
  98. end
  99. if y_elements == 0 then
  100. table.insert(toDelete, x)
  101. end
  102. end
  103.  
  104. for x in pairs(dead_cells) do
  105. for y in pairs(dead_cells[x]) do
  106. grid[x][y] = nil
  107. end
  108. end
  109.  
  110. for _, x in ipairs(toDelete) do
  111. if grid[x] and next(grid[x]) == nil then
  112. grid[x] = nil
  113. end
  114. end
  115.  
  116. for x in pairs(new_cells) do
  117. grid[x] = grid[x] or {}
  118. for y in pairs(new_cells[x]) do
  119. grid[x][y] = true
  120. end
  121. end
  122. end
  123.  
  124. function calculateCell(x, y)
  125. local count = 0
  126. local neighbors = {
  127. { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
  128. { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
  129. }
  130. for _, cell in ipairs(neighbors) do
  131. if cellAlive(cell[1], cell[2]) then
  132. count = count + 1
  133. end
  134. end
  135. return count
  136. end
  137.  
  138. function cellAlive(x, y)
  139. return grid[x] and grid[x][y]
  140. end
  141.  
  142. function isGridEmpty()
  143. for x in pairs(grid) do
  144. if grid[x] and next(grid[x]) then
  145. return false
  146. end
  147. end
  148. return true
  149. end
  150.  
  151. function resetGrid()
  152. grid = mergeGrids(grid1, grid2)
  153. draw(true)
  154. end
  155.  
  156. -- Main loop
  157. resetGrid()
  158. while true do
  159. os.sleep(0)
  160. nextGeneration()
  161. draw(false)
  162.  
  163. if isGridEmpty() then
  164. os.sleep(1)
  165. resetGrid()
  166. end
  167. end
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement