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

              
                <body>
    <h1>Play Tic Tac Toe</h1>
    <div class="spacer"></div>
    <div class="block" id="mode">
        <h1 class="text-center">Choose Player Mode</h1>
        <hr>
        <div class="row text-center">
            <div class="col-sm-6">
                <div class="btn btn-lg btn-default" id="twoplayer">Two Players</div>
            </div>
            <div class="col-sm-6">
                <div class="btn btn-lg btn-default" id="computerplay">Computer Play</div>
            </div>
        </div>
    </div>
    <div class="spacer"></div>
    <div class="block" id="msg">
        <h3>For Computer Play</h3>
        <h3>Player is always "X"</h3>
        <h3>Player always goes first</h3>
        <div class="btn btn-lg btn-default" id="msgbtn">Press to Continue</div>
    </div>
    <div class="spacer"></div>
    <div class="block" id="xo">
        <h1 class="text-center">Choose X or O</h1>
        <hr>
        <div class="row text-center">
            <div class="col-sm-6">
                <div class="btn btn-lg btn-default" id="ex">X</div>
            </div>
            <div class="col-sm-6">
                <div class="btn btn-lg btn-default" id="oh">O</div>
            </div>
        </div>
    </div>
    <div class="spacer"></div>
    <div class="block" id="gmbd">
        <div id="gameboard">
            <div class="row">
                <div class="square bottom right sq1"></div>
                <div class="square bottom right sq2"></div>
                <div class="square bottom sq3"></div>
            </div>
            <div class="row">
                <div class="square bottom right sq4"></div>
                <div class="square bottom right sq5"></div>
                <div class="square bottom sq6"></div>
            </div>
            <div class="row">
                <div class="square right sq7"></div>
                <div class="square right sq8"></div>
                <div class="square sq9"></div>
            </div>
        </div>
        <div class="spacer"></div>
        <div class="btn btn-lg btn-default" id="replay">Replay</div>
    </div>
    
</body>
              
            
!

CSS

              
                body{
    text-align: center;
}

#gameboard{
    width: 364px;
    height: 364px;
    margin: auto;
    margin-left: 51px;
}

.spacer{
    padding: 1%;
  }

.block{
    background-color: rgba(75, 66, 66, 0.8);
    color: white;
    width: 37.5%;
    margin: auto;
    opacity: .75;
    border-radius: 15px;
    font-size: 1.5em;
    padding: 3%;
}

.row{
    clear: both;
    overflow: hidden;
}

.square{
    width: 120px;
    height: 105px;
    float: left;
    cursor: pointer;
    text-align: center;
    font-size: 75px;
    padding-top:15px
}

.bottom{
    border-bottom: 2px solid black;
}

.right{
    border-right: 2px solid black
}


              
            
!

