SneakySquid

Discord Minesweeper v1

Feb 11th, 2019 (edited)
1,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. -- Made for the use with NotSoBot.
  2. -- New version: https://pastebin.com/LmsFRA7y
  3.  
  4. local Grid, Counters = {}, {
  5.     ":one:", ":two:",
  6.     ":three:", ":four:",
  7.     ":five:", ":six:",
  8.     ":seven:", ":eight:",
  9. }
  10.  
  11. local Rows = math.max(1, tonumber("{arg:0}") or 10)
  12. local Columns = math.max(1, tonumber("{arg:1}") or 10)
  13. local MineChance = math.max(0, math.min(tonumber("{arg:2}") or 20, 100)) * 0.01
  14.  
  15. function Grid:Create()
  16.     if (Rows * Columns < 9) then
  17.         return "Grid is too small. There needs to be at least 9 tiles."
  18.     end
  19.  
  20.     for r = 1, Rows do
  21.         local row = {}
  22.  
  23.         for c = 1, Columns do
  24.             table.insert(row, ":white_large_square:")
  25.         end
  26.  
  27.         table.insert(self, row)
  28.     end
  29.  
  30.     self:ScatterMines()
  31.     self:CountMines()
  32.  
  33.     return self:Format()
  34. end
  35.  
  36. function Grid:ScatterMines()
  37.     math.randomseed(os.time())
  38.  
  39.     local mines = 0
  40.  
  41.     for r, row in ipairs(self) do
  42.         for c, square in ipairs(row) do
  43.             if (math.random() <= MineChance) then
  44.                 row[c] = ":bomb:"
  45.                 mines = mines + 1
  46.             end
  47.         end
  48.     end
  49.  
  50.     if (MineChance > 0 and mines == 0) then
  51.         local r = self[math.random(Rows)]
  52.         local c = math.random(Columns)
  53.  
  54.         r[c] = ":bomb:"
  55.     end
  56. end
  57.  
  58. function Grid:CountMines()
  59.     for r, row in ipairs(self) do
  60.         for c, s in ipairs(row) do
  61.             if (s ~= ":bomb:") then
  62.                 local mines = 0
  63.  
  64.                 for x = -1, 1 do
  65.                     for y = -1, 1 do
  66.                         local r = self[r + x]
  67.  
  68.                         if (r and r[c + y] == ":bomb:") then
  69.                             mines = mines + 1
  70.                         end
  71.                     end
  72.                 end
  73.  
  74.                 if (mines > 0) then
  75.                     row[c] = Counters[mines]
  76.                 end
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. function Grid:Format()
  83.     local grid = {}
  84.  
  85.     for i, r in ipairs(self) do
  86.         r = table.concat(r, "\124\124\124\124")
  87.         table.insert(grid, string.format("\124\124%s\124\124", r))
  88.     end
  89.  
  90.     grid = table.concat(grid, "\n")
  91.  
  92.     return #grid > 2000 and "Grid is too large. 2,000 character limit met." or grid
  93. end
  94.  
  95. print(Grid:Create())
Advertisement
Add Comment
Please, Sign In to add comment