Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
<body translate="no">
    <div class="container-fluid">
        <h1>Tic Tac Toe</h1>
        <h4>You can't win this game, but you can still try</h4>
      <div class="choose">
      <h2 class="selectMark text-center">Choose X or О below:</h2>
    <div class="col-sm-12">
      <a class="btn selectX btn-success btn-large col-sm-6">X</a>
      <a class="btn selectO btn-warning col-sm-6">O</a>
    </div>
      </div>
        <div class="board text-center">
            <div class="row">
                <div class="cell" data-i=0 data-j=0></div>
                <div class="cell" data-i=0 data-j=1></div>
                <div class="cell" data-i=0 data-j=2></div>
            </div>
            <div class="row">
                <div class="cell" data-i=1 data-j=0></div>
                <div class="cell" data-i=1 data-j=1></div>
                <div class="cell" data-i=1 data-j=2></div>
            </div>
            <div class="row">
                <div class="cell" data-i=2 data-j=0></div>
                <div class="cell" data-i=2 data-j=1></div>
                <div class="cell" data-i=2 data-j=2></div>
                </br>
            </div>
            <div class="container col-sm-12 text-center">
                <a class="btn res btn-default col-sm-12">RESET</a>
            </div>
        </div>
    </div>
    </div>
              
            
!

CSS

              
                * {
    padding: 0;
    margin: 0;
    font-family: "Fascinate Inline", cursive;
    background-color: #eecb78;
}

h2 {
    padding-bottom: 20px;
    font-size: 45px;
}

h1 {
    padding-bottom: 20px;
    font-size: 50px;
    text-align: center;
}

.board {
    padding: 0;
    padding-left: 10px;
    margin: auto;
    width: 300px;
    height: 300px;
}

.cell {
    border: 3px solid #111;
    border-radius:5.5px;
    margin: 2px;
    height: 100px;
    width: 100px;
    text-align: center;
    font-size: 65px;
    float: left;
    background-color: #faebd7;
}

.cell:hover {
    background-color: white;
}

h4,
.choose {
    font-family: "Yanone Kaffeesatz", sans-serif;
    padding-bottom: 20px;
    font-size: 30px;
    text-align: center;
}

.res {
    padding: 10px;
    margin-right: 7px;
}

.btn {
    background-color: #85adad;
}

.conteiner {
    padding-top: 20px;
}
              
            
!

JS

              
                var huPlayer = "X";
var aiPlayer = "O";
var board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]];
$(document).ready(function () {
    $(".board, h4").hide();
    $(".selectMark").css("padding-top", "20px");
    $(".selectX").click(function () {
        huPlayer = "X";
        aiPlayer = "O";
        $(".choose").fadeOut("slow", function () {
            $(".board, h4").fadeIn(600);
        });
    });

    $(".selectO").click(function () {
        huPlayer = "O";
        aiPlayer = "X";
        $(".choose").fadeOut("slow", function () {
            $(".board, h4").fadeIn(600);
        });
    });
    //restart
    $(".res").click(function () {
        for (var i = 0; i < 3; i++) {
            for (var j = 0; j < 3; j++) {
                board[i][j] = " ";
                $(".cell[data-i=" + i + "][data-j=" + j + "]").html(" ");
                $(".board").fadeOut();
                $(".choose").fadeIn();
            }
        }
    });

    $(".cell").click(function () {
        var gameState = checkGameOver(board);
        var i = $(this).data("i");
        var j = $(this).data("j");

        if (board[i][j] !== " ") {
            return;
        } else if (gameState || gameState === null) {
            return;
        }

        $(this).html(huPlayer);
        board[i][j] = huPlayer;

        gameState = checkGameOver(board);
        if (gameState) {
            alert("Game Over. Winner is: " + gameState);
            return;
        } else if (gameState === null) {
            alert("Draw...");
            return;
        } else {
            var move = callAi();
            //move the ai player
            board[move.i][move.j] = aiPlayer;
            $(".cell[data-i=" + move.i + "][data-j=" + move.j + "]").html(aiPlayer);
        }
        gameState = checkGameOver(board);
        if (gameState) {
            alert("Game Over. Winner is: " + gameState);
        }
        gameState = checkGameOver(board);
    });

    function checkGameOver(board) {
        // make a copy of the board and check for win
        for (var i = 0; i < 3; i++) {
            if (
                board[i][0] !== " " &&
                board[i][0] === board[i][1] &&
                board[i][0] === board[i][2]
            ) {
                return board[i][0];
            }
        }
        // vertical
        for (var j = 0; j < 3; j++) {
            if (
                board[0][j] !== " " &&
                board[0][j] === board[1][j] &&
                board[0][j] === board[2][j]
            ) {
                return board[0][j];
            }
        }
        // diagonal - top left
        if (
            board[0][0] !== " " &&
            board[0][0] === board[1][1] &&
            board[0][0] === board[2][2]
        ) {
            return board[0][0];
        }
        // diagonal - bottom left
        if (
            board[2][0] !== " " &&
            board[2][0] === board[1][1] &&
            board[2][0] === board[0][2]
        ) {
            return board[2][0];
        }
        for (var i = 0; i < 3; i++) {
            for (var j = 0; j < 3; j++) {
                if (board[i][j] === " ") {
                    return false;
                }
            }
        }
        return null;
    }

    function callAi() {
        return minimax(board, 0, aiPlayer);
    }

    function minimax(newBoard, depth, player) {
        var gameState = checkGameOver(newBoard);

        if (gameState === false) {
            var values = [];
            for (var i = 0; i < 3; i++) {
                for (var j = 0; j < 3; j++) {
                    var testboard = JSON.parse(JSON.stringify(newBoard));
                    if (testboard[i][j] !== " ") continue;
                    testboard[i][j] = player;
                    var value = minimax(
                        testboard,
                        depth + 1,
                        player === huPlayer ? aiPlayer : huPlayer
                    );
                    values.push({
                        cost: value,
                        cell: {
                            i: i,
                            j: j
                        }
                    });
                }
            }
            if (player === aiPlayer) {
                var max = _.maxBy(values, function (v) {
                    return v.cost;
                });
                if (depth === 0) {
                    return max.cell;
                } else {
                    return max.cost;
                }
            } else {
                var min = _.minBy(values, function (v) {
                    return v.cost;
                });
                if (depth === 0) {
                    return min.cell;
                } else {
                    return min.cost;
                }
            }
        } else if (gameState === null) {
            return 0;
        } else if (gameState === huPlayer) {
            return depth - 10;
        } else if (gameState === aiPlayer) {
            return 10 - depth;
        }
    }
});

              
            
!
999px

Console