Hi all.
I figured I would build the classic Minesweeper game.
I’ve a tiny logic challenge in my external module whose job it is to find adjacent empty cells and to clear their button.
Currently for testing, my buttons are semi-transparent so I can see underlaying bomb locations and numbers, so I can also see the empty cells. But once fixed I will remove the alpha and you wouldn’t see both buttons and bombs/number at the same time.
board
is a matrix that populates an outter table or rows, before the inner table manages each column/cell in a row.
EG
board[1][1]
would reference the cell in the top left and board[10][10]
the cell in the bottom right
The challenge is some adhoc cells above, and some adhoc cells below (north & south) are missed.
See screenshots.
The first cell listed in the cell the user clicked on.
Here is the code:
function mod.clearEmptySquares(board, rowX, colY)
local rowMax = #board
local colMax = #board[1]
board[rowX][colY].info.btn = false -- receive in the box to show/hide
print("BOX["..rowX.."]["..colY.."]")
if (board[rowX][colY].info.number == 0) then
if rowX > 1 then -- ABOVE
if (colY > 1) then --LEFT
if (board[rowX-1][colY-1].info.btn == true) then
board = mod.clearEmptySquares(board, rowX-1, colY-1)
end
end
if (colY == 1) then --CENTER
if (board[rowX-1][colY].info.btn == true) then
board = mod.clearEmptySquares(board, rowX-1, colY)
end
end
if (colY < colMax) then --RIGHT
if (board[rowX-1][colY+1].info.btn == true)then
board = mod.clearEmptySquares(board, rowX-1, colY+1)
end
end
end
if rowX < rowMax then -- BELOW
if (colY > 1) then --LEFT
if (board[rowX+1][colY-1].info.btn == true) then
board = mod.clearEmptySquares(board, rowX+1, colY-1)
end
end
if (colY == 1) then --CENTER
if (board[rowX+1][colY].info.btn == true) then
board = mod.clearEmptySquares(board, rowX+1, colY)
end
end
if (colY < colMax) then -- RIGHT
if (board[rowX+1][colY+1].info.btn == true)then
board = mod.clearEmptySquares(board, rowX+1, colY+1)
end
end
end
if (colY > 1) then -- LEFT ADJACENT
if (board[rowX][colY-1].info.btn == true) then
board = mod.clearEmptySquares(board, rowX, colY-1)
end
end
if (colY < colMax) then -- RIGHT ADJACENT
if (board[rowX][colY+1].info.btn == true)then
board = mod.clearEmptySquares(board, rowX, colY+1)
end
end
end
return board
end