Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
Looking to understand how to count and reveal adjacent tiles in minesweeper

I wrote an algorithm to count and reveal all adjacent tiles in minesweeper. However, when a tile is revealed and does not contain a mine, it displays zero. The adjacent mines do not get revealed. I certain that the issue is in my code but I'm unable to figure it out. I'm looking for some guidance.

screenshot illustrating what I described above.

My code:
// Left Click
    if (event.which === 1) 
    {
         startTimer();
        if(tile.classList.contains("mine"))
        {
            tile.classList.toggle("mine_hit");
            smiley.classList.toggle("face_lose");
            window.alert("Game Over. You lose!");
            revealMines();
             
        }

        else
        {
            tile.classList.contains("hidden");
            tile.classList.remove("hidden");

            // Count and display the number of adjacent mines
            var mineCount = 0;
            var tileRow = tile.parentNode.rowIndex;
            var tileCol = tile.cellIndex;

            //alert (tileRow + "" + tileCol);
            for (var i = Math.max(tileRow-1,0); i <= Math.min(tileRow+1,9); i++)
            {
                for (var j = Math.max(tileCol-1,0); j <=Math.min(tileCol+1,9); j++)
                {
                    if(minefield.rows[i].tile[j].classList.contains("mine"))
                    {
                        mineCount++;
                    }
                }
            }
            tile.innerHTML=mineCount;
            if (mineCount==0)
            {
                //Reveal all adjacent tiles as they do not have a mine
                for (var i = Math.max(tileRow -1, 0); i <= Math.min(tileRow +1, 9); i++)
                {
                    for (var j=Math.max(tileCol -1, 0); j <=Math.min(tileCol+1,9); j++)
                    {
                        //recursive call
                        if (minefield.rows[i].tiles[j].innerHTML=="") handleTileClick(minefield.rows[i].tile[j]);
                    }
                }
            }
            //checkCompletionLevel();
             
        }
        
    }

I have tried modifying the revealMines function but no success. I changed this line tile.innerHTML=mineCount to tile.classList.containst("Hidden")=tile.ClassList.toggle("tile_1"). But the game board display 1 in blue where the tile was clicked. I have looked up other minesweeper games to try and understand but no success.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!