Building Minesweepers game

Hi everyone, i just started learning Javascript. I’m building a Minesweeper clone. I need to solve a problem. My code has comments. I need to add a random generated number in a range between (1-16) <mines). Check, if on click, one of these numbers is present in my 100 numbers, if yes, player lose the game, if not, player can continue to click the squares. Thank you all! I can only use: for and while loops, functions.

`// ! Selecting grid output (parent)
const myContainer = document.getElementById("my_container");

// * Defining clickable button
const play = document.getElementById("play");



// ! Defining how many times i need to generate a square in the grid
// ? generate a square (1oo)
// ? assign proprieties they need 
// ? add numbers inside the squares
// ? add the new generated square to the parent (output)
// ? the squares will be clickable:
// * everytime there is a click, add a class if absent
// * remove a class if present


for (let i = 1; i < 100 + 1; i++) {
    const genSquare = fNewSquare();
    

    // * Adding numbers to squares 
    const ConsoleN = genSquare.innerHTML = i;

    // ! Clickable PLAY button for user to generate a new grid
    // *Generating squares on button click 
    play.addEventListener("click", function () {
        myContainer.append(genSquare);
    })

    // *Highlighting square on button click 
    genSquare.addEventListener("click", function () {

        let randomNumber = Math.floor((Math.random() * 16) + 1);


        console.log(randomNumber);
        genSquare.classList.toggle("highlighted");
        console.log(ConsoleN);
    })

}

// *Generating element 
function fNewSquare() {
    const newBox = document.createElement("div");
    newBox.classList.add("square");
    return newBox;

}










I’m sure what exactly what you are asking about specifically. We don’t write code for users, so you need to tell us where your are confused or stuck with your efforts so far.

Hi, thanks for your reply. I started 2 weeks ago… I’m confused on how/where to implement the check for mines in the game. Mines list should be my array and then i will check with a while loop through 100 numbers if there is any correspondence in my mines array. I wonder if this is the right logic.

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