Sure! Below is a complete, self-contained HTML file that implements a modern-styled Tic Tac Toe game with animations using HTML, CSS, and JavaScript. You can create a new .html file (e.g., tic-tac-toe.html) and paste the following code into it. Then, open the file in your web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Tic Tac Toe</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #74ebd5 0%, #ACB6E5 100%);
}
.container {
background: #fff;
border-radius: 15px;
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
padding: 20px;
max-width: 400px;
width: 90%;
text-align: center;
}
h1 {
margin-bottom: 20px;
color: #333;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 10px;
justify-content: center;
margin-bottom: 20px;
}
.cell {
background: #f0f0f0;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 2.5rem;
cursor: pointer;
transition: background 0.3s, transform 0.2s;
position: relative;
}
.cell:hover {
background: #e0e0e0;
transform: scale(1.05);
}
.cell.disabled {
pointer-events: none;
cursor: default;
}
.cell::after {
content: '';
position: absolute;
width: 0;
height: 0;
background: inherit;
transition: all 0.3s;
}
.message {
margin-bottom: 20px;
font-size: 1.2rem;
color: #555;
}
.restart-btn {
padding: 10px 20px;
background: #74ebd5;
border: none;
border-radius: 5px;
color: #fff;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s, transform 0.2s;
}
.restart-btn:hover {
background: #43c6b7;
transform: scale(1.05);
}
.winner-line {
position: absolute;
width: 0;
height: 0;
background: #ff4757;
transition: all 0.5s;
z-index: 1;
}
/* Winning Lines */
.horizontal-0::after {
top: 50%;
left: 0;
width: 100%;
height: 4px;
transform: translateY(-50%);
}
.horizontal-1::after {
top: 50%;
left: 0;
width: 100%;
height: 4px;
transform: translateY(-50%);
}
.horizontal-2::after {
top: 50%;
left: 0;
width: 100%;
height: 4px;
transform: translateY(-50%);
}
.vertical-0::after {
top: 0;
left: 50%;
width: 4px;
height: 100%;
transform: translateX(-50%);
}
.vertical-1::after {
top: 0;
left: 50%;
width: 4px;
height: 100%;
transform: translateX(-50%);
}
.vertical-2::after {
top: 0;
left: 50%;
width: 4px;
height: 100%;
transform: translateX(-50%);
}
.diagonal-1::after {
top: 0;
left: 0;
width: 4px;
height: 141%;
transform: rotate(45deg);
transform-origin: top left;
}
.diagonal-2::after {
top: 0;
right: 0;
width: 4px;
height: 141%;
transform: rotate(-45deg);
transform-origin: top right;
}
</style>
</head>
<body>
<div class="container">
<h1>Tic Tac Toe</h1>
<div class="message" id="message">Player X's turn</div>
<div class="board" id="board">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<button class="restart-btn" id="restartBtn">Restart Game</button>
</div>
<script>
const board = document.getElementById('board');
const cells = Array.from(document.querySelectorAll('.cell'));
const message = document.getElementById('message');
const restartBtn = document.getElementById('restartBtn');
let currentPlayer = 'X';
let gameActive = true;
let boardState = ["", "", "", "", "", "", "", "", ""];
const winningConditions = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];
function handleCellClick(e) {
const index = e.target.getAttribute('data-index');
if (boardState[index] !== "" || !gameActive) {
return;
}
boardState[index] = currentPlayer;
e.target.textContent = currentPlayer;
e.target.classList.add('disabled');
if (checkWin()) {
message.textContent = `Player ${currentPlayer} wins!`;
gameActive = false;
highlightWinningCells();
return;
}
if (!boardState.includes("")) {
message.textContent = `It's a draw!`;
gameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
message.textContent = `Player ${currentPlayer}'s turn`;
}
function checkWin() {
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (boardState[a] && boardState[a] === boardState[b] && boardState[a] === boardState[c]) {
return condition;
}
}
return null;
}
function highlightWinningCells() {
const winningCondition = checkWin();
if (winningCondition) {
const [a, b, c] = winningCondition;
cells[a].classList.add('winner');
cells[b].classList.add('winner');
cells[c].classList.add('winner');
// Determine the type of winning line
if (a + 1 === b && b + 1 === c) {
// Horizontal
const row = Math.floor(a / 3);
board.classList.add(`horizontal-${row}`);
} else if (a % 3 === b % 3 && b % 3 === c % 3) {
// Vertical
const col = a % 3;
board.classList.add(`vertical-${col}`);
} else {
// Diagonal
if (winningCondition.includes(0) && winningCondition.includes(4) && winningCondition.includes(8)) {
board.classList.add('diagonal-1');
} else {
board.classList.add('diagonal-2');
}
}
}
}
function restartGame() {
currentPlayer = 'X';
gameActive = true;
boardState = ["", "", "", "", "", "", "", "", ""];
message.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => {
cell.textContent = "";
cell.classList.remove('disabled', 'winner');
});
board.classList.remove('horizontal-0', 'horizontal-1', 'horizontal-2', 'vertical-0', 'vertical-1', 'vertical-2', 'diagonal-1', 'diagonal-2');
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartBtn.addEventListener('click', restartGame);
</script>
</body>
</html>
Features:
- Modern Design: Clean layout with a responsive grid and smooth color gradients.
- Animations:
- Hover Effects: Cells slightly enlarge and change color on hover.
- Winning Line: A colored line animates across the winning combination.
- Button Animation: The restart button scales up slightly on hover.
- Gameplay:
- Alternates between Player X and Player O.
- Detects win and draw conditions.
- Highlights the winning cells with a line.
- Provides a restart button to reset the game.
How It Works:
- HTML Structure: Consists of a container with a title, message display, the game board (a 3x3 grid), and a restart button.
- CSS Styling: Utilizes Flexbox and Grid for layout, styled with modern colors and transitions for smooth animations.
- JavaScript Logic:
- Maintains the game state and current player.
- Handles cell clicks to place X or O.
- Checks for win or draw after each move.
- Highlights the winning combination.
- Allows restarting the game.
Feel free to customize the styles or add more features as you like. Enjoy your modern Tic Tac Toe game!