# Classic Minesweeper Game - Logic Bug Clearing Empty Squares

**URL:** <https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047>\
**Category:** Games\
**Created:** [October 11, 2023, 6:50pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047 "2023-10-11T18:50:45Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 11, 2023, 6:50pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/1 "2023-10-11T18:50:45Z")

</div>

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.

 ![example---not-clearing-a-cell-north](https://forums.solar2d.com/uploads/default/original/2X/0/06c66b15e496f2defa5c005d3d6e55079671ae58.png)  
 ![example---not-clearing-a-cell-south](https://forums.solar2d.com/uploads/default/original/2X/a/a02764c72cfb0be7549a1be38f9fa7c255b0bd35.png)

Here is the code:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [October 11, 2023, 7:03pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/2 "2023-10-11T19:03:17Z")

</div>

> [@AngelaMcCall](#):
>
> `if (colY == 1) then`

Is that intended? Seems like maybe some copy-paste from the `> 1` tests?

---

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 11, 2023, 7:05pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/3 "2023-10-11T19:05:04Z")

</div>

Hiya, funny you say that I originally had it as ```  
(colY == 1)

```auto

```

---

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 11, 2023, 7:08pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/4 "2023-10-11T19:08:19Z")

</div>

> [@StarCrunch](#):
>
> aybe some copy-paste from the `>`

Code tweeked to be \> =  
problem persists.

```auto
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
    -- if board[rowX-1][colY-1].imgBomb ~= nil then
    -- board[rowX-1][colY-1].imgBomb.isVisible = true
    -- end
    -- if board[rowX-1][colY-1].imgText ~= nil then
    -- board[rowX-1][colY-1].imgText.isVisible = true
    -- end

    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

```

 ![example---not-clearing-a-cell-north---V2](https://forums.solar2d.com/uploads/default/original/2X/4/4e391dc48ac6ed568de42cf0729a848bb2914996.png)

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [October 11, 2023, 7:18pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/5 "2023-10-11T19:18:12Z")

</div>

Oh, well, I mean the two `CENTER` cases wouldn’t seem to need column checks, at all. As is, column 1 will be botched (I’m guessing that’s what we see in the last picture).

---

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 11, 2023, 7:46pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/7 "2023-10-11T19:46:18Z")

</div>

No… my code is design to search the row above the triggering cell. For the cells above, left, centre, right.  
Then the code is designed to search the row below the triggering cell. For the cells below left, centre, right.  
This just leave the two cells directly left and right of the trigger cell.  
There is no column search.

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [October 11, 2023, 7:59pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/8 "2023-10-11T19:59:34Z")

</div>

> [@AngelaMcCall](#):
>
> `if (colY > 1) then --CENTER`

This is what I meant by `CENTER`. In those two cases the column check is pointless (you don’t need to look left or right, so there’s no reason to guard) and I assume when `colY` is 1, it’s skipping the logic.

---

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 11, 2023, 8:17pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/9 "2023-10-11T20:17:45Z")

</div>

Oh ok - let me comment out and test your theory - thanks for helping by the way… if easier we can chat on discord.

---

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 11, 2023, 8:24pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/10 "2023-10-11T20:24:11Z")

</div>

so I tweaked my code to this - still problem persists with north and south cells reveals.

```auto
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 
                if (board[rowX-1][colY].info.btn == true) then
                    board = mod.clearEmptySquares(board, rowX-1, colY)
                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  
                if (board[rowX+1][colY].info.btn == true) then
                    board = mod.clearEmptySquares(board, rowX+1, colY)
                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

```

---

<div class="post-metadata">

**Author:** ![anon63346430](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@anon63346430](https://forums.solar2d.com/u/anon63346430)\
**Post date:** [October 11, 2023, 9:17pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/11 "2023-10-11T21:17:25Z")

</div>

wouldn’t you loop from `x-1`, `x+1` and `y-1`, `y+1`?

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [October 11, 2023, 9:59pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/12 "2023-10-11T21:59:06Z")

</div>

> [@AngelaMcCall](#):
>
> ```auto
> 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 
> if (board[rowX-1][colY].info.btn == true) then
> board = mod.clearEmptySquares(board, rowX-1, colY)
> 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
> 
> ```

Change that to something like

```auto
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
                -- NOT inside the if statement
                if (board[rowX-1][colY].info.btn == true) then
                    board = mod.clearEmptySquares(board, rowX-1, colY)
                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

```

and likewise for the below case, and see what you get.

---

<div class="post-metadata">

**Author:** ![anon63346430](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@anon63346430](https://forums.solar2d.com/u/anon63346430)\
**Post date:** [October 11, 2023, 11:02pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/13 "2023-10-11T23:02:27Z")

</div>

here is some pseudo code

```auto
for i = x-1, x+1 do
  for j = y-1, y+1 do
    if <in bounds i, j> then <do something> end
  end
end

```

you can then easily extend to support looping out 2 or 3 loops easily, like this

```auto
local n = 2
for i = x-n, x+n do
  for j = y-n, y+n do
    if <in bounds i, j> then <do something> end
  end
end

```

---

<div class="post-metadata">

**Author:** ![AngelaMcCall](https://forums.solar2d.com/user_avatar/forums.solar2d.com/angelamccall/32/1457_2.png) [@AngelaMcCall](https://forums.solar2d.com/u/AngelaMcCall)\
**Post date:** [October 12, 2023, 3:00pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/14 "2023-10-12T15:00:46Z")

</div>

THANK YOU SO MUCH SGS 😄

I think with my initial code, i over thought it way to much!

Having scrapped my code and adopted your advice, I have success!!

Here was the resulting function code, for anyone who reads this forum post in the future and fancies learning from this 🙂

```auto
function mod.clearEmptySquares(board, rowX, colY)
    local rowMax = #board 
    local colMax = #board[1]

    local count = 1

    if (board[rowX][colY].info.number == 0) then
        for i = rowX-count, rowX+count do -- itterate matrix
            for j = colY-count, colY+count do -- itterate matrix
                if i >=1 and i <= rowMax then -- stay within board boundaries
                    if j >=1 and j <= colMax then -- stay within board boundaries

                        if (board[i][j].info.number >=1) then -- found clearing edge
                            board[i][j].info.btn = false
                        elseif ( i == rowX) and (j == colY) then -- found myself/trigger box
                            board[i][j].info.btn = false
                        elseif (board[i][j].info.number == 0 ) and ( board[i][j].info.btn == true ) then -- investigate box whose btn still shows
                            mod.clearEmptySquares(board, i, j)
                        end

                    end
                end
            end
        end

    end

    return board
end

```

---

<div class="post-metadata">

**Author:** ![anon63346430](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@anon63346430](https://forums.solar2d.com/u/anon63346430)\
**Post date:** [October 12, 2023, 3:19pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/15 "2023-10-12T15:19:30Z")

</div>

FYI, you can also optimize some more here (makes it more readable too)…

```auto
local tile = board[i][j].info
if (tile.number >=1) then -- found clearing edge
    tile.btn = false
elseif ( i == rowX) and (j == colY) then -- found myself/trigger box
    tile.btn = false
elseif (tile.number == 0 ) and ( tile.btn == true ) then -- investigate box whose btn still shows
    mod.clearEmptySquares(board, i, j)
end

```

---

<div class="post-metadata">

**Author:** ![system](https://forums.solar2d.com/uploads/default/original/1X/1bf6a947166df24e92aaa3358558f4b82d7f3d5a.png) [@system](https://forums.solar2d.com/u/system)\
**Post date:** [April 9, 2024, 3:20pm UTC](https://forums.solar2d.com/t/classic-minesweeper-game-logic-bug-clearing-empty-squares/356047/16 "2024-04-09T15:20:06Z")

</div>

Closed on April 9, 2024, 3:20pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
