Advertisement
Mangus875

Lua Minesweeper

Aug 30th, 2023 (edited)
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. math.randomseed(os.time())
  2. local realBoard = {}
  3. local myBoard = {}
  4.  
  5. -- settings:
  6. width = 9       -- board width
  7. height = 9      -- board height
  8. mines = 10      -- mine count
  9. deterministic = false   -- avoid 50/50 guess cases
  10. openFirstMove = false   -- ensure first move lands on a tile with 0 surrounding mines
  11.  
  12. -- beginner : 9x9/10
  13. -- intermediate : 16x16/40
  14. -- expert   : 30x16/99
  15.  
  16. local function shuffle(table)
  17.     local buffer
  18.     for i = #table, 1, -1 do
  19.         local r = math.random(1,i)
  20.         buffer = table[i]
  21.         table[i] = table[r]
  22.         table[r] = buffer
  23.     end
  24. end
  25.  
  26. local function isValid(x, y)
  27.     return x>0 and x<=width and y>0 and y<=height
  28. end
  29.  
  30. local function getNeighbors(i)
  31.     local neighbors = {}
  32.     local x = (i-1)%width + 1
  33.     local y = math.floor((i-1)/width)+1
  34.    
  35.     for ix = x-1, x+1 do
  36.         for iy = y-1, y+1 do
  37.             if isValid(ix, iy) then
  38.                 table.insert(neighbors, ix+(iy-1)*width)
  39.             end
  40.         end
  41.     end
  42.    
  43.     return neighbors
  44. end
  45.  
  46. local function labelTiles()
  47.     for i, bomb in ipairs(realBoard) do
  48.         if bomb then
  49.             for _, n in ipairs(getNeighbors(i)) do
  50.                 if not realBoard[n] then
  51.                     myBoard[n] = myBoard[n]+1
  52.                 else
  53.                     myBoard[n] = "■"
  54.                 end
  55.             end
  56.         end
  57.     end
  58. end
  59.  
  60. local function printBoard()
  61.     for y = 1, height do
  62.         local row = ""
  63.         for x = 1, width do
  64.             local i = x+(y-1)*width
  65.             local v = myBoard[i]
  66.             if v ~= 0 then
  67.                 row = row..myBoard[i].." "
  68.             else
  69.                 row = row.."  "
  70.             end
  71.         end
  72.         print(row)
  73.     end
  74. end
  75.  
  76. local function clamp(n, min, max)
  77.     if n < min then return min end
  78.     if n > max then return max end
  79.     return n
  80. end
  81.  
  82. local function setup()
  83.     realBoard = {}
  84.     myBoard = {}
  85.    
  86.     mines = clamp(mines, 1, width*height-1)
  87.     for i = 1, width*height do
  88.         myBoard[i] = 0
  89.         realBoard[i] = false
  90.         if i <= mines then
  91.             realBoard[i] = true
  92.         end
  93.     end
  94.    
  95.     shuffle(realBoard)
  96.     labelTiles()
  97. end
  98.  
  99. setup()
  100. printBoard()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement