Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mon = peripheral.find("monitor")
- mon.setTextScale(0.5)
- -- Combine grids
- local grid1 = {
- [10] = {[10] = true,[11] = true},[11] = {[10] = true,[11] = true},[13] = {[10] = true,[11] = true,[12] = true},
- [50] = {[10] = true,[11] = true},[51] = {[10] = true,[11] = true},[53] = {[11] = true},[54] = {[11] = true},[55] = {[11] = true}
- }
- local grid2 = {
- [10] = { [10] = true, [11] = true },[11] = { [10] = true, [11] = true },
- [18] = { [11] = true, [12] = true },[19] = { [10] = true, [12] = true },[20] = { [10] = true, [11] = true },
- [26] = { [12] = true, [13] = true, [14] = true },[27] = { [12] = true },[28] = { [13] = true },
- [32] = { [10] = true, [9] = true },[33] = { [10] = true, [8] = true },[34] = { [8] = true, [9] = true, [20] = true, [21] = true },
- [35] = { [20] = true, [22] = true },[36] = { [20] = true },[44] = { [8] = true, [9] = true },
- [45] = { [8] = true, [9] = true, [15] = true, [16] = true, [17] = true },[46] = { [15] = true},[47] = { [16] = true},
- }
- local function mergeGrids(a, b)
- local result = {}
- for x, col in pairs(a) do
- result[x] = result[x] or {}
- for y in pairs(col) do result[x][y] = true end
- end
- for x, col in pairs(b) do
- result[x] = result[x] or {}
- for y in pairs(col) do result[x][y] = true end
- end
- return result
- end
- local initialGrid = mergeGrids(grid1, grid2)
- local grid = {}
- local dead_cells = {}
- local new_cells = {}
- local function draw(redraw)
- if redraw then
- mon.setBackgroundColor(colors.black)
- mon.clear()
- for x in pairs(grid) do
- for y in pairs(grid[x]) do
- mon.setCursorPos(x, y)
- mon.setBackgroundColor(colors.white)
- mon.write(" ")
- end
- end
- else
- for x in pairs(dead_cells) do
- for y in pairs(dead_cells[x]) do
- mon.setCursorPos(x, y)
- mon.setBackgroundColor(colors.black)
- mon.write(" ")
- end
- end
- for x in pairs(new_cells) do
- for y in pairs(new_cells[x]) do
- mon.setCursorPos(x, y)
- mon.setBackgroundColor(colors.white)
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- local function nextGeneration()
- dead_cells = {}
- new_cells = {}
- local toDelete = {}
- for x in pairs(grid) do
- local y_elements = 0
- for y in pairs(grid[x]) do
- y_elements = y_elements + 1
- local alive_neighbors = calculateCell(x, y)
- if not (alive_neighbors == 2 or alive_neighbors == 3) then
- dead_cells[x] = dead_cells[x] or {}
- dead_cells[x][y] = true
- end
- local neighbors = {
- { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
- { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
- }
- for _, cell in ipairs(neighbors) do
- local cx, cy = cell[1], cell[2]
- if not cellAlive(cx, cy) and calculateCell(cx, cy) == 3 then
- new_cells[cx] = new_cells[cx] or {}
- new_cells[cx][cy] = true
- end
- end
- end
- if y_elements == 0 then table.insert(toDelete, x) end
- end
- for x in pairs(dead_cells) do
- for y in pairs(dead_cells[x]) do
- grid[x][y] = nil
- end
- end
- for _, x in ipairs(toDelete) do
- if grid[x] and next(grid[x]) == nil then grid[x] = nil end
- end
- for x in pairs(new_cells) do
- grid[x] = grid[x] or {}
- for y in pairs(new_cells[x]) do
- grid[x][y] = true
- end
- end
- end
- function calculateCell(x, y)
- local count = 0
- local neighbors = {
- { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
- { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
- }
- for _, cell in ipairs(neighbors) do
- if cellAlive(cell[1], cell[2]) then
- count = count + 1
- end
- end
- return count
- end
- function cellAlive(x, y)
- return grid[x] and grid[x][y]
- end
- function resetGrid()
- grid = mergeGrids(grid1, grid2)
- draw(true)
- end
- -- Main loop with 30-second reset timer
- local function run()
- while true do
- resetGrid()
- local start = os.epoch("utc")
- while os.epoch("utc") - start < 30000 do
- os.sleep(0)
- nextGeneration()
- draw(false)
- end
- end
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement