Advertisement
yeeeeeeeeeeeee

e

May 5th, 2025
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. -- Attach to monitor on the right
  2. local monitor = peripheral.wrap("right")
  3. if not monitor then
  4.     error("Monitor not found on the right side.")
  5. end
  6.  
  7. -- Setup monitor
  8. monitor.setTextScale(0.5) -- Smaller text = more space
  9. term.redirect(monitor)
  10. monitor.clear()
  11.  
  12. -- Configuration
  13. local width, height = monitor.getSize()
  14. local delay = 0.2
  15. local alive = "O"
  16. local dead = " "
  17.  
  18. -- Create grid
  19. local function createGrid()
  20.     local grid = {}
  21.     for y = 1, height do
  22.         grid[y] = {}
  23.         for x = 1, width do
  24.             grid[y][x] = math.random() < 0.2 and 1 or 0
  25.         end
  26.     end
  27.     return grid
  28. end
  29.  
  30. -- Count live neighbors
  31. local function countNeighbors(grid, x, y)
  32.     local count = 0
  33.     for dy = -1, 1 do
  34.         for dx = -1, 1 do
  35.             if not (dx == 0 and dy == 0) then
  36.                 local nx, ny = x + dx, y + dy
  37.                 if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
  38.                     count = count + grid[ny][nx]
  39.                 end
  40.             end
  41.         end
  42.     end
  43.     return count
  44. end
  45.  
  46. -- Update the grid
  47. local function updateGrid(grid)
  48.     local newGrid = {}
  49.     for y = 1, height do
  50.         newGrid[y] = {}
  51.         for x = 1, width do
  52.             local neighbors = countNeighbors(grid, x, y)
  53.             local cell = grid[y][x]
  54.             if cell == 1 then
  55.                 newGrid[y][x] = (neighbors == 2 or neighbors == 3) and 1 or 0
  56.             else
  57.                 newGrid[y][x] = (neighbors == 3) and 1 or 0
  58.             end
  59.         end
  60.     end
  61.     return newGrid
  62. end
  63.  
  64. -- Check if all cells are dead
  65. local function isAllDead(grid)
  66.     for y = 1, height do
  67.         for x = 1, width do
  68.             if grid[y][x] == 1 then
  69.                 return false
  70.             end
  71.         end
  72.     end
  73.     return true
  74. end
  75.  
  76. -- Draw the grid
  77. local function drawGrid(grid)
  78.     monitor.setCursorPos(1,1)
  79.     for y = 1, height do
  80.         for x = 1, width do
  81.             monitor.write(grid[y][x] == 1 and alive or dead)
  82.         end
  83.     end
  84. end
  85.  
  86. -- Main loop
  87. while true do
  88.     local grid = createGrid()
  89.     while true do
  90.         drawGrid(grid)
  91.         sleep(delay)
  92.         grid = updateGrid(grid)
  93.         if isAllDead(grid) then
  94.             monitor.clear()
  95.             monitor.setCursorPos(1, 1)
  96.             monitor.write("All cells died. Restarting...")
  97.             sleep(1)
  98.             monitor.clear()
  99.             break
  100.         end
  101.     end
  102. end
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement