Of course! Here is a complete, self-contained Tic-Tac-Toe game. You can copy and paste this code into a single index.html file and open it in your web browser to play.
Features
Sleek Dark Theme: Easy on the eyes with a modern, dark background.
Move Timer: Each player has 10 seconds to make a move. If the timer runs out, the turn is forfeited to the other player.
Clear Status Updates: Tells you whose turn it is, who won, or if it's a draw.
Responsive Design: Looks good on both desktop and mobile screens.
Clean, Commented Code: Easy to understand and modify.
index.html (The Complete Code)
html
<!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>
/* --- Basic Setup & Dark Theme --- */
:root {
--bg-color: #121212;
--cell-bg: #1e1e1e;
--cell-hover-bg: #2a2a2a;
--text-color: #e0e0e0;
--border-color: #333;
--x-color: #3498db; /* A nice blue for X */
--o-color: #e74c3c; /* A soft red for O */
--accent-color: #4CAF50; /* Green for buttons and status */
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
background-color: var(--bg-color);
color: var(--text-color);
}
/* --- Game Title --- */
h1 {
font-size: 3rem;
color: var(--text-color);
margin-bottom: 0.5em;
}
/* --- Game Board & Cells --- */
#gameBoard {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
width: 300px;
height: 300px;
}
.cell {
width: 100%;
height: 100%;
background-color: var(--cell-bg);
border: 2px solid var(--border-color);
border-radius: 8px;
display: flex;
justify-content: center;
align-items: center;
font-size: 4rem;
font-weight: bold;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
user-select: none; /* Prevents text selection on click */
}
.cell:hover {
background-color: var(--cell-hover-bg);
}
/* Apply colors for X and O */
.cell.x { color: var(--x-color); }
.cell.o { color: var(--o-color); }
/* --- Status, Timer, and Controls --- */
#gameInfo {
text-align: center;
margin-top: 20px;
}
#statusDisplay {
font-size: 1.5rem;
margin-bottom: 10px;
height: 30px; /* Reserve space to prevent layout shift */
}
#timerDisplay {
font-size: 1.2rem;
color: var(--accent-color);
height: 25px; /* Reserve space */
}
#restartButton {
margin-top: 20px;
padding: 12px 25px;
font-size: 1.1rem;
color: white;
background-color: var(--accent-color);
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.2s;
}
#restartButton:hover {
background-color: #45a049;
}
/* Media query for smaller screens */
@media (max-width: 400px) {
h1 { font-size: 2.5rem; }
#gameBoard { width: 240px; height: 240px; }
.cell { font-size: 3rem; }
#statusDisplay { font-size: 1.2rem; }
}
</style>
</head>
<body>
<h1>Tic-Tac-Toe</h1>
<div id="gameBoard">
<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 id="gameInfo">
<div id="statusDisplay">Player X's turn</div>
<div id="timerDisplay">Time left: 10s</div>
<button id="restartButton">Restart Game</button>
</div>
<script>
// --- DOM Element References ---
const statusDisplay = document.querySelector('#statusDisplay');
const timerDisplay = document.querySelector('#timerDisplay');
const cells = document.querySelectorAll('.cell');
const restartButton = document.querySelector('#restartButton');
// --- Game State Variables ---
let gameActive = true;
let currentPlayer = 'X';
let gameState = ["", "", "", "", "", "", "", "", ""];
// --- Timer Variables ---
const MOVE_TIME = 10; // Time per move in seconds
let timeLeft = MOVE_TIME;
let timerInterval = null;
// --- Winning Conditions (indices of the board) ---
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]
];
// --- Message Templates ---
const winningMessage = () => `Player ${currentPlayer} has won!`;
const drawMessage = () => `Game ended in a draw!`;
const currentPlayerTurn = () => `Player ${currentPlayer}'s turn`;
// --- Game Logic Functions ---
function startGame() {
gameActive = true;
currentPlayer = 'X';
gameState = ["", "", "", "", "", "", "", "", ""];
statusDisplay.innerHTML = currentPlayerTurn();
cells.forEach(cell => {
cell.innerHTML = "";
cell.classList.remove('x', 'o');
});
startTimer();
}
function handleCellClick(event) {
const clickedCell = event.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
// If cell is already played or game is over, do nothing
if (gameState[clickedCellIndex] !== "" || !gameActive) {
return;
}
// Process the move
handleCellPlayed(clickedCell, clickedCellIndex);
handleResultValidation();
}
function handleCellPlayed(clickedCell, clickedCellIndex) {
gameState[clickedCellIndex] = currentPlayer;
clickedCell.innerHTML = currentPlayer;
clickedCell.classList.add(currentPlayer.toLowerCase()); // Add 'x' or 'o' class for styling
}
function handleResultValidation() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const winCondition = winningConditions[i];
let a = gameState[winCondition[0]];
let b = gameState[winCondition[1]];
let c = gameState[winCondition[2]];
if (a === '' || b === '' || c === '') {
continue;
}
if (a === b && b === c) {
roundWon = true;
break;
}
}
if (roundWon) {
statusDisplay.innerHTML = winningMessage();
gameActive = false;
stopTimer();
timerDisplay.innerHTML = "Game Over!";
return;
}
// Check for a draw
let roundDraw = !gameState.includes("");
if (roundDraw) {
statusDisplay.innerHTML = drawMessage();
gameActive = false;
stopTimer();
timerDisplay.innerHTML = "Game Over!";
return;
}
// If we are here, the game continues. Change player.
handlePlayerChange();
}
function handlePlayerChange() {
currentPlayer = currentPlayer === "X" ? "O" : "X";
statusDisplay.innerHTML = currentPlayerTurn();
// Reset and start the timer for the new player
startTimer();
}
// --- Timer Functions ---
function startTimer() {
stopTimer(); // Clear any existing timer first
timeLeft = MOVE_TIME;
timerDisplay.innerHTML = `Time left: ${timeLeft}s`;
timerInterval = setInterval(() => {
timeLeft--;
timerDisplay.innerHTML = `Time left: ${timeLeft}s`;
if (timeLeft <= 0) {
stopTimer();
// Player ran out of time, forfeit turn
statusDisplay.innerHTML = `Player ${currentPlayer} ran out of time!`;
// Use a short delay before switching players so the message is visible
setTimeout(handlePlayerChange, 1500);
}
}, 1000);
}
function stopTimer() {
clearInterval(timerInterval);
}
// --- Event Listeners ---
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartButton.addEventListener('click', startGame);
// --- Initial Game Start ---
startGame();
</script>
</body>
</html>