Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Game-of-Life style simulation with random change + fade, for ComputerCraft Tweaked
- math.randomseed(os.time())
- -- Settings
- local termW, termH = term.getSize()
- local width, height = termW, termH -- use full terminal size
- local aliveChar = "O"
- local deadChar = "o"
- local steps = 8 -- number of fade steps (0=dead → full alive)
- local fadeSpeed = 0.1 -- how quickly intensity moves toward target
- local randomFlipChance = 0.02 -- baseline random flip each tick
- -- grid holds intensities 0..1
- local grid = {}
- for y = 1, height do
- grid[y] = {}
- for x = 1, width do
- grid[y][x] = math.random() -- random starting intensity
- end
- end
- -- count alive neighbours (intensity > 0.5 counts)
- local function countNeighbours(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
- -- map intensity to a symbol
- local function intensityToChar(intensity)
- local step = math.floor(intensity * steps)
- if step < 1 then
- return deadChar
- else
- return aliveChar
- end
- end
- -- update grid
- local function updateGrid()
- local newGrid = {}
- for y = 1, height do
- newGrid[y] = {}
- for x = 1, width do
- local neighbours = countNeighbours(x, y)
- local current = grid[y][x]
- -- neighbour influence: more neighbours → target alive
- local target
- local neighborInfluence = neighbours / 8
- if math.random() < neighborInfluence then
- target = 1
- elseif math.random() < randomFlipChance then
- target = 1 - current
- else
- target = 0
- end
- -- fade 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
- -- 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 .. intensityToChar(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