I'm trying to replicate in Js the old Mine Sweeper game. But I'm stuck declaring conditions to the blue grid (that is distant 2 numbers from the bomb at the center of it); I have 2 bugged cells appearing on the other side when a bomb is on the grid left/right border. See this Example
(In the conditions you can see comments with numbers referring to 50 bomb position to help locate better which condition does what, hope it helps)
Thanks a lot for help me, I'm a student :)
let gridLength = Math.sqrt(numbOfCells);
const cellNumb = Number(singleCell.textContent);
const atRightSide = cellNumb % gridLength === 0;
const atLeftSide = (cellNumb - 1) % gridLength === 0;
const twoRightSide = cellNumb % gridLength === 0 || (cellNumb + 1) % gridLength === 0;
//check 9+10 nums and 10 multiplies
const twoLeftSide = (cellNumb - 1) % gridLength === 0 || (cellNumb) % gridLength === 2;
// check 1+10 nums and 2+10 nums
if (bombsArray.includes(cellNumb)) {
singleCell.classList.add('bomb')
// this create green cells
} else if (!atLeftSide && bombsArray.includes(cellNumb - 1)
|| !atRightSide && bombsArray.includes(cellNumb + 1)
|| bombsArray.includes(cellNumb - gridLength)
|| bombsArray.includes(cellNumb + gridLength)
|| !atLeftSide && bombsArray.includes(cellNumb - gridLength - 1)
|| !atRightSide && bombsArray.includes(cellNumb - gridLength + 1)
|| !atLeftSide && bombsArray.includes(cellNumb + gridLength - 1)
|| !atRightSide && bombsArray.includes(cellNumb + gridLength + 1)
) {
singleCell.classList.add('green');
singleCell.addEventListener('click', function () {
addGreenPoints()
})
// this create blue cells (example 55)
} else if (
!twoLeftSide && bombsArray.includes(cellNumb - 2)
// 53
|| !twoRightSide && bombsArray.includes(cellNumb + 2)
// 57
//left and right blue cell
|| bombsArray.includes(cellNumb - (gridLength * 2))
|| bombsArray.includes(cellNumb + (gridLength * 2))
// these are the top/bottom blue cells
|| !twoLeftSide && bombsArray.includes(cellNumb - (gridLength * 2) - 2)
// ↖↖ (33)
// ----------- I think these are the bugged ones
|| bombsArray.includes(cellNumb - (gridLength * 2) - 1)
// 34
|| bombsArray.includes(cellNumb - (gridLength * 2) + 1)
// ↗ 36
|| bombsArray.includes(cellNumb + (gridLength * 2) + 1)
// ➡ 76
|| bombsArray.includes(cellNumb + (gridLength * 2) - 1)
// ⬅ 74
// -----------
|| !twoRightSide && bombsArray.includes(cellNumb - (gridLength * 2) + 2)
// ↗↗ 37
|| !twoRightSide && bombsArray.includes(cellNumb - gridLength + 2)
// ➡ 47
|| !twoRightSide && bombsArray.includes(cellNumb + gridLength + 2)
// ↘ 67
|| !twoRightSide && bombsArray.includes(cellNumb + (gridLength * 2) + 2)
// ↘↘ 77
|| !twoLeftSide && bombsArray.includes(cellNumb + (gridLength * 2) - 2)
// ↙↙ 73
|| !twoLeftSide && bombsArray.includes(cellNumb + gridLength - 2)
// ⬅ 63
|| !twoLeftSide && bombsArray.includes(cellNumb - gridLength - 2)
// ⬅ 43
) {
singleCell.classList.add('blue');
singleCell.addEventListener('click', function () {
addBluePoints()
})
}
I think I have these conditions that are useful when generating normal numbs cells, but at borders they create the bugged cells. I tried adding my condition !twoRightSide/!twoLeftSide at these, but I got removed also good cells as you can see here.
|| bombsArray.includes(cellNumb - (gridLength * 2) - 1)
|| bombsArray.includes(cellNumb - (gridLength * 2) + 1)
|| bombsArray.includes(cellNumb + (gridLength * 2) + 1)
|| bombsArray.includes(cellNumb + (gridLength * 2) - 1)