cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              

    <div id="canvas">
        <div id="board">
            <div id="welcome">tic-tac-toe</div>
            <div id="player"></div>
            <div id="first-row" class="row">
                <div id="1" class="cell empty" data-number="1"></div>
                <div id="2" class="cell empty" data-number="2"></div>
                <div id="3" class="cell empty" data-number="3"></div>
            </div>
            <div id="seconds-row" class="row">
                <div id="4" class="cell empty" data-number="4"></div>
                <div id="5" class="cell empty" data-number="5"></div>
                <div id="6" class="cell empty" data-number="6"></div>
            </div>
            <div id="third-row" class="row">
                <div id="7" class="cell empty" data-number="7"></div>
                <div id="8" class="cell empty" data-number="8"></div>
                <div id="9" class="cell empty" data-number="9"></div>
            </div>
        </div>
        <div id="score-area">

            <div class="ind-area" id="player-user">
                <div class="score-area-title noselect">User</div>
                <div class="score-number" id="user-score">0</div>
            </div>
            <div id="delimiter">
                :
            </div>
            <div class="ind-area" id="player-cpu">
                <div class="score-number" id="cpu-score">0</div>
                <div class="score-area-title noselect">Computer</div>

            </div>

        </div>
    </div>



    <div id="dialog">
        <p style="text-align: center;">Select player</p>
    </div>
  
            
          
!
            
              * {
    margin: 0;
    padding: 0;
}

body {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
    /*background-image: url('https://source.unsplash.com/kktq8zzmPEo/1600x900');
    background-size: cover;*/
    background-color: #141215;
    color: #ffffff;
}

#canvas {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 10px;
}

#board {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #141215;
    min-height: 350px;
}

.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.cell {
    background-color: grey;
    min-width: 100px;
    min-height: 100px;
    text-align: center;
    vertical-align: middle;
    margin: 3px;
    line-height: 100px;
    cursor: pointer;
}

.cross {
    background-image: url('http://toastmasters.ru/wp-content/uploads/2016/04/Tic-tac-toe-cross.png');
    background-size: cover;
    pointer-events: none;
    cursor: default;
}

.nought {
    background-image: url('http://toastmasters.ru/wp-content/uploads/2016/04/Tic-tac-toe-nought-e1461667111145.png');
    background-size: cover;
    pointer-events: none;
    cursor: default;
}

#player {
    text-align: center;
}

#dialog {
    display: none;
}

#player {
    color: white;
    margin-bottom: 10px;
    margin-top: 10px;
}

#score-area {
    margin-top: 20px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    background-color: #00008B;
}

.ind-area {
    display: flex;
    flex-direction: row;
    color: white;
    min-width: 150px;
    justify-content: center;
}

#user-score {}

#cpu-score {}

.score-area-title {
    width: 80%;
    text-align: center;
}

.score-number {
    margin-left: 10px;
    margin-right: 10px;
}

#delimiter {}

.score-number {}

.freeze-all-cells {
    pointer-events: none;
}

.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset {
    float: none !important;
}

.ui-dialog .ui-dialog-buttonpane {
    text-align: center !important;
}

#player {
    color: white;
}

.noselect {
    -webkit-touch-callout: none;
    /* iOS Safari */
    -webkit-user-select: none;
    /* Safari */
    -khtml-user-select: none;
    /* Konqueror HTML */
    -moz-user-select: none;
    /* Firefox */
    -ms-user-select: none;
    /* Internet Explorer/Edge */
    user-select: none;
}
            
          
!
            
              $(document).ready(function() {

    var ttt = {
        /* is */
        userTurnToMove: true,
        step: 0,
        arrayWin: [],
        madeMove: false,
        userMoveCounter: 0,
        userPlaysCross: true,
        cpuMoveCounter: 0,
        isUserPlayedGame: false,
        victoriesCpu: 0,
        victoriesUser: 0
    };


    var initialPlayerSelect = function() {
        $("#dialog").dialog({
            closeOnEscape: false,
            open: function(event, ui) {
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-titlebar").hide();
            },
            autoOpen: true,
            resizable: false,
            height: "auto",
            width: 400,
            modal: true,
            buttons: {
                "Play X": function() {

                    $(this).dialog("close");
                    ttt.userTurnToMove = true;
                    $('#player').text('User starts');
                },
                "Play O": function() {
                    $(this).dialog("close");
                    ttt.userTurnToMove = false;
                    ttt.userPlaysCross = false;
                    $('#player').text('Computer starts');
                    attack();
                }
            },
            show: {
                effect: "fade",
                duration: 500
            },
            hide: {
                effect: "fade",
                duration: 500
            }
        });
    };

    initialPlayerSelect();

    console.log('userPlaysCross', ttt.userPlaysCross);

    function setup() {

        $('#board .cell').each(function(idx, el) {
            if ($(el).hasClass('cross')) {
                $(el).removeClass('cross');
            }
            if ($(el).hasClass('nought')) {
                $(el).removeClass('nought');
            }
            $(el).addClass('empty');
        });
        console.log('board prepared for new game');

        ttt.userMoveCounter = 0;
        ttt.cpuMoveCounter = 0;

        if (ttt.isUserPlayedGame) {
            console.log('Computer has to put a X already');
            ttt.userPlaysCross = false;
            ttt.userTurnToMove = false;
            $('#player').text('Computer starts');
            attack();
        } else {
            ttt.userPlaysCross = true;
            ttt.userTurnToMove = true;
            $('#player').text('User starts');
        }



    }

    $('.cell').click(function() {
        /**
         * Scenario when a user plays X
         */
        if (ttt.userTurnToMove && ttt.userPlaysCross) {
            //ttt.step++;
            //let cellNumber = $(this).data('number');

            if ($(this).hasClass('empty')) {
                $(this).removeClass('empty');
                $(this).addClass('cross');
                ttt.userMoveCounter++;

                if (checkForWin('cross')) {
                    setTimeout(function() {
                        alert('User has won');
                    }, 100);
                    setTimeout(function() {
                        ttt.isUserPlayedGame = true;
                        setup();
                        ttt.victoriesUser++;
                    }, 100);
                } else if (isAllCellsPlayed()) {
                    setTimeout(function() {
                        alert('Draw');
                    }, 100);
                    setTimeout(function() {
                        ttt.isUserPlayedGame = true;
                        setup();
                    }, 100);
                }

            }
            ttt.userTurnToMove = false;

            ttt.madeMove = true;
            /* Here is where Computer defends itself agains the user */
            if (ttt.madeMove && !ttt.userTurnToMove) {
                defend();
            }
            //ttt.isUserCross = false;
            /**
             * END of Scenario when a user plays X
             */

        } else if (ttt.userTurnToMove && !ttt.userPlaysCross) {

            if ($(this).hasClass('empty')) {
                $(this).removeClass('empty');
                $(this).addClass('nought');


                if (checkForWin('nought')) {
                    setTimeout(function() {
                        alert('Computer has won');

                    }, 100);
                    setTimeout(function() {
                        ttt.isUserPlayedGame = false;
                        setup();
                        ttt.victoriesUser++;
                    }, 100);
                } else if (isAllCellsPlayed()) {
                    setTimeout(function() {
                        alert('Draw');
                    }, 100);
                    setTimeout(function() {
                        ttt.isUserPlayedGame = false;
                        setup();
                    }, 100);
                }

            }

            ttt.userTurnToMove = false;

            ttt.madeMove = true;
            /* Here is where Computer defends itself agains the user */
            if (ttt.madeMove && !ttt.userTurnToMove) {
                attack();
            }
        }
    });

    function attack() {
        console.log('Computer attacks');
        var opportunityPosition = '';
        var dangerPosition = '';

        ttt.cpuMoveCounter++;
        console.log('ttt.cpuMoveCounter', ttt.cpuMoveCounter);
        if (ttt.cpuMoveCounter === 1) {
            if (isCellEmpty('#1')) {
                makeMove('#1', 'cross');
            }
        }

        if (ttt.cpuMoveCounter === 2) {

            if ($('#1').hasClass('cross') && (
                $('#2').hasClass('nought') ||
                $('#4').hasClass('nought') ||
                $('#6').hasClass('nought') ||
                $('#8').hasClass('nought'))) {
                makeMove('#5', 'cross');
            } else if (isCellEmpty('#7')) {
                makeMove('#7', 'cross');
            } else if (isCellEmpty('#9')) {
                makeMove('#9', 'cross');
            } else if (isCellEmpty('#9') && $('#1').hasClass('cross') && $('#3').hasClass('nought')) {
                makeMove('#9', 'cross');
            }

        }

        if (ttt.cpuMoveCounter > 2) {

            dangerPosition = forkAt('nought');
            opportunityPosition = forkAt('cross');

            if (opportunityPosition !== -1) {
                makeMove(opportunityPosition, 'cross');

            } else {
                if (dangerPosition !== -1) {
                    makeMove(dangerPosition, 'cross');
                } else {
                    if (isCellEmpty('#3') && isCornerClean('#3')) {
                        makeMove('#3', 'cross');
                    } else if (isCellEmpty('#7') && isCornerClean('#7')) {
                        makeMove('#7', 'cross');
                    } else if (isCellEmpty('#9')) {
                        makeMove('#9', 'cross');
                    } else if (isCellEmpty('#2')) {
                        makeMove('#2', 'cross');
                    } else if (isCellEmpty('#4')) {
                        makeMove('#4', 'cross');
                    } else if (isCellEmpty('#6')) {
                        makeMove('#6', 'cross');
                    } else if (isCellEmpty('#8')) {
                        makeMove('#8', 'cross');
                    } else if (isCellEmpty('#3')) {
                        makeMove('#3', 'cross');
                    } else if (isCellEmpty('#7')) {
                        makeMove('#7', 'cross');
                    } else if (isCellEmpty('#9')) {
                        makeMove('#9', 'cross');
                    }
                }
            }
        }

        setTimeout(function() {
            if (checkForWin('cross')) {
                setTimeout(function() {
                    alert('Computer has won');
                }, 1);
                setTimeout(function() {
                    ttt.isUserPlayedGame = false;
                    setup();
                    ttt.victoriesCpu++;
                    $('#cpu-score').text(ttt.victoriesCpu);
                }, 100);
            } else if (checkForWin('nought')) {
                setTimeout(function() {
                    alert('User has won');
                }, 1);
                setTimeout(function() {
                    ttt.isUserPlayedGame = false;
                    setup();
                    ttt.victoriesUser++;
                    $('#user-score').text(ttt.victoriesUser);

                }, 100);
            } else if (isAllCellsPlayed()) {
                setTimeout(function() {
                    alert('Draw');
                }, 100);
                setTimeout(function() {
                    ttt.isUserPlayedGame = false;
                    setup();
                }, 100);
            } else {
                console.log('Nobody wins so far!');
            }
        }, 100);

        ttt.userTurnToMove = true;
    }

    function defend() {
        console.log('Computer defends');
        var dangerPosition = '';
        var attackPosition = '';

        if (($('#1').hasClass('cross') ||
            $('#3').hasClass('cross') ||
            $('#7').hasClass('cross') ||
            $('#9').hasClass('cross')) &&
            ttt.userMoveCounter === 1) {
            if (isCellEmpty('#5')) {
                makeMove('#5', 'nought');
            //              ttt.userTurnToMove = true;
            }
        } else if (($('#4').hasClass('cross') ||
            $('#2').hasClass('cross') ||
            $('#6').hasClass('cross') ||
            $('#8').hasClass('cross')) &&
            ttt.userMoveCounter === 1) {
            if (isCellEmpty('#5')) {
                makeMove('#5', 'nought');
            //ttt.userTurnToMove = true;
            }
        } else if ($('#5').hasClass('cross') && ttt.userMoveCounter === 1) {
            if (isCellEmpty('#1')) {
                makeMove('#1', 'nought');
            //ttt.userTurnToMove = true;
            }
        }

        if (ttt.userMoveCounter === 2) {
            dangerPosition = forkAt('cross');
            if (dangerPosition === -1 && ($('#1').hasClass('cross') ||
                $('#3').hasClass('cross') ||
                $('#7').hasClass('cross') ||
                $('#9').hasClass('cross'))) {
                if (isCellEmpty('#2')) {
                    makeMove('#2', 'nought');
                } else if (isCellEmpty('#4')) {
                    makeMove('#4', 'nought');
                } else if (isCellEmpty('#6')) {
                    makeMove('#6', 'nought');
                } else if (isCellEmpty('#8')) {
                    makeMove('#8', 'nought');
                }
            } else {
                makeMove(dangerPosition, 'nought');
            }
        }

        if (ttt.userMoveCounter > 2) {
            dangerPosition = forkAt('cross');
            attackPosition = forkAt('nought');

            if (attackPosition !== -1) {
                makeMove(attackPosition, 'nought');
            } else if (dangerPosition === -1) {
                if (isCellEmpty('#1')) {
                    makeMove('#1', 'nought');
                } else if (isCellEmpty('#3')) {
                    makeMove('#3', 'nought');
                } else if (isCellEmpty('#9')) {
                    makeMove('#9', 'nought');
                } else if (isCellEmpty('#7')) {
                    makeMove('#7', 'nought');
                } else if (isCellEmpty('#2')) {
                    makeMove('#2', 'nought');
                } else if (isCellEmpty('#4')) {
                    makeMove('#4', 'nought');
                } else if (isCellEmpty('#6')) {
                    makeMove('#6', 'nought');
                } else if (isCellEmpty('#8')) {
                    makeMove('#8', 'nought');
                }
            } else {
                makeMove(dangerPosition, 'nought');
            }
        //ttt.userTurnToMove = true;
        }

        setTimeout(function() {
            if (checkForWin('nought')) {
                setTimeout(function() {
                    alert('Computer has won');
                }, 1);
                setTimeout(function() {
                    ttt.isUserPlayedGame = true;
                    setup();
                    ttt.victoriesCpu++;
                    $('#cpu-score').text(ttt.victoriesCpu);
                }, 100);
            } /*else if (isAllCellsPlayed()) {
                setTimeout(function() {
                    alert('draw');
                }, 100);
                setTimeout(function() {
                    ttt.isUserPlayedGame = true;
                    setup();

                }, 100);
            }*/
        }, 100);

        ttt.userTurnToMove = true;
    }


    function isCellEmpty(cellId) {
        if ($(cellId).hasClass('empty')) {
            return true;
        }
        return false;
    }

    function makeMove(cellNumber, player) {
        //setTimeout(function() {
        $(cellNumber).removeClass('empty');
        $(cellNumber).addClass(player);
        //}, 100);

    }

    function forkAt(player) {
        if ($('#1').hasClass(player) && $('#2').hasClass(player) && isCellEmpty('#3')) {
            return '#3';
        } else if ($('#2').hasClass(player) && $('#3').hasClass(player) && isCellEmpty('#1')) {
            return '#1';
        } else if ($('#1').hasClass(player) && $('#3').hasClass(player) && isCellEmpty('#2')) {
            return '#2'; /*----*/
        } else if ($('#4').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#6')) {
            return '#6';
        } else if ($('#5').hasClass(player) && $('#6').hasClass(player) && isCellEmpty('#4')) {
            return '#4';
        } else if ($('#4').hasClass(player) && $('#6').hasClass(player) && isCellEmpty('#5')) {
            return '#5'; /*----*/
        } else if ($('#7').hasClass(player) && $('#8').hasClass(player) && isCellEmpty('#9')) {
            return '#9';
        } else if ($('#8').hasClass(player) && $('#9').hasClass(player) && isCellEmpty('#7')) {
            return '#7';
        } else if ($('#7').hasClass(player) && $('#9').hasClass(player) && isCellEmpty('#8')) {
            return '#8'; /*----*/
        } else if ($('#1').hasClass(player) && $('#4').hasClass(player) && isCellEmpty('#7')) {
            return '#7';
        } else if ($('#4').hasClass(player) && $('#7').hasClass(player) && isCellEmpty('#1')) {
            return '#1';
        } else if ($('#1').hasClass(player) && $('#7').hasClass(player) && isCellEmpty('#4')) {
            return '#4'; /*----*/
        } else if ($('#2').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#8')) {
            return '#8';
        } else if ($('#2').hasClass(player) && $('#8').hasClass(player) && isCellEmpty('#5')) {
            return '#5';
        } else if ($('#8').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#2')) {
            return '#2'; /*----*/
        } else if ($('#3').hasClass(player) && $('#6').hasClass(player) && isCellEmpty('#9')) {
            return '#9';
        } else if ($('#3').hasClass(player) && $('#9').hasClass(player) && isCellEmpty('#6')) {
            return '#6';
        } else if ($('#9').hasClass(player) && $('#6').hasClass(player) && isCellEmpty('#3')) {
            return '#3'; /*----*/
        } else if ($('#7').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#3')) {
            return '#3';
        } else if ($('#7').hasClass(player) && $('#3').hasClass(player) && isCellEmpty('#5')) {
            return '#5';
        } else if ($('#3').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#7')) {
            return '#7'; /*----*/
        } else if ($('#1').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#9')) {
            return '#9';
        } else if ($('#1').hasClass(player) && $('#9').hasClass(player) && isCellEmpty('#5')) {
            return '#5';
        } else if ($('#9').hasClass(player) && $('#5').hasClass(player) && isCellEmpty('#1')) {
            return '#1';
        } else {
            return -1;
        }

    }

    function checkForWin(player) {
        if ($('#1').hasClass(player) && $('#2').hasClass(player) && $('#3').hasClass(player) ||
            ($('#4').hasClass(player) && $('#5').hasClass(player) && $('#6').hasClass(player)) ||
            ($('#7').hasClass(player) && $('#8').hasClass(player) && $('#9').hasClass(player)) ||
            ($('#1').hasClass(player) && $('#4').hasClass(player) && $('#7').hasClass(player)) ||
            ($('#2').hasClass(player) && $('#5').hasClass(player) && $('#8').hasClass(player)) ||
            ($('#3').hasClass(player) && $('#6').hasClass(player) && $('#9').hasClass(player)) ||
            ($('#1').hasClass(player) && $('#5').hasClass(player) && $('#9').hasClass(player)) ||
            ($('#3').hasClass(player) && $('#5').hasClass(player) && $('#7').hasClass(player))) {
            /*setTimeout(function() {
                alert(player + ' has won');
            }, 300);
            setTimeout(function() {
                setup();
            }, 1500);*/
            return true;
        } else {
            return false;
        }
    }

    function isAllCellsPlayed() {
        var playedCellCounter = 0;
        $('#board .cell').each(function(idx, el) {
            if ($(el).hasClass('cross') || $(el).hasClass('nought')) {
                playedCellCounter++;
            }
        });
        if (playedCellCounter === 9) {
            return true;
        }
        return false;
    }

    function isCornerClean(cellNumber) {
        if (cellNumber === '#3') {
            if ( ($('#2').hasClass('nought') || $('#6').hasClass('nought')) ) {
                return false;
            } else {
                return true;
            }
        }

        if (cellNumber === '#7') {
            if ( ($('#4').hasClass('nought') || $('#8').hasClass('nought')) ) {
                return false;
            } else {
                return true;
            }
        }
    }
});
            
          
!
999px
Loading ..................

Console