code in a html a tic-tac-toe game
- User
code in a html a tic-tac-toe game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code in a html a tic-tac-toe game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll create a complete, responsive Tic-Tac-Toe game with modern styling, game state tracking, and animations. The solution will be contained in a single HTML file.
<!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>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
padding: 20px;
}
.container {
background-color: rgba(255, 255, 255, 0.95);
border-radius: 20px;
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
padding: 30px;
max-width: 500px;
width: 100%;
text-align: center;
transition: transform 0.3s ease;
}
.container:hover {
transform: translateY(-5px);
}
h1 {
color: #2c3e50;
margin-bottom: 20px;
font-size: 2.5rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}
.game-info {
margin: 20px 0;
padding: 15px;
background: #f8f9fa;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.status {
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 10px;
color: #2c3e50;
min-height: 30px;
}
.scores {
display: flex;
justify-content: space-around;
margin: 15px 0;
}
.score-card {
background: #3498db;
color: white;
padding: 10px 20px;
border-radius: 10px;
font-weight: bold;
min-width: 100px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.score-card.x {
background: #e74c3c;
}
.score-card.o {
background: #3498db;
}
.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 12px;
margin: 25px auto;
max-width: 400px;
}
.cell {
aspect-ratio: 1;
background: #fff;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 3.5rem;
font-weight: bold;
cursor: pointer;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.cell:before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(45deg, #f5f7fa, #e4e7eb);
z-index: -1;
}
.cell:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}
.cell.x {
color: #e74c3c;
}
.cell.o {
color: #3498db;
}
.cell.win {
animation: winAnimation 1s infinite;
}
@keyframes winAnimation {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.controls {
display: flex;
justify-content: center;
gap: 15px;
margin-top: 20px;
}
button {
padding: 12px 25px;
font-size: 1rem;
font-weight: bold;
border: none;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
button:hover {
transform: translateY(-3px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.25);
}
#restart-btn {
background: #2ecc71;
color: white;
}
#restart-btn:hover {
background: #27ae60;
}
#reset-scores {
background: #e67e22;
color: white;
}
#reset-scores:hover {
background: #d35400;
}
.message {
position: fixed;
top: 0;
left: 0;
right: 0;
background: #2ecc71;
color: white;
padding: 15px;
font-weight: bold;
text-align: center;
transform: translateY(-100%);
transition: transform 0.4s ease;
z-index: 100;
}
.message.show {
transform: translateY(0);
}
.message.error {
background: #e74c3c;
}
footer {
margin-top: 25px;
color: #7f8c8d;
font-size: 0.9rem;
}
@media (max-width: 500px) {
.container {
padding: 20px;
}
.cell {
font-size: 2.5rem;
}
.controls {
flex-direction: column;
align-items: center;
}
button {
width: 100%;
max-width: 300px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Tic-Tac-Toe</h1>
<div class="game-info">
<div class="status">Player X's turn</div>
<div class="scores">
<div class="score-card x">Player X: <span id="score-x">0</span></div>
<div class="score-card o">Player O: <span id="score-o">0</span></div>
</div>
</div>
<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>
<div class="controls">
<button id="restart-btn">New Game</button>
<button id="reset-scores">Reset Scores</button>
</div>
<footer>
Created with HTML, CSS, and JavaScript
</footer>
</div>
<div class="message" id="message"></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Game state
let currentPlayer = 'X';
let gameBoard = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;
let scores = { X: 0, O: 0 };
// DOM elements
const cells = document.querySelectorAll('.cell');
const statusDisplay = document.querySelector('.status');
const scoreXDisplay = document.getElementById('score-x');
const scoreODisplay = document.getElementById('score-o');
const restartButton = document.getElementById('restart-btn');
const resetScoresButton = document.getElementById('reset-scores');
const messageElement = document.getElementById('message');
// Winning combinations
const winPatterns = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
[0, 4, 8], [2, 4, 6] // Diagonals
];
// Messages
const winMessages = {
'X': 'Player X wins!',
'O': 'Player O wins!'
};
// Initialize the game
function initGame() {
gameBoard = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
currentPlayer = 'X';
updateStatus(`${currentPlayer}'s turn`);
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('x', 'o', 'win');
});
hideMessage();
}
// Update game status
function updateStatus(message) {
statusDisplay.textContent = message;
}
// Show message
function showMessage(message, isError = false) {
messageElement.textContent = message;
messageElement.classList.remove('error');
if (isError) {
messageElement.classList.add('error');
}
messageElement.classList.add('show');
setTimeout(hideMessage, 3000);
}
// Hide message
function hideMessage() {
messageElement.classList.remove('show');
}
// Handle cell click
function handleCellClick(e) {
const cell = e.target;
const index = parseInt(cell.getAttribute('data-index'));
// Check if cell is empty and game is active
if (gameBoard[index] !== '' || !gameActive) {
if (gameBoard[index] !== '') {
showMessage('This cell is already taken!', true);
}
return;
}
// Update game board and UI
gameBoard[index] = currentPlayer;
cell.textContent = currentPlayer;
cell.classList.add(currentPlayer.toLowerCase());
// Check for win or draw
if (checkWin()) {
endGame(false);
} else if (isBoardFull()) {
endGame(true);
} else {
// Switch player
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
updateStatus(`${currentPlayer}'s turn`);
}
}
// Check for win
function checkWin() {
return winPatterns.some(pattern => {
const [a, b, c] = pattern;
return (
gameBoard[a] !== '' &&
gameBoard[a] === gameBoard[b] &&
gameBoard[a] === gameBoard[c]
);
});
}
// Check for draw
function isBoardFull() {
return gameBoard.every(cell => cell !== '');
}
// End the game
function endGame(isDraw) {
gameActive = false;
if (isDraw) {
updateStatus('Game ended in a draw!');
showMessage('Game ended in a draw!');
} else {
// Update scores
scores[currentPlayer]++;
updateScores();
// Highlight winning cells
highlightWinningCells();
updateStatus(winMessages[currentPlayer]);
showMessage(winMessages[currentPlayer]);
}
}
// Highlight winning cells
function highlightWinningCells() {
for (const pattern of winPatterns) {
const [a, b, c] = pattern;
if (
gameBoard[a] !== '' &&
gameBoard[a] === gameBoard[b] &&
gameBoard[a] === gameBoard[c]
) {
cells[a].classList.add('win');
cells[b].classList.add('win');
cells[c].classList.add('win');
break;
}
}
}
// Update score display
function updateScores() {
scoreXDisplay.textContent = scores.X;
scoreODisplay.textContent = scores.O;
}
// Reset scores
function resetScores() {
scores = { X: 0, O: 0 };
updateScores();
showMessage('Scores have been reset!');
}
// Event listeners
cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
});
restartButton.addEventListener('click', initGame);
resetScoresButton.addEventListener('click', resetScores);
// Initialize the game
initGame();
});
</script>
</body>
</html>
Modern UI Design:
Game Functionality:
User Experience:
Additional Features:
Simply copy this entire code into an HTML file and open it in any modern browser to start playing!