123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <main>
    <section>
      <div>
        <h2 class="text-center">Tic Tac Toe Game</h2>
        <table class="gameTable">
            <tr>
                <td id="a1"></td>
                <td id="a2"></td>
                <td id="a3"></td>
            </tr>
            <tr>
                <td id="b1"></td>
                <td id="b2"></td>
                <td id="b3"></td>
            </tr>
            <tr>
                <td id="c1"></td>
                <td id="c2"></td>
                <td id="c3"></td>
            </tr>
        </table>
      </div>
        <footer class="hide"></footer>
    </section>
    <aside>
        <form>
            <label>Choose to play:</label><br>
            <input class="setSign" id="o" name="setSign" type="radio" value="o" autocomplete="off">
            <i class="fa fa-circle-o"></i>
            <input class="setSign" id="x" name="setSign" type="radio" value="x" autocomplete="off">
            <i class="fa fa-times"></i>
        </form>
        <button class="hard-reset">Reset All</button>
    </aside>
</main>
            
          
!
            
              
.text-center {
    text-align:center;
}

main {
    display: table;
    margin: 0 auto;
}

section {
    float: left;
}

aside {
    float: left;
    margin: 118px 20px;
}

table {
    border-collapse: collapse;
    text-align: center;
    vertical-align: middle;
    border-style: hidden;
}

tr, td {
    border: 7px solid #444446;
    width: 100px;
    height: 100px;
    transition: background-color .2s;
    box-shadow: 10px 9px 166px 8px rgba(158, 158, 158, 1);
}

td:hover {
    background-color: #5b5b5b;
    cursor: pointer;
}

td.winnerLine {
    background-color: #5b5b5b;
    transition: opacity 3s linear;
    opacity: 0.5;
}

td > .fa-circle-o {
    color: #444446;
    font-size: 5em;
}

td > .fa-times {
    color: #B62434;
    font-size: 5em;
}

form > label {
    line-height: 150%;
    font-weight: bold;
}

form > .fa {
    color: #5b5b5b;
    font-size: 1.8em;
}

button.hard-reset {
    width: 125px;
}

footer {
    text-align: center;
    padding-top: 10px;
    font-weight: bold;
    font-size: 150%;
}

footer.show {

    opacity: 1;
    transition: opacity 2s linear;
}

