Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda