Sure! Below is a complete, modern-styled Tic Tac Toe game contained within a single HTML file. It utilizes HTML, CSS, and JavaScript, and includes animations for a smoother and more engaging user experience.
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>Modern Tic Tac Toe</title>
<style>
:root {
--bg-color: #1e1e2f;
--board-bg: #2e2e3e;
--cell-color: #ffffff;
--x-color: #ff4757;
--o-color: #1e90ff;
--modal-bg: rgba(0, 0, 0, 0.8);
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
--transition-speed: 0.3s;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: var(--font-family);
}
body {
background-color: var(--bg-color);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: var(--cell-color);
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
animation: fadeIn 1s ease-in-out;
}
h1 {
margin-bottom: 20px;
font-size: 2.5rem;
text-transform: uppercase;
letter-spacing: 2px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 15px;
background-color: var(--board-bg);
padding: 15px;
border-radius: 15px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.cell {
background-color: var(--bg-color);
display: flex;
justify-content: center;
align-items: center;
font-size: 2.5rem;
cursor: pointer;
position: relative;
transition: transform var(--transition-speed) ease;
}
.cell:hover {
transform: scale(1.05);
background-color: #3e3e50;
}
.cell.disabled {
cursor: not-allowed;
opacity: 0.7;
}
.x::before,
.o::before {
content: '';
position: absolute;
width: 80%;
height: 80%;
border-radius: 50%;
animation: popIn 0.3s ease forwards;
}
.x::before {
border: 5px solid var(--x-color);
transform: rotate(45deg);
}
.o::before {
border: 5px solid var(--o-color);
}
.x::after {
content: '';
position: absolute;
width: 80%;
height: 5px;
background-color: var(--x-color);
transform: rotate(45deg);
animation: drawX 0.3s ease forwards;
}
.x::after {
content: '';
position: absolute;
width: 80%;
height: 5px;
background-color: var(--x-color);
transform: rotate(-45deg);
animation: drawX 0.3s ease forwards;
}
.o::before {
border: 5px solid var(--o-color);
animation: drawO 0.5s ease forwards;
}
@keyframes fadeIn {
from { opacity: 0; transform: scale(0.95); }
to { opacity: 1; transform: scale(1); }
}
@keyframes popIn {
from { transform: scale(0); }
to { transform: scale(1); }
}
@keyframes drawX {
from { width: 0; }
to { width: 80%; }
}
@keyframes drawO {
from { transform: scale(0); }
to { transform: scale(1); }
}
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: var(--modal-bg);
display: flex;
justify-content: center;
align-items: center;
visibility: hidden;
opacity: 0;
transition: opacity var(--transition-speed) ease, visibility var(--transition-speed) ease;
}
.modal.active {
visibility: visible;
opacity: 1;
}
.modal-content {
background-color: var(--board-bg);
padding: 30px;
border-radius: 10px;
text-align: center;
animation: slideDown 0.5s ease;
}
.modal-content h2 {
margin-bottom: 20px;
font-size: 2rem;
}
.modal-content button {
padding: 10px 20px;
background-color: var(--x-color);
border: none;
border-radius: 5px;
color: var(--cell-color);
font-size: 1rem;
cursor: pointer;
transition: background-color var(--transition-speed) ease;
}
.modal-content button:hover {
background-color: #ff6b81;
}
@keyframes slideDown {
from { transform: translateY(-50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.restart-btn {
margin-top: 20px;
padding: 10px 20px;
background-color: var(--o-color);
border: none;
border-radius: 5px;
color: var(--cell-color);
font-size: 1rem;
cursor: pointer;
transition: background-color var(--transition-speed) ease;
}
.restart-btn:hover {
background-color: #1e90ff;
}
@media (max-width: 500px) {
.board {
grid-template-columns: repeat(3, 80px);
grid-template-rows: repeat(3, 80px);
gap: 10px;
padding: 10px;
}
.cell {
font-size: 2rem;
}
h1 {
font-size: 2rem;
}
}
</style>
</head>
<body>
<div class="game-container">
<h1>Tic Tac Toe</h1>
<div class="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">Restart Game</button>
</div>
<div class="modal" id="modal">
<div class="modal-content">
<h2 id="modal-message">Player X Wins!</h2>
<button id="close-modal">Play Again</button>
</div>
</div>
<script>
const cells = document.querySelectorAll('.cell');
const modal = document.getElementById('modal');
const modalMessage = document.getElementById('modal-message');
const closeModalBtn = document.getElementById('close-modal');
const restartBtn = document.querySelector('.restart-btn');
let currentPlayer = 'X';
let board = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;
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]
];
const handleCellClick = (e) => {
const index = e.target.getAttribute('data-index');
if (board[index] !== '' || !gameActive) {
return;
}
updateCell(e.target, index);
checkResult();
};
const updateCell = (cell, index) => {
board[index] = currentPlayer;
cell.classList.add(currentPlayer.toLowerCase());
cell.classList.add('disabled');
};
const switchPlayer = () => {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
};
const checkResult = () => {
let roundWon = false;
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
roundWon = true;
highlightWinningCells(condition);
break;
}
}
if (roundWon) {
gameActive = false;
setTimeout(() => {
modalMessage.textContent = `Player ${currentPlayer} Wins!`;
modal.classList.add('active');
}, 300);
return;
}
if (!board.includes('')) {
gameActive = false;
setTimeout(() => {
modalMessage.textContent = `It's a Draw!`;
modal.classList.add('active');
}, 300);
return;
}
switchPlayer();
};
const highlightWinningCells = (condition) => {
condition.forEach(index => {
cells[index].style.backgroundColor = '#555';
});
};
const resetGame = () => {
currentPlayer = 'X';
board = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
cells.forEach(cell => {
cell.classList.remove('x', 'o', 'disabled');
cell.style.backgroundColor = 'var(--bg-color)';
});
modal.classList.remove('active');
};
cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
});
closeModalBtn.addEventListener('click', resetGame);
restartBtn.addEventListener('click', resetGame);
</script>
</body>
</html>