Advertisement
Mangus875

Theoretical Minesweeper Solver approach: "Grouped Flags"

Sep 11th, 2023 (edited)
232
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. --[[ Procedure:
  2. for each revealed cell:
  3.   if neighbors.Unknown == cellValue:
  4.     neighbors:FlagAll()
  5.  
  6.   elseif #neighbors.Flagged == cellValue:
  7.     open all non-flagged neighbors
  8.  
  9.   else:
  10.     add each non-flagged neighbor to a groupped flag:
  11.       a groupped flag is multiple cells grouped together that function as 1 to groupSize-1 flagged cells
  12.       (logic for grouped flags functions the same as normal flags. i.e, i group is treated as one cell)
  13.       for example, 3 cells grouped together with a flag value of 2
  14.         this means that between the 3 cells there are 2 mines/regular flags but which 2 specifically is currently unknown
  15.  
  16.   repeat this logic until the board state does not change
  17. --]]
  18.  
  19.  
  20. -- code:
  21.  
  22. local realBoard = {}
  23. local gameBoard = {}
  24. local liveBoard = {}
  25. local showBoard = {}
  26.  
  27. local width = 9
  28. local height = 9
  29. local mines = 10
  30.  
  31. local function shuffle(myTable)
  32. local function isValid(x,y)
  33. local function toXY(i)
  34. local function toIndex(x,y)
  35. local Neighbors = {}
  36. function Neighbors.All(x,y)
  37. function Neighbors.Unknown(x,y)
  38. function Neighbors.Mines(x,y)
  39. function Neighbors.Flagged(x,y)
  40. function Neighbors:Flag(x,y)
  41. function Neighbors:Open(x,y)
  42. function Neighbors:Clear(x,y)
  43. local function getEmptyCells()
  44. local function display()
  45. local function update()
  46. local function expand(x,y)
  47. local function chord(x,y)
  48. local isFirst = true
  49. local function open(x,y)
  50.     if isFirst then
  51.         isFirst = false
  52.         return firstMove(x,y)
  53.     end
  54.     local c = toIndex(x,y)
  55.     if liveBoard[c] == 2 then return end
  56.     liveBoard[c] = 1
  57. end
  58. local function flag(x,y)
  59. local function groupFlag(cells,minFlags,maxFlags)
  60.     for _, i in ipairs(cells) do
  61.         if liveBoard[i]
  62.         liveBoard[i] = {minFlags, maxFlags, {unpack(cells)}}
  63.     end
  64. end
  65. local function reduceGroupFlags()
  66.     local groups = {}
  67.     for _, i in ipairs(liveBoard) do
  68.         if type(i) ~= "table" then continue end
  69.         if #i.Cells == i.Flags then
  70.             for _, c in ipairs(i.Cells) do
  71.                 liveBoard[c] = 2
  72.             end
  73.         end
  74.     end
  75. end
  76. local function validate(firstMove)
  77. local function labelCells()
  78. function firstMove(x,y)
  79. local function setup()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement