Spaces:
Running

droidbazaar / tictactoe.html
fxoryn's picture
make an simple app that plays tic tac toe
686844e verified
Raw History Blame Contribute Delete
4.87 kB
<!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>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
font-family: 'Poppins', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #111827;
color: white;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 10px;
}
.cell {
width: 100px;
height: 100px;
background-color: #1F2937;
display: flex;
justify-content: center;
align-items: center;
font-size: 3rem;
cursor: pointer;
border-radius: 8px;
transition: all 0.3s ease;
}
.cell:hover {
background-color: #374151;
}
.status {
margin-bottom: 20px;
font-size: 1.5rem;
font-weight: bold;
text-align: center;
}
</style>
</head>
<body>
<div class="game-container">
<h1 class="text-3xl font-bold mb-6 text-center">Tic Tac Toe</h1>
<div class="status" id="status">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 id="reset" class="mt-6 bg-blue-500 hover:bg-blue-600 text-white px-6 py-2 rounded-lg mx-auto block transition">Reset Game</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const board = document.getElementById('board');
const cells = document.querySelectorAll('.cell');
const status = document.getElementById('status');
const resetButton = document.getElementById('reset');
let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;
const winningConditions = [
[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
];
function handleCellClick(e) {
const clickedCell = e.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[clickedCellIndex] !== '' || !gameActive) return;
gameState[clickedCellIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;
checkResult();
}
function checkResult() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
if (gameState[a] === '' || gameState[b] === '' || gameState[c] === '') continue;
if (gameState[a] === gameState[b] && gameState[b] === gameState[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
status.textContent = `Player ${currentPlayer} wins!`;
gameActive = false;
return;
}
if (!gameState.includes('')) {
status.textContent = "Game ended in a draw!";
gameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
status.textContent = `Player ${currentPlayer}'s turn`;
}
function resetGame() {
currentPlayer = 'X';
gameState = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
status.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => cell.textContent = '');
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);
});
</script>
</body>
</html>