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 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.

HTML

              
                <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Tic Tac Toe</title>
    <link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/dseg14" type="text/css" />
</head>

<body>
    <div class="container">
        <div class="row">
            <div id="info" class="black">Choose</div>
            <div id="play">
                <h3>
                    <a id="x" class="sb">X</a> 
                    or
                    <a id="o" class="sb">O</a>
                </h3>
            </div>
            <div id="again">
                <a id="clr" class="sb">
                    <h3>Play again?</h3>
                </a>
            </div>
        </div>
        <div class="row">
            <button id="b7" class="bt black h2 v0 d1"></button>
            <button id="b8" class="bt black h2 v1"></button>
            <button id="b9" class="bt black h2 v2 d0"></button>
        </div>
        <div class="row">
            <button id="b4" class="bt black h1 v0"></button>
            <button id="b5" class="bt black h1 v1 d0 d1"></button>
            <button id="b6" class="bt black h1 v2"></button>
        </div>
        <div class="row">
            <button id="b1" class="bt black h0 v0 d0"></button>
            <button id="b2" class="bt black h0 v1"></button>
            <button id="b3" class="bt black h0 v2 d1"></button>
        </div>
    </div>
    </div>
</body>
              
            
!

CSS

              
                body {
    background: #D1CED3;
}

.container {
    margin: 5% auto;
    display: block;
    width: 300px;
    height: 400px;
    border: black 3px solid;
    background: #000;
}

#again {
    height: 25%;
    display: none;
    text-align: center;
}

.row {
    height: 25%;
    display: block;
}

.bt {
    border: black 1px solid;
    width: 33.3%;
    margin: 0px;
    float: left;
    padding: 0;
    height: 100%;
    font-weight: bold;
    font-size: 1.3em;
}

.black {
    background: #333333;
    color: #D1CED3;
}

.bt:hover {
    background: #000;
    color: #0095FF;
}

.bt:active {
    background: #000;
    color: #D1CED3;
}

.bt:focus {
    outline: 0;
}

#play h3 {
    color: #D1CED3;
}

.sb {
    color: #D1CED3;
}

.sb:hover {
    color: #0095FF;
}

#info {
    text-align: center;
}

#play {
    text-align: center;
}

.won {
    color: #0095FF;
}
              
            
!

JS

              
                var cells = []; //cell values
var pval = ""; //player value
var cval = ""; //computer value
var endstate = false; //lock cells

