|
| 1 | +const boardEl = document.querySelector(".board"); |
| 2 | +const cellsEl = [...document.querySelectorAll(".cell")]; |
| 3 | +const gameStatusEl = document.querySelector(".game-status"); |
| 4 | + |
| 5 | +const CLASS_X = "x"; |
| 6 | +const CLASS_CIRCLE = "circle"; |
| 7 | + |
| 8 | +const WINNING_COMBINATIONS = [ |
| 9 | + [0, 1, 2], |
| 10 | + [3, 4, 5], |
| 11 | + [6, 7, 8], |
| 12 | + [0, 3, 6], |
| 13 | + [1, 4, 7], |
| 14 | + [2, 5, 8], |
| 15 | + [2, 4, 6], |
| 16 | + [0, 4, 8], |
| 17 | +]; |
| 18 | + |
| 19 | +const xPositions = []; |
| 20 | + |
| 21 | +cellsEl.forEach((cell) => |
| 22 | + cell.addEventListener("click", handleClick, { once: true }) |
| 23 | +); |
| 24 | + |
| 25 | +function handleClick(e) { |
| 26 | + const currentXCell = e.target; |
| 27 | + |
| 28 | + updateCell(currentXCell, CLASS_X); |
| 29 | + |
| 30 | + xPositions.push(cellsEl.indexOf(currentXCell)); |
| 31 | + |
| 32 | + const xWins = checkWin(CLASS_X); |
| 33 | + |
| 34 | + xWins ? showGameModal(`won`) : addO(); |
| 35 | +} |
| 36 | + |
| 37 | +function addO() { |
| 38 | + const position = getOPositon(); |
| 39 | + |
| 40 | + const currentOCell = cellsEl[position]; |
| 41 | + |
| 42 | + if (currentOCell) { |
| 43 | + updateCell(currentOCell, CLASS_CIRCLE); |
| 44 | + currentOCell.removeEventListener("click", handleClick); |
| 45 | + const oWins = checkWin(CLASS_CIRCLE); |
| 46 | + |
| 47 | + if (oWins) showGameModal(`lost`); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function updateCell(cell, currentClass) { |
| 52 | + cell.classList.add(currentClass); |
| 53 | +} |
| 54 | + |
| 55 | +function getOPositon() { |
| 56 | + const xWinningCombinations = WINNING_COMBINATIONS.filter( |
| 57 | + (column) => |
| 58 | + xPositions.some((i) => column.includes(i)) && |
| 59 | + !column.some((i) => cellsEl[i].classList.contains(CLASS_CIRCLE)) |
| 60 | + ); |
| 61 | + |
| 62 | + const emptyCells = WINNING_COMBINATIONS.filter((i) => |
| 63 | + i.some((v) => !cellsEl[v].classList.contains(CLASS_CIRCLE)) |
| 64 | + ); |
| 65 | + |
| 66 | + const xPositionArr = xWinningCombinations |
| 67 | + .map((column) => |
| 68 | + column.filter((i) => !cellsEl[i].classList.contains(CLASS_X)) |
| 69 | + ) |
| 70 | + .sort((a, b) => a.length - b.length); |
| 71 | + |
| 72 | + const position = xPositionArr[0]?.[getRandomNumber(xPositionArr[0]?.length)]; |
| 73 | + |
| 74 | + if (!position && position !== 0) showGameModal(`draw`); |
| 75 | + |
| 76 | + return position; |
| 77 | +} |
| 78 | + |
| 79 | +function checkWin(currentClass) { |
| 80 | + return WINNING_COMBINATIONS.some((column) => |
| 81 | + column.every((i) => cellsEl[i].classList.contains(currentClass)) |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function stopGame() { |
| 86 | + cellsEl.forEach((cell) => cell.removeEventListener("click", handleClick)); |
| 87 | + boardEl.classList.add("game-end"); |
| 88 | +} |
| 89 | + |
| 90 | +function showGameModal(gameStatus) { |
| 91 | + const gameStatusTitle = gameStatusEl.querySelector(".game-status-title"); |
| 92 | + const restartBtn = gameStatusEl.querySelector(".btn-primary"); |
| 93 | + |
| 94 | + const gameStatues = { |
| 95 | + won: { className: "won", text: "You Won!" }, |
| 96 | + lost: { className: "lost", text: "Oops! You Lost" }, |
| 97 | + draw: { className: "draw", text: "It's a draw" }, |
| 98 | + }; |
| 99 | + |
| 100 | + const { |
| 101 | + [gameStatus]: { className, text }, |
| 102 | + } = gameStatues; |
| 103 | + |
| 104 | + gameStatusEl.classList.add(className); |
| 105 | + gameStatusEl.style.display = "flex"; |
| 106 | + gameStatusTitle.textContent = text; |
| 107 | + stopGame(); |
| 108 | + |
| 109 | + restartBtn.addEventListener("click", () => window.location.reload()); |
| 110 | +} |
| 111 | + |
| 112 | +// Helper functions |
| 113 | + |
| 114 | +function getRandomNumber(limit) { |
| 115 | + return Math.floor(Math.random() * limit); |
| 116 | +} |
0 commit comments