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.

            
              <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>
            
          
!
            
              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
}


            
          
!
            
              $(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
🕑 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