SneakySquid icon

Discord Minesweeper v1

SneakySquid | PRO | 02/27/26 07:05:15 PM UTC (Edited) | 0 ⭐ | 1252 👁️ | Never ⏰ | []
Lua |

1.87 KB

|

None

|

0 👍

/

0 👎

-- Made for the use with NotSoBot.
-- New version: https://pastebin.com/LmsFRA7y
 
local Grid, Counters = {}, {
    ":one:", ":two:",
    ":three:", ":four:",
    ":five:", ":six:",
    ":seven:", ":eight:",
}
 
local Rows = math.max(1, tonumber("{arg:0}") or 10)
local Columns = math.max(1, tonumber("{arg:1}") or 10)
local MineChance = math.max(0, math.min(tonumber("{arg:2}") or 20, 100)) * 0.01
 
function Grid:Create()
    if (Rows * Columns < 9) then
        return "Grid is too small. There needs to be at least 9 tiles."
    end
 
    for r = 1, Rows do
        local row = {}
 
        for c = 1, Columns do
            table.insert(row, ":white_large_square:")
        end
 
        table.insert(self, row)
    end
 
    self:ScatterMines()
    self:CountMines()
 
    return self:Format()
end
 
function Grid:ScatterMines()
    math.randomseed(os.time())
 
    local mines = 0
 
    for r, row in ipairs(self) do
        for c, square in ipairs(row) do
            if (math.random() <= MineChance) then
                row[c] = ":bomb:"
                mines = mines + 1
            end
        end
    end
 
    if (MineChance > 0 and mines == 0) then
        local r = self[math.random(Rows)]
        local c = math.random(Columns)
 
        r[c] = ":bomb:"
    end
end
 
function Grid:CountMines()
    for r, row in ipairs(self) do
        for c, s in ipairs(row) do
            if (s ~= ":bomb:") then
                local mines = 0
 
                for x = -1, 1 do
                    for y = -1, 1 do
                        local r = self[r + x]
 
                        if (r and r[c + y] == ":bomb:") then
                            mines = mines + 1
                        end
                    end
                end
 
                if (mines > 0) then
                    row[c] = Counters[mines]
                end
            end
        end
    end
end
 
function Grid:Format()
    local grid = {}
 
    for i, r in ipairs(self) do
        r = table.concat(r, "\124\124\124\124")
        table.insert(grid, string.format("\124\124%s\124\124", r))
    end
 
    grid = table.concat(grid, "\n")
 
    return #grid > 2000 and "Grid is too large. 2,000 character limit met." or grid
end
 
print(Grid:Create())

Comments