我正在尝试在 Js 中复制旧的扫雷游戏。但是我一直在向蓝色网格声明条件(距离它中心的炸弹有 2 个数字);当炸弹位于网格的左/右边界时,我在另一侧出现了 2 个有问题的单元格。
(在条件下你可以看到带有数字的评论指的是 50 个炸弹位置,以帮助更好地定位哪个条件做什么,希望它有所帮助)
非常感谢您的帮助,我是学生:)
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()
})
}
我想我有 3 个条件,它们在生成正常的麻木细胞时很有用,但在边界处它们会产生错误的细胞。我尝试添加我的条件 !twoRightSide/!twoLeftSide 在这些,但我也被删除了好细胞.
|| bombsArray.includes(cellNumb - (gridLength * 2) - 1)
|| bombsArray.includes(cellNumb - (gridLength * 2) + 1)
|| bombsArray.includes(cellNumb + (gridLength * 2) + 1)
|| bombsArray.includes(cellNumb + (gridLength * 2) - 1)