yeeeeeeeeeeeee

e

Aug 6th, 2026
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. -- Lua Language for ComputerCraft
  2. -- Conway's Game of Life (Tweaked Version)
  3.  
  4. -- CONFIGURATION
  5. local width, height = 20, 10        -- Size of the grid
  6. local survival = {2,3}              -- Cells survive with this many neighbors
  7. local birth = {3}                   -- Dead cells become alive with this many neighbors
  8. local delay = 0.5                   -- Delay between generations
  9.  
  10. -- Initialize empty grid
  11. local grid = {}
  12. for y=1,height do
  13.     grid[y] = {}
  14.     for x=1,width do
  15.         grid[y][x] = math.random(0,1) -- Randomly start alive or dead
  16.     end
  17. end
  18.  
  19. -- Count alive neighbors of a cell
  20. local function countNeighbors(x, y)
  21.     local count = 0
  22.     for dy=-1,1 do
  23.         for dx=-1,1 do
  24.             if not (dx == 0 and dy == 0) then
  25.                 local nx, ny = x+dx, y+dy
  26.                 if nx >= 1 and nx <= width and ny >=1 and ny <= height then
  27.                     count = count + grid[ny][nx]
  28.                 end
  29.             end
  30.         end
  31.     end
  32.     return count
  33. end
  34.  
  35. -- Compute next generation
  36. local function nextGeneration()
  37.     local newGrid = {}
  38.     for y=1,height do
  39.         newGrid[y] = {}
  40.         for x=1,width do
  41.             local alive = grid[y][x]
  42.             local neighbors = countNeighbors(x, y)
  43.             if alive == 1 then
  44.                 newGrid[y][x] = 0
  45.                 for _, v in ipairs(survival) do
  46.                     if neighbors == v then
  47.                         newGrid[y][x] = 1
  48.                         break
  49.                     end
  50.                 end
  51.             else
  52.                 newGrid[y][x] = 0
  53.                 for _, v in ipairs(birth) do
  54.                     if neighbors == v then
  55.                         newGrid[y][x] = 1
  56.                         break
  57.                     end
  58.                 end
  59.             end
  60.         end
  61.     end
  62.     grid = newGrid
  63. end
  64.  
  65. -- Render the grid on terminal
  66. local function render()
  67.     term.clear()
  68.     term.setCursorPos(1,1)
  69.     for y=1,height do
  70.         for x=1,width do
  71.             if grid[y][x] == 1 then
  72.                 term.write("█")
  73.             else
  74.                 term.write(" ")
  75.             end
  76.         end
  77.         term.write("
  78. ")
  79.     end
  80. end
  81.  
  82. -- Main loop
  83. while true do
  84.     render()
  85.     nextGeneration()
  86.     sleep(delay)
  87. end
  88. Features of This Tweaked Version
Advertisement
Add Comment
Please, Sign In to add comment