footer.hide {
    opacity: 0;
    transition: opacity 2s linear;
}
            
          
!
            
              document.addEventListener("DOMContentLoaded", function () {
    var signPlayer, signAI;
    var signValue = document.getElementsByClassName("setSign");
    var signO = '<i class="fa fa-circle-o"></i>',
        signX = '<i class="fa fa-times"></i>';
    var footer = document.getElementsByTagName('footer')[0];
    for (i = 0; i < signValue.length; i++) {
        signValue[i].addEventListener("click", function () {
            signPlayer = this.value;
        });
    }
    var tableElem = document.getElementsByTagName("td");
    console.log(tableElem);
    var board = ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'];

    var Reset = document.getElementsByClassName('hard-reset')[0];
    Reset.addEventListener('click', resetAll);

    for (var i = 0; i < tableElem.length; i++) {
        tableElem[i].addEventListener("click", function () {
            if ((signPlayer) && !(this.innerHTML) && (footer.className === "hide")) {
                this.innerHTML = (signPlayer === "o") ? signO : signX;
                signAI = (signPlayer === 'x') ? 'o' : 'x';
                switch (this.id) {
                    case "a1":
                        board[0] = signPlayer;
                        break;
                    case "a2":
                        board[1] = signPlayer;
                        break;
                    case "a3":
                        board[2] = signPlayer;
                        break;
                    case "b1":
                        board[3] = signPlayer;
                        break;
                    case "b2":
                        board[4] = signPlayer;
                        break;
                    case "b3":
                        board[5] = signPlayer;
                        break;
                    case "c1":
                        board[6] = signPlayer;
                        break;
                    case "c2":
                        board[7] = signPlayer;
                        break;
                    case "c3":
                        board[8] = signPlayer;
                        break;
                }
                makeMove();
            }
        })
    }

    function makeMove() {
        if (game.over(board)[0] === true) {
            footer.innerHTML = "DRAW!";
            footer.className = "show";
            restartGame();
            return;
        }
        var positionAiCoord = getMove();
        tableElem[positionAiCoord].innerHTML = (signAI === 'o') ? signO : signX;
        board[positionAiCoord] = signAI;
        if (game.over(board)[0] === "x") {
            var arr = game.over(board)[1];
            for (var i = 0; i < arr.length; i++) {
                tableElem[arr[i]].className = "winnerLine";
            }
            footer.innerHTML = "The Winner is X";
            footer.className = "show";
            restartGame();
        } else if (game.over(board)[0] === "o") {
            footer.innerHTML = "The Winner is O";
            footer.className = "show";
            restartGame();
        }
    }

    function hideFooter() {
        for (i = 0; i < tableElem.length; i++) {
            tableElem[i].innerHTML = "";
            tableElem[i].className = "";
        }
        footer.className = "hide";
    }

    function restartGame() {
        board = ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'];
        setTimeout(hideFooter, 750);

    }

    function resetAll() {
        board = ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'];
        hideFooter();
    }

    var game = {
        over: function (board) {

            for (var i = 0; i < board.length; i += 3) {
                if ((board[i] === board[i + 1] && board[i + 1] === board[i + 2]) &&
                    (board[i] !== 'e')) {
                    return [board[i], [i, i + 1, i + 2]];
                }
            }
            for (var j = 0; j < 3; j++) {
                if ((board[j] === board[j + 3] && board[j + 3] === board[j + 6]) &&
                    (board[j] !== 'e')) {
                    return [board[j], [j, j + 3, j + 6]];
                }
            }

            if ((board[4] === board[0] && board[4] === board[8]) &&
                (board[4] !== 'e')) {
                return [board[4], [0, 4, 8]];
            }
            if ((board[4] === board[2] && board[4] === board[6]) &&
                (board[4] !== 'e')) {
                return [board[4], [2, 4, 6]];
            }
            return [board.every(function (element) {
                return element !== 'e';
            })];
        },
        possible_moves: function (board, sign) {
            var testBoard = [],
                nextBoard;
            for (var i = 0; i < board.length; i++) {
                nextBoard = board.slice();
                if (nextBoard[i] === 'e') {
                    nextBoard[i] = sign;
                    testBoard.push(nextBoard);
                }
            }
            return testBoard;
        }
    };

    function moveScore(board) {
        var result = game.over(board)[0];
        if (result === signPlayer) {
            return -100;
        }
        if (result === signAI) {
            return +100;
        }
        return 0;//Game is a draw
    }

    function max(board) {

        if (game.over(board)[0]) {
            return [moveScore(board), []];
        }
        var newGame = [];
        var bestMove = [];
        var score;
        var best_score = -Infinity;
        var movesArray = game.possible_moves(board, signAI);

        for (var i = 0; i < movesArray.length; i++) {
            newGame = movesArray[i].slice();
            score = min(newGame)[0];
            if (score > best_score) {
                best_score = score;
                bestMove = newGame;
            }
        }
        return [best_score, bestMove];
    }

    function min(board) {

        if (game.over(board)[0]) {
            return [moveScore(board), []];
        }
        var newGame = [];
        var worstMove = [];
        var score;
        var worst_score = +Infinity;
        var movesArray = game.possible_moves(board, signPlayer);

        for (var i = 0; i < movesArray.length; i++) {
            newGame = movesArray[i].slice();
            score = max(newGame)[0];
            if (score < worst_score) {
                worst_score = score;
                worstMove = newGame;
            }
        }
        return [worst_score, worstMove];
    }

    function getMove() {
        var getBestMove = max(board)[1];
        console.log(getBestMove);
        for (var i = 0; i < getBestMove.length; i++) {
            if (getBestMove[i] !== board[i]) {
                return i;
            }
        }
    }
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console