write tic tac teo game using html and css and js modern with animations in one file
- User
write tic tac teo game using html and css and js modern with animations in one file
- Assistant
write tic tac teo game using html and css and js modern with animations in one file
Sure! Below is a complete, self-contained HTML file that implements a modern Tic Tac Toe game using HTML, CSS, and JavaScript. The game includes responsive design and smooth animations to enhance the user experience.
You can copy the following code into a .html file and open it in your browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Tic Tac Toe</title>
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #f0f0f0, #c0c0c0);
}
.container {
text-align: center;
}
h1 {
margin-bottom: 20px;
color: #333;
}
#game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 10px;
margin: 0 auto;
}
.cell {
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
display: flex;
justify-content: center;
align-items: center;
font-size: 2.5rem;
cursor: pointer;
position: relative;
transition: transform 0.2s ease-in-out, background 0.3s;
}
.cell:hover {
transform: scale(1.05);
background: #f9f9f9;
}
.cell.X::before {
content: "X";
color: #e74c3c;
animation: fadeIn 0.3s ease-in-out;
}
.cell.O::before {
content: "O";
color: #3498db;
animation: fadeIn 0.3s ease-in-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: scale(0.5); }
to { opacity: 1; transform: scale(1); }
}
.message {
margin-top: 20px;
font-size: 1.2rem;
color: #555;
}
.restart-btn {
margin-top: 15px;
padding: 10px 20px;
font-size: 1rem;
background: #3498db;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
.restart-btn:hover {
background: #2980b9;
}
@media (max-width: 500px) {
#game {
grid-template-columns: repeat(3, 80px);
grid-template-rows: repeat(3, 80px);
gap: 8px;
}
.cell {
font-size: 2rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Tic Tac Toe</h1>
<div id="game">
<!-- 9 Cells -->
<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>
<div class="message" id="message">Player X's turn</div>
<button class="restart-btn" id="restartBtn">Restart Game</button>
</div>
<script>
(function() {
const cells = document.querySelectorAll('.cell');
const message = document.getElementById('message');
const restartBtn = document.getElementById('restartBtn');
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]
];
let board = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let isGameActive = true;
const handleCellClick = (e) => {
const index = e.target.getAttribute('data-index');
if (board[index] !== '' || !isGameActive) return;
updateCell(e.target, index);
checkResult();
};
const updateCell = (cell, index) => {
board[index] = currentPlayer;
cell.classList.add(currentPlayer);
cell.style.pointerEvents = 'none';
// Animation handled via CSS
};
const switchPlayer = () => {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
message.textContent = `Player ${currentPlayer}'s turn`;
};
const checkResult = () => {
let roundWon = false;
for (let condition of winningCombinations) {
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
roundWon = true;
highlightWinningCells(condition);
break;
}
}
if (roundWon) {
message.textContent = `Player ${currentPlayer} Wins! 🎉`;
isGameActive = false;
return;
}
if (!board.includes('')) {
message.textContent = `It's a Draw! 🤝`;
isGameActive = false;
return;
}
switchPlayer();
};
const highlightWinningCells = (condition) => {
condition.forEach(index => {
cells[index].style.background = currentPlayer === 'X' ? '#ffe6e6' : '#e6f0ff';
});
};
const restartGame = () => {
board = ['', '', '', '', '', '', '', '', ''];
isGameActive = true;
currentPlayer = 'X';
message.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => {
cell.classList.remove('X', 'O');
cell.style.background = '#fff';
cell.style.pointerEvents = 'auto';
});
};
cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
});
restartBtn.addEventListener('click', restartGame);
})();
</script>
</body>
</html>
Responsive Design: The game adapts to different screen sizes, ensuring a good user experience on both desktop and mobile devices.
Animations: Smooth scaling on hover and fade-in effects when players make a move enhance the visual appeal.
Game Logic: Implements all the standard rules of Tic Tac Toe, including checking for wins and draws.
User Feedback: Displays messages indicating the current player's turn, win notifications, and draw scenarios.
Restart Functionality: A "Restart Game" button allows players to reset the game at any time.
HTML Structure:
data-index attribute.CSS Styling:
.cell class defines the appearance of each cell, including hover effects and animations for displaying "X" and "O".JavaScript Functionality:
checkResult function evaluates the board after each move to determine if there's a winner or if the game ends in a draw.restartGame function resets the game state, allowing for a new game.Feel free to customize the game according to your preferences:
This implementation provides a solid foundation for a Tic Tac Toe game with modern design practices. It's easy to understand and extend, making it a great starting point for further enhancements or customization.
Enjoy playing!
Not yet included in a published release.