Frontend Forever App
We have a mobile app for you to download and use. And you can unlock many features in the app.
Get it now
Intall Later
Run
HTML
CSS
Javascript
Output
Tic Tac Toe
Tic Tac Toe
.board { display: grid; grid-template-columns: repeat(3, 100px); grid-gap: 5px; } .cell { width: 100px; height: 100px; text-align: center; font-size: 24px; background-color: #ccc; cursor: pointer; } .cell:hover { background-color: #ddd; } #message { font-size: 20px; margin-top: 20px; }
const board = document.getElementById("board"); const message = document.getElementById("message"); const cells = []; let currentPlayer = "X"; let gameOver = false; // Create the Tic Tac Toe grid for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { const cell = document.createElement("div"); cell.classList.add("cell"); cell.dataset.row = i; cell.dataset.col = j; cell.addEventListener("click", handleClick); cells.push(cell); board.appendChild(cell); } } function checkWinner() { const winPatterns = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ]; for (const pattern of winPatterns) { const [a, b, c] = pattern; if (