sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

How to get alert to display if there is a mine #205

Closed irishjack490 closed 9 months ago

irishjack490 commented 9 months ago

What's the problem you're trying to solve?

I am trying to allocate mines for minesweeper manually (I am planning to change it to random function later). First I want to test if it works. I am able to click on the box and the color will change to gray which is fine. But I can't seem to find a way to allocate mines

Post any code you think might be relevant (one fenced block per file)

(I have changed this array to display only numbers and not letters)
const board = [
    [c0r4,c1r4,c2r4,c3r4,c4r4],
    [c0r3,c1r3,c2r3,c3r3,c4r3],
    [c0r2,c1r2,c2r2,c3r2,c4r2],
    [c0r1,c1r1,c2r1,c3r1,c4r1],
    [c0r0,c1r0,c2r0,c3r0,c4r0],
]
const numberOfMines = 5;

//let gameOver = false

function setMines (){
let.minesLocation = [0][1]; //testing if alert will come up if I click on c0r4
alert("There is a mine!");

}

function startGame (){
    setMines ();

}

const boxes = document.querySelectorAll('.box');
boxes.forEach(box => {
    box.addEventListener('click', function boxClicked(e) {
        console.log('box clicked', e );
        box.setAttribute('style', 'background-color: lightgray');
    });
});

function boxClicked () {
    let box = this;

}

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

What is your best guess as to the source of the problem?

I think the problem is the array

What things have you already tried to solve the problem?

changed the array values to just numbers

Paste a link to your repository here

https://github.com/irishjack490/Project-1---Browser-Based-Game

Thank you!!

timmshinbone commented 9 months ago

I'm not seeing any code here that is placing the mines on any cached elements, do you have any code like that?

irishjack490 commented 9 months ago

Ah, I don't, let me work on that.. tks for steering me in that direction

irishjack490 commented 9 months ago

My apologies on the delay, I was able to manually allocate the mines. I went ahead and created the boxes via JavaScript. I did a loop to create a row and then used another loop to create column and gave it the Id of box. I then was able to set mines based on coordinates of my choosing. I clicked on a square where I designated mines, it worked. I am a bit lost on trying to get the random mines to allocate, could you take a look? here is what I have for them right now (here is my git:https://github.com/irishjack490/Project-1---Browser-Based-Game/blob/main/main.js)

function setMines () { while(setMines < numberOfMines){ const row = Math.floor(Math.random () rows); const column = Math.floor(Math.random () columns); if(!board[row][column].isMine){ board[row][column].isMine=true; setMines++; }

function for box clicked

function boxClicked () { let box = this; if (minesLocation.isMine) { alert("GAMER OVER"); gameOver = true; return; } Thanks so much

nayaba commented 9 months ago

So the first thing I'm noticing is that we have a counter inside of the setMines function. That counter is also the variable setMines. Do we perhaps need to declare another variable to be our counter?

irishjack490 commented 9 months ago

I will try that, if I run into other issues. I will open a new one. Thank you so much, should I close this issue?