Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Dynamic Game of Life with fade-in/fade-out
- math.randomseed(os.time())
- local width, height = 20, 10
- local fadeSpeed = 0.2 -- how fast cells fade
- local symbols = {"o", ".", "0", "O"} -- from dead to alive
- local maxRandomInfluence = 0.3
- -- Initialize grid with intensity 0..1
- local grid = {}
- for y = 1, height do
- grid[y] = {}
- for x = 1, width do
- grid[y][x] = math.random()
- end
- end
- -- Count alive neighbors (intensity > 0.5 counts as alive)
- local function countNeighbors(x, y)
- local count = 0
- for dy = -1,1 do
- for dx = -1,1 do
- if not (dx == 0 and dy == 0) then
- local nx, ny = x + dx, y + dy
- if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
- if grid[ny][nx] > 0.5 then
- count = count + 1
- end
- end
- end
- end
- end
- return count
- end
- -- Update grid with neighbor influence and random flips
- local function updateGrid()
- local newGrid = {}
- for y = 1, height do
- newGrid[y] = {}
- for x = 1, width do
- local neighbors = countNeighbors(x, y)
- local current = grid[y][x]
- local target
- -- Neighbor-influenced target intensity
- local neighborInfluence = neighbors / 8
- local randomChance = math.random()
- if randomChance < neighborInfluence then
- target = 1 -- move toward alive
- elseif randomChance < neighborInfluence + maxRandomInfluence then
- target = 1 - current -- small random flip
- else
- target = 0 -- move toward dead
- end
- -- Fade intensity toward target
- if current < target then
- current = math.min(current + fadeSpeed, target)
- elseif current > target then
- current = math.max(current - fadeSpeed, target)
- end
- newGrid[y][x] = current
- end
- end
- grid = newGrid
- end
- -- Map intensity to symbol
- local function intensityToSymbol(intensity)
- local index = math.floor(intensity * #symbols) + 1
- if index > #symbols then index = #symbols end
- return symbols[index]
- end
- -- Draw grid
- local function drawGrid()
- term.clear()
- term.setCursorPos(1,1)
- for y = 1, height do
- local line = ""
- for x = 1, width do
- line = line .. intensityToSymbol(grid[y][x])
- end
- print(line)
- end
- end
- -- Main loop
- while true do
- drawGrid()
- updateGrid()
- sleep(0.2)
- end
Advertisement
Add Comment
Please, Sign In to add comment