Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local realBoard = {} -- true/false = mine/no mine
- local myBoard = {}
- local gameState = {} -- 0 = undiscovered / 1 = discovered / 2 = flag / 3 = ?
- --[[
- local realBoard = {} -- stores positions of mines
- local gameBoard = {} -- stores numbers of adjacent mines
- local liveBoard = {} -- stores current state of each board tile (unknown ,found, flagged, and marked)
- local gamePieces = {} -- physical game pieces
- --]]
- -- settings:
- local width = 9 -- board width
- local height = 9 -- board height
- local density = 0.123 -- avg mines/tile (nil to ignore) [0.123 = beginner / 0.156 = intermediate / 0.206 = expert]
- local mines = 10 -- mine count
- if density then mines = width*height*density end
- local deterministic = false -- avoid 50/50 guess cases (currently unimplemented)
- local openFirstMove = true -- ensure first move lands on a tile with 0 surrounding mines
- --[[ default board settings:
- beginner : 9x9/10
- intermediate : 16x16/40
- expert : 30x16/99
- --]]
- local function shuffle(table)
- local buffer
- for i = #table, 1, -1 do
- local r = math.random(1,i)
- buffer = table[i]
- table[i] = table[r]
- table[r] = buffer
- end
- end
- local function isValid(x, y)
- return x>0 and x<=width and y>0 and y<=height
- end
- local function getNeighbors(i) -- gets neighbor indexes
- local neighbors = {}
- local x = (i-1)%width + 1
- local y = math.floor((i-1)/width)+1
- for ix = x-1, x+1 do
- for iy = y-1, y+1 do
- if isValid(ix, iy) then
- table.insert(neighbors, ix+(iy-1)*width)
- end
- end
- end
- return neighbors
- end
- local function countNearbyMines(i)
- local total = 0
- for _, i in ipairs(getNeighbors(i)) do
- if realBoard[i] then total += 1 end
- end
- return total
- end
- local function labelTiles()
- for i, bomb in ipairs(realBoard) do
- if not bomb then return end
- for _, n in ipairs(getNeighbors(i)) do
- if not realBoard[n] then
- myBoard[n] = myBoard[n]+1
- else
- myBoard[n] = "X"
- end
- end
- end
- end
- local function getGameState() -- 0 = in progress / -1 = lost / 1 = won
- local win = true
- for i, v in ipairs(realBoard) do
- if v and gameState[i] == 1 then
- return -1
- end
- if win and not v and gameState[i] ~= 1 then
- win = false
- end
- end
- if win then return 1 end
- return 0
- end
- local function win() -- does stuff when you win
- end
- local function lose() -- does stuff when you lost
- end
- local function update()
- -- add visual updates to the board here
- local state = getGameState()
- if state == -1 then return lose() end
- if state == 1 then return win() end
- end
- local function open(x,y)
- local i = x+(y-1)*width
- if not isValid(x, y) then return end
- if not gameState[i] == 0 then return end
- gameState[i] = 1
- if myBoard[i] == 0 then
- cord(x,y)
- end
- update()
- end
- local function cord(x,y)
- local state = gameState[x+(y-1)*width]
- local nearbyFlags = 0
- for _, i in ipairs(getNeighbors(x+(y-1)*width)) do
- if gameState[i] == 2 then
- nearbyFlags += 1
- end
- end
- if nearbyFlags == myBoard[t] and (state == 1 or state == 3) then
- for ix = x-1, x+1 do
- for iy = y-1, y+1 do
- open(ix,iy)
- end
- end
- end
- update()
- end
- local function flag(x,y)
- local i = x+(y-1)*width
- local state = gameState[i]
- if state == 0 then
- gameState[i] = 2
- elseif state == 2 then
- gameState[i] = 3
- elseif state = 3 then
- gameState[i] = 0
- end
- end
- local function firstMove(x,y)
- local i = x+(y-1)*width
- if realBoard[i] then
- local newPos = 1
- while realBoard[newPos] do
- newPos += 1
- end
- realBoard[newPos] = true
- realBoard[i] = false
- end
- if openFirstMove then
- local shift = 1
- while countNearbyMines(i) > 0 and shift < width*height do
- local buffer = realBoard[1]
- for s = 1, #realBoard-1 do
- realBoard[s] = realBoard[s+1]
- end
- realBoard[#realBoard] = buffer
- shift = shift + 1
- end
- if countNearbyMines(i) > 0 then
- shuffle(realBoard)
- firstMove(x,y)
- end
- end
- labelTiles()
- open(x,y)
- end
- local function buildBoard() -- creates physical game board
- end
- local function setupBoard()
- realBoard = {}
- myBoard = {}
- gameState = {}
- mines = clamp(mines, 1, width*height-1)
- for i = 1, width*height do
- gameState[i] = 0
- myBoard[i] = 0
- realBoard[i] = false
- if i <= mines then
- realBoard[i] = true
- end
- end
- shuffle(realBoard)
- buildBoard()
- end
- setupBoard()
- firstMove(math.floor(width/2), math.floor(height/2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement