//上 functionhintUp(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.y = condition.y - 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //下 functionhintBottom(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.y = condition.y + 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //左 functionhintLeft(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.x = condition.x - 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //右 functionhintRight(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.x = condition.x + 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //左上 functionhintTopLeft(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.y = condition.y - 1 condition.x = condition.x - 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //右上 functionhintTopRight(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.y = condition.y - 1 condition.x = condition.x + 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //左下 functionhintBottomLeft(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.y = condition.y + 1 condition.x = condition.x - 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 } //右下 functionhintBottomRight(cell) { let flatCells = cells.flat() let condition = { ...cell } condition.y = condition.y + 1 condition.x = condition.x + 1 let findCell = flatCells.find(c => c.x === condition.x && c.y === condition.y) if (findCell && findCell.isBoom) return1 return0 }
//設定提示有幾個炸彈 functionnumOfBooms() { for (let y = 0; y < cells.length; y++) { for (let x = 0; x < cells.length; x++) { let cell = cells[y][x] if (cell.isBoom === false) { let hint = 0 hint += hintUp(cell) hint += hintBottom(cell) hint += hintLeft(cell) hint += hintRight(cell) hint += hintTopLeft(cell) hint += hintTopRight(cell) hint += hintBottomLeft(cell) hint += hintBottomRight(cell) cell.hint = hint } } } }
//初始化 dom functioninitDom() { let game = document.querySelector('.game') let flatCells = cells.flat() for (let cell of flatCells) { let template = `<div class="cell" data-pos="${cell.pos}" data-x="${cell.x}" data-y="${cell.y}"></div>` game.insertAdjacentHTML('beforeend', template) } }
插旗幟及是否贏了系列函數
初始化旗幟比較簡單, 只要塞入 10 個旗幟, 並且加上座標, 還有是否使用即可
1 2 3 4 5 6
//初始化旗幟 functioninitFlags() { for (let i = 0; i < 10; i++) { flags.push({ isUse: false, pos: -1, x: -1, y: -1 }) } }
let domCells = document.querySelectorAll('[class^="cell"]') domCells.forEach(domCell => { domCell.addEventListener('click', function () { let pos = domCell.dataset.pos let cell = findCell(parseInt(pos)) //這裡一定要寫這樣生命週期才會正確 //不然拿不到 querySelector 的 dom clickCell(cell) }) })
functionclickCell(cell) { if (cell.isHidden === false) return
cell.isHidden = false let domCell = document.querySelector(`[data-pos="${cell.pos}"]`); if (cell.isBoom) { domCell.textContent = '🍑' alert('Game Over!') } else { //如果是空格需要遞迴掃描 if (cell.hint === 0) { domCell.textContent = cell.hint
//自己周圍要先點 let selfAroundCells = scanZeroAround(cell) for (let selfAroundCell of selfAroundCells) { clickCell(selfAroundCell) }
//其他 0 相鄰的 let scanCells = scanZero(cell) for (let zeroCell of scanCells) { let aroundCells = scanZeroAround(zeroCell) for (let aroundCell of aroundCells) { clickCell(aroundCell) } }