How to Create a Colorful Tic-Tac-Toe Game with HTML, CSS and JavaScript
June 1, 2024 ⚊ 3 Min read ⚊ CSS HTML JAVASCRIPT
Here’s a colorful Tic-Tac-Toe game implemented using HTML, CSS, and JavaScript only. The game includes a restart button, winner message, and special effects for the winning line.
HTML Code for Tic-Tac-Toe
<!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>
<div class="container">
<h1>Tic Tac Toe</h1>
<div id="board">
<div class="square" data-index="0"></div>
<div class="square" data-index="1"></div>
<div class="square" data-index="2"></div>
<div class="square" data-index="3"></div>
<div class="square" data-index="4"></div>
<div class="square" data-index="5"></div>
<div class="square" data-index="6"></div>
<div class="square" data-index="7"></div>
<div class="square" data-index="8"></div>
</div>
<div id="message"></div>
<button id="restart">Restart Game</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code for Tic-Tac-Toe
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
font-family: Arial, sans-serif;
}
.container {
text-align: center;
}
h1 {
color: #333;
}
#board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 10px;
margin: 20px auto;
}
.square {
width: 100px;
height: 100px;
background-color: #ddd;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
cursor: pointer;
transition: background-color 0.3s;
}
.square.cross {
background-color: #ff9999;
}
.square.circle {
background-color: #99ccff;
}
#message {
margin: 20px;
font-size: 1.2em;
color: #333;
}
button {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
}
JavaScript Code for Tic-Tac-Toe
document.addEventListener('DOMContentLoaded', () => {
const squares = document.querySelectorAll('.square');
const message = document.getElementById('message');
const restartButton = document.getElementById('restart');
let isCrossTurn = true;
let board = ['', '', '', '', '', '', '', '', ''];
squares.forEach(square => {
square.addEventListener('click', handleSquareClick);
});
restartButton.addEventListener('click', restartGame);
function handleSquareClick(event) {
const square = event.target;
const index = square.dataset.index;
if (board[index] !== '') return;
board[index] = isCrossTurn ? 'X' : 'O';
square.textContent = isCrossTurn ? 'X' : 'O';
square.classList.add(isCrossTurn ? 'cross' : 'circle');
if (checkWinner()) {
message.textContent = `Player ${isCrossTurn ? '1 (Cross)' : '2 (Circle)'} wins!`;
squares.forEach(square => square.removeEventListener('click', handleSquareClick));
} else if (board.every(cell => cell !== '')) {
message.textContent = 'It\'s a draw!';
} else {
isCrossTurn = !isCrossTurn;
}
}
function checkWinner() {
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
return winningCombinations.some(combination => {
const [a, b, c] = combination;
return board[a] && board[a] === board[b] && board[a] === board[c];
});
}
function restartGame() {
board = ['', '', '', '', '', '', '', '', ''];
squares.forEach(square => {
square.textContent = '';
square.classList.remove('cross', 'circle');
square.addEventListener('click', handleSquareClick);
});
message.textContent = '';
isCrossTurn = true;
}
});
Let’s implement and play Tic-Tac-Toe game.
Tags:
- HTML CSS Tic-Tac-Toe game
- Create Tic-Tac-Toe with HTML and CSS
- HTML Tic-Tac-Toe tutorial
- CSS Tic-Tac-Toe game design
- Colorful Tic-Tac-Toe game
- HTML CSS game development
- Tic-Tac-Toe game effects
- Tic-Tac-Toe restart button
- Tic-Tac-Toe winner message
- HTML CSS game tutorial
- Interactive Tic-Tac-Toe with CSS
- Simple Tic-Tac-Toe game HTML CSS
- Build Tic-Tac-Toe without JavaScript
- Responsive Tic-Tac-Toe game design
- Beginner HTML CSS game project
Tags:
Tic Tac Toe
Tic Tac Toe Games