Skip to main content

HTML Code for Tic tac toe Game with beautiful interface

Use this html code to make your own Tic Tac Toe game. HTML Code for Tic tac toe Game with beautiful interface.

<!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 | Beautiful Game By Tipstv4you</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial Rounded MT Bold', Arial, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1e1e1e, #2d2d2d);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .scene {
            position: absolute;
            display: none;
            flex-direction: column;
            align-items: center;
            gap: 2rem;
        }

        .start-scene {
            animation: fadeIn 1s ease-out;
        }

        .game-container {
            background: rgba(255, 255, 255, 0.1);
            padding: 2rem;
            border-radius: 20px;
            box-shadow: 0 0 30px rgba(0, 0, 0, 0.3);
            backdrop-filter: blur(10px);
        }

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

        .cell {
            width: 100px;
            height: 100px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 15px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 3rem;
            color: white;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .cell:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: scale(1.05);
        }

        .cell.x {
            color: #ff4757;
        }

        .cell.o {
            color: #2ed573;
        }

        button {
            padding: 1rem 2rem;
            font-size: 1.2rem;
            border: none;
            border-radius: 10px;
            background: #2ed573;
            color: white;
            cursor: pointer;
            transition: all 0.3s ease;
            text-transform: uppercase;
            letter-spacing: 2px;
        }

        button:hover {
            transform: scale(1.1);
            box-shadow: 0 0 20px rgba(46, 213, 115, 0.5);
        }

        .result-scene {
            text-align: center;
            color: white;
            animation: slideUp 0.5s ease-out;
        }

        .result-text {
            font-size: 3rem;
            margin-bottom: 2rem;
            text-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-50px); }
            to { opacity: 1; transform: translateY(0); }
        }

        @keyframes slideUp {
            from { opacity: 0; transform: translateY(100px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .title {
            font-size: 4rem;
            color: white;
            text-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }

        .active {
            display: flex;
        }
    </style>
</head>
<body>
    <!-- Start Scene -->
    <div class="scene start-scene active">
        <h1 class="title">Tic Tac Toe</h1>
        <button onclick="startGame()">Start Game</button>
    </div>

    <!-- Game Scene -->
    <div class="scene game-scene">
        <div class="game-container">
            <div class="game-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 onclick="resetGame()">Restart Game</button>
        </div>
    </div>

    <!-- Result Scene -->
    <div class="scene result-scene">
        <div class="result-text" id="result-text">You Win! 🎉</div>
        <button onclick="resetGame()">Play Again</button>
    </div>

    <script>
        let currentPlayer = 'X';
        let gameBoard = ['', '', '', '', '', '', '', '', ''];
        let gameActive = true;

        function startGame() {
            document.querySelector('.start-scene').classList.remove('active');
            document.querySelector('.game-scene').classList.add('active');
            initializeGame();
        }

        function initializeGame() {
            const cells = document.querySelectorAll('.cell');
            cells.forEach(cell => {
                cell.classList.remove('x', 'o');
                cell.textContent = '';
                cell.addEventListener('click', handleCellClick);
            });
            gameBoard = ['', '', '', '', '', '', '', '', ''];
            currentPlayer = 'X';
            gameActive = true;
        }

        function handleCellClick(e) {
            const cell = e.target;
            const index = cell.dataset.index;

            if (gameBoard[index] !== '' || !gameActive) return;

            gameBoard[index] = currentPlayer;
            cell.textContent = currentPlayer;
            cell.classList.add(currentPlayer.toLowerCase());

            if (checkWin()) {
                showResult(`${currentPlayer} Wins! 🎉`);
                gameActive = false;
                return;
            }

            if (checkDraw()) {
                showResult("Draw! 🤝");
                gameActive = false;
                return;
            }

            currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
        }

        function checkWin() {
            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
            ];

            return winPatterns.some(pattern => 
                pattern.every(index => gameBoard[index] === currentPlayer)
            );
        }

        function checkDraw() {
            return gameBoard.every(cell => cell !== '');
        }

        function showResult(text) {
            document.querySelector('.game-scene').classList.remove('active');
            const resultScene = document.querySelector('.result-scene');
            document.getElementById('result-text').textContent = text;
            resultScene.classList.add('active');
        }

        function resetGame() {
            document.querySelector('.result-scene').classList.remove('active');
            startGame();
        }

        // Keyboard controls
        document.addEventListener('keydown', (e) => {
            if (e.key === 'r') resetGame();
            if (e.key === 'n') startGame();
        });
    </script>
</body>
</html>

Comments

Popular posts from this blog

Submit Your Website on all Search Engines to get Traffic on your website

  Submit Your Website on all Search Engines to get Traffic on your website In this article, I'm going to give you 63 Big Search engines to submit your website. after submit your website on these Search engines your website can Instantly start to get lots of traffic. you will have to submit your website on all search engines. if you will not submit your website on search engines then your website will not show in Search results.  yandex search engines like when someone will try to search your website on Google then your website will not show in search result. you will have to submit your website on Google Console. similarly I have collected all search Engines to submit your website and get free lots of traffic. read this full articles. 63+ Search Engines that I have collected to submit your website and get over 500K free traffic instantly Today..   Check out all these search engines -  1 -  ASR (Active Search Results ) 2 -  Google 3-  Amidalla 4 - ...

Earn Money From Google AdMob ( Make Money Online 2024 ) | PASSIVE INCOME 2024

Earn Passive Money From Google AdMob ( Make Money Online ) |  https://cpamarketingbeginners.blogspot.com/ Admob Earnings Earn Instantly Hundreds of dollars without doing hard work with Google AdMob. Google Ad Mob is a Google's Product. AdMob is A Just like AdSense. You can Earn Money From Your Website and YouTube Channel Using AdSense. But if you want to earn money from Your app and Game then you can Use AdMob.  Google Run Ads on Website, YouTube Channel, App and Game Application. when Someone Clicks on that ads then You can Earn money. You can Earn $1 - $5 only on one Click.  Earning depends on Traffic quality and Category. If you have made your website, YouTube Channel or App on Business category and If you are getting traffic from United states, United kingdom, Canada, Germany.... then you can earn high amount of money. In this article, I am going to share with you how you can earn money from admob. read this full article. Guys, First of all you will have to make game ...

Make $100 Online TODAY (EASY Method) [2024] | EARN MONEY

Would You like to earn $100 Per day? You should read this full articles. Because I am going to share with you best and trusted platform to earn your first and fast $100 today. Make $100 Online TODAY (EASY Method) [2024] | EARN MONEY You can see payments proof, All these people are cashing out their money every single day. You can earn up to $100 daily with this platform. This platform's name is shrtfly.com  . This is amazing platform. This platform is available worldwide so no matter what country you are in. You can use this platform to make money online. shrtfly platform have 1 Million Publishers. This platform show clean ads only. You can not get virus , Malware and adult type ads. It's a AdSense Compatible Platform.  How to earn $100 per day with shrtfly platform. You can Visit on these platform like mxplayer.in , YouTube.com, bilibili, Facebook or any social media platform and then copy URL Of that movies and reel. You can also copy website url. watch full tutorial ab...