document.addEventListener("DOMContentLoaded", function () {

    function q(query) { //quick querySelector
        return document.querySelector(query);
    }

    function qa(query) { //quick querySelectorAll
        return document.querySelectorAll(query);
    }

    clear();

    for (var i = 0; i < 9; i++) { //create button handlers
        handle("#b" + (i + 1), i);
    }

    q("#x").addEventListener("click", () => { //play as X
        pval = "X";
        cval = "O";
        q("#info").innerHTML = "Playing as X";
        q("#play").style.display = "none";
        q("#again").style.display = "none";
    });

    q("#o").addEventListener("click", () => { //play as O
        pval = "O";
        cval = "X";
        computerTurn();
        q("#info").innerHTML = "Playing as O";
        q("#play").style.display = "none";
        q("#again").style.display = "none";
    });

    q("#clr").addEventListener("click", () => { //clear button handler
        clear();
    });


    function handle(query, cell) { //button event handler
        q(query).addEventListener("click", () => { //button press
            if (cells[cell] == "" && endstate === false) {
                cells[cell] = pval;
                update();
                computerTurn();
            }
        });

    }

    function computerTurn() { //do computer turn
        var cell = null;
        var row = [];
        var col = [];
        var diagl = [];
        var diagr = [];
        if (endstate === false) {
            for (let i = 0; i < 3; i++) {
                row = [cells[i * 3], cells[i * 3 + 1], cells[i * 3 + 2]];
                col = [cells[i], cells[i + 3], cells[i + 6]];
                diagl = [cells[2], cells[4], cells[6]];
                diagr = [cells[0], cells[4], cells[8]];
                if ((row[0] == row[1] && row[0] !== "") || (row[0] == row[2] && row[0] !== "") || (row[1] == row[2] && row[1] !== "")) {
                    if (row.indexOf("") !== -1){
                        cell = i * 3 + row.indexOf("");
                        if(row.indexOf(cval) !== -1){
                            break;
                        }
                    }
                }
                else if ((col[0] == col[1] && col[0] !== "") || (col[0] == col[2] && col[0] !== "") || (col[1] == col[2] && col[1] !== "")){
                    if (col.indexOf("") !== -1){
                        cell = 3 * col.indexOf("") + i;
                        if(col.indexOf(cval) !== -1){
                            break;
                        }
                    }   
                }
                else if ((diagl[0] == diagl[1] && diagl[0] !== "") || (diagl[0] == diagl[2] && diagl[0] !== "") || (diagl[1] == diagl[2] && diagl[1] !== "")){
                    if (diagl.indexOf("") !== -1){
                        cell = 2 + 2 * diagl.indexOf("");
                        if(diagl.indexOf(cval) !== -1){
                            break;
                        }
                    }   
                } 
                else if ((diagr[0] == diagr[1] && diagr[0] !== "") || (diagr[0] == diagr[2] && diagr[0] !== "") || (diagr[1] == diagr[2] && diagr[1] !== "")){
                    if (diagr.indexOf("") !== -1){
                        cell = 4 * diagr.indexOf("");
                        if(diagr.indexOf(cval) !== -1){
                            break;
                        }
                    }   
                } 

            }
            if (cell == null){
                while (cells[cell] != "" && cells.indexOf("") !== -1) {
                    cell = Math.floor((Math.random() * 9));
                }
            }
            cells[cell] = cval;
        }
        update();
    }

    function update() { //update cells after a button press and a computer turn
        for (let i = 0; i < 9; i++) {
            q("#b" + (i + 1)).innerHTML = cells[i];
        }
        if (cells.indexOf("") == -1) { //check if there are any cells left
            endstate = true;
            q("#info").innerHTML = "Tie!";
            q("#again").style.display = "block";
        }
        for (let i = 0; i < 3; i++) { //check if there is a winner
            if (cells[i * 3] == cells[i * 3 + 1] && cells[i * 3] == cells[i * 3 + 2] && cells[i * 3] !== "") {
                endstate = true;
                qa(".h" + i).forEach((e) => {
                    e.style.color = "#0095FF";
                    e.style.background = "#000";
                });
                q("#info").innerHTML = cells[i * 3] + " wins!";
                q("#again").style.display = "block";
            }
            if (cells[i] == cells[i + 3] && cells[i] == cells[i + 6] && cells[i] !== "") {
                endstate = true;
                qa(".v" + i).forEach((e) => {
                    e.style.color = "#0095FF";
                    e.style.background = "#000";
                });
                q("#info").innerHTML = cells[i] + " wins!";
                q("#again").style.display = "block";
            }
            if (cells[0] == cells[4] && cells[0] == cells[8] && cells[0] != "") {
                endstate = true;
                qa(".d0").forEach((e) => {
                    e.style.color = "#0095FF";
                    e.style.background = "#000";
                });
                q("#info").innerHTML = cells[0] + " wins!";
                q("#again").style.display = "block";
            }
            if (cells[2] == cells[4] && cells[2] == cells[6] && cells[2] != "") {
                endstate = true;
                qa(".d1").forEach((e) => {
                    e.style.color = "#0095FF";
                    e.style.background = "#000";
                });
                q("#info").innerHTML = cells[2] + " wins!";
                q("#again").style.display = "block";
            }
        }
    }

    function clear() { //clear everything
        //...
        q("#info").innerHTML = "Choose";
        q("#play").style.display = "block";
        q("#again").style.display = "none";
        qa(".h0, .h1, .h2, .v0, .v1, .v2, .d0, .d1").forEach((e) => {
            e.style.color = null;
            e.style.background = null;
        });
        endstate = false;
        cells = ["", "", "", "", "", "", "", "", ""];
        pval = "";
        cval = "";
        update();
    }

});
              
            
!
999px

Console