()
| 10 | |
| 11 | //create Board |
| 12 | function createBoard() { |
| 13 | flagsLeft.innerHTML = numBombs; |
| 14 | |
| 15 | //get shuffled game array with random bombs |
| 16 | const bombsArray = Array(numBombs).fill("bomb"); |
| 17 | const emptyArray = Array(width * width - numBombs).fill("valid"); |
| 18 | const gameArray = emptyArray.concat(bombsArray); |
| 19 | const shuffledArray = gameArray.sort(() => Math.random() - 0.5); |
| 20 | |
| 21 | for (let i = 0; i < width * width; i++) { |
| 22 | const square = document.createElement("div"); |
| 23 | square.setAttribute("id", i); |
| 24 | square.classList.add(shuffledArray[i]); |
| 25 | grid.appendChild(square); |
| 26 | squares.push(square); |
| 27 | |
| 28 | //normal click |
| 29 | square.addEventListener("click", function (e) { |
| 30 | click(square); |
| 31 | }); |
| 32 | |
| 33 | //cntrl and left click |
| 34 | square.oncontextmenu = function (e) { |
| 35 | e.preventDefault(); |
| 36 | addFlag(square); |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | //add numbers |
| 41 | for (let count = 0; count < squares.length; count++) { |
| 42 | let total = 0; |
| 43 | const isLeftEdge = count % width === 0; |
| 44 | const isRightEdge = count % width === width - 1; |
| 45 | |
| 46 | if (squares[count].classList.contains("valid")) { |
| 47 | if ( |
| 48 | count > 0 && |
| 49 | !isLeftEdge && |
| 50 | squares[count - 1].classList.contains("bomb") |
| 51 | ) |
| 52 | total++; |
| 53 | if ( |
| 54 | count > 9 && |
| 55 | !isRightEdge && |
| 56 | squares[count + 1 - width].classList.contains("bomb") |
| 57 | ) |
| 58 | total++; |
| 59 | if (count > 10 && squares[count - width].classList.contains("bomb")) |
| 60 | total++; |
| 61 | if ( |
| 62 | count > 11 && |
| 63 | !isLeftEdge && |
| 64 | squares[count - 1 - width].classList.contains("bomb") |
| 65 | ) |
| 66 | total++; |
| 67 | if ( |
| 68 | count < 98 && |
| 69 | !isRightEdge && |
no test coverage detected