CCCPaste

Discord Minesweeper v2

-- For use with NotSoBot
-- Updated version of https://pastebin.com/g2T2mHcE

math.randomseed(os.time())

local grid_width = tonumber("{arg:0}") or 10
local difficulty = (tonumber("{arg:1}") or 20) / 100

local grid, numbers = {}, {
        [0] = ":white_large_square:",
        ":one:", ":two:", ":three:",
        ":four:", ":five:", ":six:",
        ":seven:", ":eight:", ":bomb:",
}

local neighbour_offsets = {
        {-1, 0, 1, -1, 1, -1, 0, 1},
        {-1, -1, -1, 0, 0, 1, 1, 1},
}

for i = 0, grid_width^2 - 1 do
        if math.random() <= difficulty then
                local x = i % grid_width
                local y = math.floor(i / grid_width)

                grid[i] = 9

                for j = 1, 8 do
                        local x = x + neighbour_offsets[1][j]
                        local y = y + neighbour_offsets[2][j]

                        if x >= 0 and x < grid_width and y >= 0 and y < grid_width then
                                local k = y * grid_width + x
                                local count = grid[k] or 0

                                if count < 9 then
                                        grid[k] = count + 1
                                end
                        end
                end
        end
end

for i = 0, grid_width^2 - 1 do
        grid[i] = numbers[grid[i] or 0]

        if (i + 1) % grid_width == 0 then
                print("||" .. table.concat(grid, "||||", i - grid_width + 1, i) .. "||")
        end
end