JS

              
                $(document).ready(function(){

    $('#xo').hide();
    $('#gmbd').hide();
    $('#replay').hide();
    $('#msg').hide();

    var player = 1;
    var random = Math.floor(Math.random() * 9) + 1;
    var rand = Math.floor(Math.random() * 9) + 1;
    var tag = ".sq" + rand.toString();

    $('#twoplayer').click(function(){

        $('#mode').hide();
        $('#msg').hide();
        $('#xo').show();

        $('#ex').click(function(){

            $('#mode').hide();
            $('#xo').hide();
            $('#msg').hide();
            $('#gmbd').show();
            $('#replay').show();

            $('.square').on('click', function(event){

                var squareSelected = $(this);
        
                if(squareSelected.hasClass('fa fa-times') || squareSelected.hasClass('fa fa-circle-o')){
                    alert('This square has already been selected. Please select another');
                }
                else{
                    if(player === 1){
                        squareSelected.addClass('fa fa-times');
                        if(checkIfPlayerWon('fa fa-times')){
                            alert('Congratulations! Player ' + player + ' has won!');
                            $('#replay').click(function(){
                                location.reload(true);
                            });
                            
                        }
                        else{
                            player = 2;
                        }
                    }
                    else{
                        squareSelected.addClass('fa fa-circle-o');
                        if(checkIfPlayerWon('fa fa-circle-o')){
                            alert('Congratulations! Player ' + player + ' has won!');
                            $('#replay').click(function(){
                                location.reload(true);
                            });
                        }
                        else{
                            player = 1;
                        }
                    }
                }
        
            });
            
        });

        $('#oh').click(function(){

            $('#mode').hide();
            $('#xo').hide();
            $('#msg').hide();
            $('#gmbd').show();
            $('#replay').show();

            $('.square').on('click', function(event){

                var squareSelected = $(this);
        
                if(squareSelected.hasClass('fa fa-times') || squareSelected.hasClass('fa fa-circle-o')){
                    alert('This square has already been selected. Please select another');
                }
                else{
                    if(player === 1){
                        squareSelected.addClass('fa fa-circle-o');
                        if(checkIfPlayerWon('fa fa-circle-o')){
                            alert('Congratulations! Player ' + player + ' has won!');
                            $('#replay').click(function(){
                                location.reload(true);
                            });
                            
                        }
                        else{
                            player = 2;
                        }
                    }
                    else{
                        squareSelected.addClass('fa fa-times');
                        if(checkIfPlayerWon('fa fa-times')){
                            alert('Congratulations! Player ' + player + ' has won!');
                            $('#replay').click(function(){
                                location.reload(true);
                            });
                        }
                        else{
                            player = 1;
                        }
                    }
                }
        
            });
            
        });

    });

    $('#computerplay').click(function(){

        $('#mode').hide();
        $('#xo').hide();
        $('#gmbd').hide();
        $('#replay').hide();
        $('#msg').show();

        $('#msgbtn').click(function(){

            $('#mode').hide();
            $('#xo').hide();
            $('#msg').hide();
            $('#gmbd').show();
            $('#replay').show();

            $('.square').on('click', function(event){

                var squareSelected = $(this);
        
                if(squareSelected.hasClass('fa fa-times') || squareSelected.hasClass('fa fa-circle-o')){
                    alert('This square has already been selected. Please select another');
                }
                else{
                    if(player === 1){
                        squareSelected.addClass('fa fa-times');
                        if(checkIfPlayerWon('fa fa-times')){
                            alert('Congratulations! Player ' + player + ' has won!');
                            $('#replay').click(function(){
                                location.reload(true);
                            });
                            
                        }
                        else{
                            player = 2;
                        }
                    }
                    else{
                        $(randomSquare(random)).addClass('fa fa-circle-o');
                        if(checkIfPlayerWon('fa fa-circle-o')){
                            alert('Oh no! The Computer won!');
                            $('#replay').click(function(){
                                location.reload(true);
                            });
                        }
                        else{
                            player = 1;
                        }
                    }
                }
        
            });
        
        });

    });

    $('#replay').click(function(){
        location.reload(true);
    });

    function randomSquare(rand){
        while($(tag).hasClass('fa fa-times') || $(tag).hasClass('fa fa-circle-o')){
            rand = Math.floor(Math.random() * 9) + 1;
            tag = ".sq" + rand.toString();
        }
        return tag;
    }

    function checkIfPlayerWon(symbol){

        if($('.sq1').hasClass(symbol) && $('.sq2').hasClass(symbol) && $('.sq3').hasClass(symbol)){
            return true;
        }
        else if($('.sq4').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq6').hasClass(symbol)){
            return true;
        }
        else if($('.sq7').hasClass(symbol) && $('.sq8').hasClass(symbol) && $('.sq9').hasClass(symbol)){
            return true;
        }
        else if($('.sq1').hasClass(symbol) && $('.sq4').hasClass(symbol) && $('.sq7').hasClass(symbol)){
            return true;
        }
        else if($('.sq2').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq8').hasClass(symbol)){
            return true;
        }
        else if($('.sq3').hasClass(symbol) && $('.sq6').hasClass(symbol) && $('.sq9').hasClass(symbol)){
            return true;
        }
        else if($('.sq1').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq9').hasClass(symbol)){
            return true;
        }
        else if($('.sq3').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq7').hasClass(symbol)){
            return true;
        }
        else{
            return false;
        }

    }

});
              
            
!
999px

Console