DEV Community

Cover image for Tic-Tac-Toe Game using html css and javascript code . Follow us on instagram
Prince
Prince

Posted on

Tic-Tac-Toe Game using html css and javascript code . Follow us on instagram

<!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 Game</title>
    <!-- <link rel="stylesheet" href="style.css"> -->
     <style>
        * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  /* background: linear-gradient(135deg, #1e1e1e, #4e4e4e);
   */
  background-image: url("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEhUSEhIVFRUVFxUVFRcXFRUVFRUVFRcXFhcVFRUYHSggGB0lHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQFy0fHyUrLS0tLS0tLS0rLS0tLS0tLS0tLi0tLS0tLSstLS0tLS0tLS0tLS0tLS0tLS0tLS0tK//AABEIAKMBNgMBIgACEQEDEQH/xAAaAAACAwEBAAAAAAAAAAAAAAACAwABBAUG/8QANBAAAgECAggGAQQBBQEAAAAAAAECAxEEIRIxQVFxgZHwBWGhscHR8SIyUuETFUJicqIU/8QAGgEAAwEBAQEAAAAAAAAAAAAAAQIDBAAFBv/EACMRAAMAAgICAwADAQAAAAAAAAABAgMREiEEMRNBUSIysTP/2gAMAwEAAhEDEQA/AHINIpIOKPrTESwLQwpoKChMkJkaJITMoikiJC9eS1j403J2Suzq4HBKGbze1/R15FCLJHArQcXZ5Mzykdbx2nqnyfx8nFlIrjfKdlFJU5CZyJOQipIqpHUg1JGWrIZUkZakh1JoiRNSQphSZVgtGqUUQuxLC6GKIWQGjgoGqkZImqiKydo2U2aYPu5kps002SZjtGmLHRZmix8WTZnpDosbFiYjYiiNDYjYiYMdE4VoZFDEgIjYoAjQUUMSKig0gCMiRLBpEscILsC0aIUr8C8RBW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7Nkapaq7jOu/6DRBwiXFBSbIC35ECcVNe4DCfffIDiMh0BIpoJ9/BTQ6Y6YJGi7EG2NskIXdjYog0KeXH27Zoitu4nVh5FNWy6hRs9evvWCEkKdyDURkUVB7GMSByF5FJBpFIJA2K6LSMeK/d0NqMOJf6ny9kNjfYrYsy+I08lLd7Pv1NQNaN4tb0y81pgmtPZxWQjZRq2bORCiyhjuQVGei09ev1VsjdH02Pec404apbJ6vZ7xLX2TqjdEZSWfDPoJGxdot78uS7XQzUwJlVZXYmTLchcmcHZGc2vLSk304LJG2rKyb5Ln/VzCUj9BsBItIKxIods7ZcUMgCkMjvEbFbGx9R8TPTHRI0Ix0ZBxYtMKOZNiBWIS1+/ogoAWDYKWrv1sVx9rDIZAtghXIMMDYZShd22bfoA1UForPmCq0jtjIhyyyKi9vfeYJH7DsJBA3DG2DZcRkp2QEUC3fMX2DY1Ve7IJVe8hCDQdIXY5VTDOV23vDrz2dRKY8LXYrYVywEyXKCnIrK0mvNgMdjMpsQapfRqVdEKIUx9h5BINARDA2I2aKFXY+T+DViJWyusstaXH1uc0FkXG3s5M2Sqx334J/IqWIW59UvszlNh4IIVarey1Wz/ACLZbIg+jiiygjmzi0hkUAg0Izg4MckKTDXbJsRjUGBwCv3lvJihpohL2KAAvv3Akvn5LIFDA7O/Mu2fQhAnFxXs/QkZPvgWQV+woOpNrR5epcZPLmQgq9HFymw9N79pCHALlN21i1N5EIcgMNzeXewvTe8hAgFz1t+ZCEHQhHqJ37kIMA5uP/dyXsZmQhon0i8+imWyEHGL+voJshAMQhCEAMge/UqTy5FEOCR99C39+xCAOI9vexlSdiEFYQ9ve4al7EIKwMalmuLChq5IhBGKwo6r8BhCEwBvUQhBQH//2Q==");
}

.container {
  text-align: center;
  backdrop-filter: blur(15px);
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  max-width: 400px;
  width: 90%;
}

h1 {
  color: #fff;
  margin-bottom: 20px;
  text-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
}

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.cell {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  cursor: pointer;
  font-size: 5rem;
  color: #fff;
  transition: background 0.3s, transform 0.2s;
}

.cell:hover {
  background: rgba(255, 255, 255, 0.4);
}

.cell:active {
  transform: scale(0.95);
}

#reset {
  padding: 10px 20px;
  background: #06d6a0;
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.3s;
}

#reset:hover {
  background: #05c18a;
}

#message {
  margin-top: 10px;
  color: #fff;
  font-size: 1.2rem;
}

     </style>
</head>
<body>
    <div class="container">
        <h1>Tic-Tac-Toe</h1>
        <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">Reset Game</button>
        <p id="message"></p>
    </div>
    <script >
        const board = document.getElementById('board');
const cells = document.querySelectorAll('.cell');
const resetButton = document.getElementById('reset');
const message = document.getElementById('message');

let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
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 = (event) => {
    const clickedCell = event.target;
    const cellIndex = clickedCell.getAttribute('data-index');

    if (gameState[cellIndex] !== '' || !gameActive) return;

    gameState[cellIndex] = currentPlayer;
    clickedCell.textContent = currentPlayer;

    if (checkWinner()) {
        message.textContent = `Player ${currentPlayer} Wins! 🎉`;
        gameActive = false;
    } else if (gameState.every(cell => cell !== '')) {
        message.textContent = `It's a Draw! 😶`;
        gameActive = false;
    } else {
        currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
        message.textContent = `Player ${currentPlayer}'s Turn`;
    }
};

const checkWinner = () => {
    return winningConditions.some(combination => {
        return combination.every(index => gameState[index] === currentPlayer);
    });
};

const resetGame = () => {
    gameState = ['', '', '', '', '', '', '', '', ''];
    gameActive = true;
    currentPlayer = 'X';
    message.textContent = `Player X's Turn`;
    cells.forEach(cell => cell.textContent = '');
};

cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);

message.textContent = `Player X's Turn`;

    </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)