Sure! Below is an updated version of the Tic Tac Toe game with features like highlighting the winning combination and improving the user interface.
HTML (index.html)
We keep this the same as before because the structure doesn't need to change.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Tic Tac Toe</h1>
<div class="board" id="board">
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
</div>
<button id="restartButton">Restart Game</button>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
Now, we add styles to improve the user interface and highlight winning combinations.
css Copy code
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: 'Arial', sans-serif;
color: #333;
}
h1 {
margin-bottom: 20px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin-bottom: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
cursor: pointer;
border: 2px solid #000;
transition: background-color 0.3s;
}
.cell.x {
color: #ff6b6b;
}
.cell.circle {
color: #4ecdc4;
}
.cell.winning {
background-color: #ffe66d;
}
button {
padding: 10px 20px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
border: none;
background-color: #4ecdc4;
color: #fff;
border-radius: 5px;
transition: background-color 0.3s;
}
button:hover {
background-color: #3bb5a6;
}
JavaScript (script.js)
Now, we update the script to include the feature of highlighting the winning combination.
javascript Copy code
const X_CLASS = 'x'
const CIRCLE_CLASS = 'circle'
const WINNING_COMBINATIONS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
const cellElements = document.querySelectorAll('[data-cell]')
const board = document.getElementById('board')
const restartButton = document.getElementById('restartButton')
let circleTurn
startGame()
restartButton.addEventListener('click', startGame)
function startGame() {
circleTurn = false
cellElements.forEach(cell => {
cell.classList.remove(X_CLASS)
cell.classList.remove(CIRCLE_CLASS)
cell.classList.remove('winning')
cell.removeEventListener('click', handleClick)
cell.addEventListener('click', handleClick, { once: true })
})
}
function handleClick(e) {
const cell = e.target
const currentClass = circleTurn ? CIRCLE_CLASS : X_CLASS
placeMark(cell, currentClass)
if (checkWin(currentClass)) {
endGame(false)
highlightWinningCombination()
} else if (isDraw()) {
endGame(true)
} else {
swapTurns()
}
}
function endGame(draw) {
if (draw) {
alert("Draw!")
} else {
alert(`${circleTurn ? "O's" : "X's"} Wins!`)
}
}
function isDraw() {
return [...cellElements].every(cell => {
return cell.classList.contains(X_CLASS) || cell.classList.contains(CIRCLE_CLASS)
})
}
function placeMark(cell, currentClass) {
cell.classList.add(currentClass)
}
function swapTurns() {
circleTurn = !circleTurn
}
function checkWin(currentClass) {
return WINNING_COMBINATIONS.some(combination => {
return combination.every(index => {
return cellElements[index].classList.contains(currentClass)
})
})
}
function highlightWinningCombination() {
WINNING_COMBINATIONS.forEach(combination => {
if (combination.every(index => cellElements[index].classList.contains(circleTurn ? CIRCLE_CLASS : X_CLASS))) {
combination.forEach(index => {
cellElements[index].classList.add('winning')
})
}
})
}
Summary of Changes
CSS Enhancements:
Different colors for "X" and "O".
Highlighting winning cells with a yellow background.
A more polished button with hover effects.
JavaScript Logic:
Enhanced to add the winning class to the cells of the winning combination, which gets styled in the CSS.
Now, when someone wins the game, the winning cells will be highlighted, and the UI will look a bit nicer with styled buttons and text. This should make the game more visually appealing and interactive.