Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

301
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda