Minesweeper Nearby Bomb Trouble

Question:
I’m trying to find the number of bombs next to a square so i’m manually checking each square for a bomb next to it. The problem is, only a couple work and I can’t find out why.

// 0-5 = Bombs nearby
// 6 == Bomb
let board = {
        0: [0, 0, 0, 0, 0],
        1: [0, 0, 0, 0, 0],
        2: [0, 0, 6, 0, 0],
        3: [0, 0, 0, 0, 0],
        4: [0, 0, 0, 0, 0]
}

for (row in board){
        for (i in board[row]){
                if (board[row][i] != 6){
                        try {
                                if (board[row][i-1] == 6){
                                        board[row][i] += 1 
                                        // Works
                                }
                        } catch {};
                        try {
                                if (board[row][i+1] == 6){
                                        board[row][i] += 1  
                                        // Doesn't Work
                                }
                        } catch {};
                        try {
                                if (board[row+1][i] == 6){
                                      board[row][i] += 1  
                                }
                        } catch {};
                        try {
                                if (board[row-1][i] == 6){
                                      board[row][i] += 1  
                                }
                        } catch {};
                        try {
                                if (board[row-1][i-1] == 6){
                                      board[row][i] += 1  
                                }
                        } catch {};
                        try {
                                if (board[row-1][i+1] == 6){
                                      board[row][i] += 1  
                                }
                        } catch {};
                        try {
                                if (board[row+1][i-1] == 6){
                                      board[row][i] += 1  
                                }
                        } catch {};
                        try {
                                if (board[row+1][i+1] == 6){
                                      board[row][i] += 1  
                                }
                        } catch {};
                }
        }
}

console.log("0: ", board[0])
console.log("1: ", board[1])
console.log("2: ", board[2])
console.log("3: ", board[3])
console.log("4: ", board[4])

Is this the only check that fails, or are other ones missed as well?

1 Like

Sorry, it was hard for me to describe the issue so it took a while

let board = {
    0: [0, 0, 0, 0, 0],
    1: [0, 0, 0, 0, 0],
    2: [0, 0, 6, 0, 0],
    3: [0, 0, 0, 0, 0],
    4: [0, 0, 0, 0, 0]
}

for (let row in board) {
    for (let i in board[row]) {
        if (board[row][i] != 6) {
            try {
                if (board[row][parseInt(i) - 1] == 6) {
                    board[row][i] += 1;
                    // Works
                }
            } catch {}
            try {
                if (board[row][parseInt(i) + 1] == 6) {
                    board[row][i] += 1;
                    // Works now
                }
            } catch {}
            try {
                if (board[parseInt(row) + 1][i] == 6) {
                    board[row][i] += 1;
                }
            } catch {}
            try {
                if (board[parseInt(row) - 1][i] == 6) {
                    board[row][i] += 1;
                }
            } catch {}
            try {
                if (board[parseInt(row) - 1][parseInt(i) - 1] == 6) {
                    board[row][i] += 1;
                }
            } catch {}
            try {
                if (board[parseInt(row) - 1][parseInt(i) + 1] == 6) {
                    board[row][i] += 1;
                }
            } catch {}
            try {
                if (board[parseInt(row) + 1][parseInt(i) - 1] == 6) {
                    board[row][i] += 1;
                }
            } catch {}
            try {
                if (board[parseInt(row) + 1][parseInt(i) + 1] == 6) {
                    board[row][i] += 1;
                }
            } catch {}
        }
    }
}

console.log("0: ", board[0]);
console.log("1: ", board[1]);
console.log("2: ", board[2]);
console.log("3: ", board[3]);
console.log("4: ", board[4]);

See if this works.

2 Likes

The only three that work are the first one, the third one, and the seventh one

Wow! That worked perfectly, thanks!

no problem! part of the issue was how you were accessing certain things. I couldn’t describe though exactly what you needed to change since i don’t use this language often.

1 Like

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