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.

            
              <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>
            
          
!
            
              * {
    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;
}
            
          
!
            
              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
🕑 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