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.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

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.

            
              
    <body>
        <button class='btn btn-default'>Settings</button>
        <div class="container">
            <div class="box top left" id="1"></div>
            <div class="box top" id="2"></div>
            <div class="box top right" id="3"></div>
            <div class="box left" id="4"></div>
            <div class="box" id="5"></div>
            <div class="box right" id="6"></div>
            <div class="box bottom left" id="7"></div>
            <div class="box bottom" id="8"></div>
            <div class="box bottom right" id="9"></div>
        </div>
        <div class="modal" id="settings">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">X</button>
                        <h4 class="modal-title">Settings</h4>
                    </div>
                    <div class="modal-body">
                        <div class="modal-settings">
                            <h5 class="modal-settings-header">Dificulty level:</h5>
                            <button class='dlevel' id='red'>&checkmark;</button>
                            <button class='dlevel' id='yellow'></button>
                            <button class='dlevel' id='green'></button>
                        </div>
                        <div class="modal-settings">
                            <h5 class="modal-settings-header">Who starts?</h5>
                            <button class='who' id='iam'>I'm</button>
                            <button class='who' id='computer'>Computer</button>
                        </div>
                        <div class="modal-settings">
                            <h5 class="modal-settings-header">X's or O's?</h5>
                            <button class='XO' id='pickX'>X</button>
                            <button class='XO' id='pickO'>O</button>
                        </div>
                    </div>
                    <div class='clear'></div>
                    <div class="modal-footer">
                        <button class="btn btn-success" data-dismiss="modal">Play!</button>
                    </div>
                </div>
            </div>
        </div>
    </body>


            
          
!
            
              *{
    margin:0;
    padding:0;
}
.btn-default{
    position: absolute;
    top: 10px;
    right: 10px;
}
button {
    border-radius: 4px;
}
.container{
    height: 500px;
    width: 480px;
    margin:auto;
    margin-top: 100px;
}
.box{
    height:150px;
    width:150px;
    border: 1px solid black;
    float: left;
    cursor: pointer;
    text-align: center;
    font-size: 2em;
}
.box p{
    margin-top: 55px;
}
.modal-settings {
    float: left;
    margin-left: 75px;
    height:100px;
}
.dlevel {
    height: 25px;
    width: 25px;
    margin-left:5px;
}
#red{
    background-color:red;
}
#yellow{
    background-color:yellow;
}
#green{
    background-color:greenyellow;
}
.who {
    height: 25px;
    padding:2px;
    margin-left: 5px;
    background-color:greenyellow;
}
.XO{
    height: 25px;
    width: 25px;
    margin-left:5px;
    background-color:greenyellow;
}
#pickX{
    background-color:red;
}
.clear {
    clear: both;
}

.top{
    border-top: none;
}
.left{
    border-left: none;
}
.right{
    border-right: none;
}
.bottom{
    border-bottom: none;
}
#iam {
    background-color:red;
}
            
          
!
            
              $(document).ready(function(){
    
    //X's or O's
    $('#pickX').click(function(){
        usersChoice="<p>X</p>";
        computersChoice="<p>O</p>";
        $(this).css('background-color', 'greenyellow');
        $('#pickO').css('background-color', 'red');
    });
    $('#pickO').click(function(){
        usersChoice="<p>O</p>";
        computersChoice="<p>X</p>";
        $('#pickX').css('background-color', 'red');
        $(this).css('background-color', 'greenyellow');
    });
    
    
    //Who starts
    $('#iam').click(function(){
        starter='player';
        $('#computer').css('background-color', 'red');
        $(this).css('background-color', 'greenyellow');
    });
    $('#computer').click(function(){
        starter='computer';
        $('#iam').css('background-color', 'red');
        $(this).css('background-color', 'greenyellow');
    });
    
    
    // Difficulty level
    $('#red').click(function(){
        difficulty='red';
        $(this).html('&checkmark;');
        $('#yellow').html('');
        $('#green').html('');
    });
    $('#yellow').click(function(){
        difficulty='yellow';
        $(this).html('&checkmark;');
        $('#green').html('');
        $('#red').html('');
    });
    $('#green').click(function(){
        difficulty='green';
        $(this).html('&checkmark;');
        $('#yellow').html('');
        $('#red').html('');
    });
    
    
    //Modal
    $('#settings').modal('show');
    $('.btn-success').click(function(){
        play();
    });
    $('.btn-default').click(function(){
        $('#settings').modal('show');
    });
    
    play();
});


// variables
var move;
var finish=0;
var difficulty='red';
var starter='computer';
var used=[];
var usedByPlayer=[];
var usedByComp=[];
var count=0;
var usersChoice="<p>O</p>";
var computersChoice="<p>X</p>";
var toprow=['1','2','3'];
var middlerow=['4','5','6'];
var bottomrow=['7','8','9'];
var leftcol=['1','4','7'];
var middlecol=['2','5','8'];
var righttcol=['3','6','9'];
var crossone=['1','5','9'];
var crosstwo=['3','5','7'];
var win=[toprow, middlerow, bottomrow, leftcol, middlecol, righttcol, crossone, crosstwo];
var moveCount=usedByComp.length;


// Let's play!

function play(){
    finish=0;
    used=[];
    usedByPlayer=[];
    usedByComp=[];
    count=0;
    $('.box').html('');
    switch (difficulty){
        case 'red':
            if(starter==='computer'){
                redComputersMove();
                $('.box').click(usersMove());
            }
            break;
        case 'yellow':
            if(starter==='computer'){
                yellowComputersMove();
                $('.box').click(usersMove());
            }
            break;
        case 'green':
            if(starter==='computer'){
                greenComputersMove();
                $('.box').click(usersMove());
            }
            break;
    }
}

//Users's move

function usersMove(){
    $('.box').click(function(){
        if(used.indexOf($(this).attr('id'))===-1){
            used.push($(this).attr('id'));
            usedByPlayer.push($(this).attr('id'));
            $('#'+$(this).attr('id')).html(usersChoice);
            userWin();
            if(finish===0){
                switch (difficulty){
                    case 'green':
                        greenComputersMove();
                        break;
                    case 'yellow':
                        yellowComputersMove();
                        break;
                    case 'red':
                        redComputersMove();
                        break;
                }
            }
        }
    });
    
}


//Did you win?
function userWin(){
    for (var i in win){
        for (var j in win[i]){
            if(usedByPlayer.indexOf(win[i][j])!==-1){
                count++;
                if (count===3){
                    finish=1;
                    $('.modal-title').html('You won!');
                    $('#settings').modal('show');
                    return;
                }
            }
        }
        count=0;
    }
    if(used.length===9){
        $('.modal-title').html('Tie!');
        $('#settings').modal('show');
        return;
    }
}

//Did computer win?
function compWin(){
    for (var i in win){
        for (var j in win[i]){
            if(usedByComp.indexOf(win[i][j])!==-1){
                count++;
                if (count===3){
                    $('.modal-title').html('Computer won!');
                    $('#settings').modal('show');
                    return;
                }
            }
        }
        count=0;
    }
    if(used.length===9){
        $('.modal-title').html('Tie!');
        $('#settings').modal('show');
        return;
    }
}



//Green computers moves
function greenComputersMove(){
    move=Math.floor(Math.random()*9)+1+'';
    if (used.indexOf(move)===-1){
        used.push(move);
        usedByComp.push(move);
        $('#'+move).html(computersChoice);
        compWin();
        
    }
    else {
        greenComputersMove();
    }
}

//Yellow computers moves
function yellowComputersMove(){
    
    var ul=used.length;
    switch (ul){
        case 0:
            console.log('case0');
            move=Math.floor(Math.random()*9)+1+'';
            used.push(move);
            usedByComp.push(move);
            $('#'+move).html(computersChoice);
            break;
        case 1:
            console.log('case1');
            if(used.indexOf('5')!==-1){
                var moveOption=['1','3','7','9'];
                move=Math.floor(Math.random()*4)+'';
                move=moveOption[move];
                check();
            }
            else {
                move='5';
                used.push(move);
                usedByComp.push(move);
                $('#'+move).html(computersChoice);
            }
                
            return;
        case 2:
            console.log('case2');
            move=Math.floor(Math.random()*8)+1+'';
            check();
            return;
        case 3:
            console.log('case3');
            move=Math.floor(Math.random()*8)+1+'';
            check();
            return;
        case 4:
            console.log('case4');
            move=Math.floor(Math.random()*8)+1+'';
            check();
            return;
        case 5:
            console.log('case5');
            scheme();
            if (usedByComp.length===2){
                var moveOption=['1','3','7','9'];
                move=Math.floor(Math.random()*4);
                move=moveOption[move];
                check();
            }
            if (usedByComp.length===2){
                move=Math.floor(Math.random()*8)+1+'';
                check();
            }
            break;
        case 6:
            console.log('case6');
            scheme();
            if (usedByComp.length===3){
                move=Math.floor(Math.random()*8)+1+'';
                check();
            }
            break;
        case 7:
            console.log('case7');
            scheme();
            if (usedByComp.length===3){
                move=Math.floor(Math.random()*8)+1+'';
                check();
            }
            break;
        case 8:
            console.log('case8');
            greenComputersMove();
            break;
    }
}


//Red computers moves
function redComputersMove(){
    
    var ul=used.length;
    switch (ul){
        case 0:
            console.log('case0');
            var moveOption=['1','3','7','9'];
            move=Math.floor(Math.random()*4);
            move=moveOption[move];
            used.push(move);
            usedByComp.push(move);
            $('#'+move).html(computersChoice);
            return;
        case 1:
            console.log('case1');
            if(used.indexOf('5')!==-1){
                var moveOption=['1','3','7','9'];
                move=Math.floor(Math.random()*4)+'';
                move=moveOption[move];
                check();
            }
            else {
                move='5';
                used.push(move);
                usedByComp.push(move);
                $('#'+move).html(computersChoice);
            }
                
            return;
        case 2:
            console.log('case2');
            if(used.indexOf('5')===-1){
                var moveOption=['1','3','7','9'];
                move=Math.floor(Math.random()*4);
                move=moveOption[move];
                check();
            }
            else{
                switch(usedByComp[usedByComp.length-1]){
                    case "1":
                        move="9";
                        break;
                    case "3":
                        move="7";
                        break;
                    case "7":
                        move="3";
                        break;
                    case "9":
                        move="1";
                        break;
                }
                used.push(move);
                usedByComp.push(move);
                $('#'+move).html(computersChoice);
            }
            break;
        case 3:
            console.log('case3');
            if(usedByPlayer.indexOf('2')!==-1){
                if(usedByPlayer.indexOf('6')!==-1){
                    move='3';
                    check();
                    return;
                }
            }
            if(usedByPlayer.indexOf('6')!==-1){
                if(usedByPlayer.indexOf('8')!==-1){
                    move='9';
                    check();
                    return;
                }
            }
            if(usedByPlayer.indexOf('8')!==-1){
                if(usedByPlayer.indexOf('4')!==-1){
                    move='7';
                    check();
                    return;
                }
            }

            if(usedByPlayer.indexOf('4')!==-1){
                if(usedByPlayer.indexOf('2')!==-1){
                    move='1';
                    check();
                    return;
                }
            }
            if (usedByComp.length===1){
                scheme();
                if (usedByComp.length===1){
                    move=Math.floor(Math.random()*8)+1+'';
                    check();
                    return;
                }
            }
            break;
        case 4:
        console.log('case4');
        scheme();
        
        if (usedByComp.length===2){
            console.log('case4if');
            move=Math.floor(Math.random()*8)+1+'';
            check();
            return;
        }
        else {
            return;
        }
        case 5:
            console.log('case5');
            scheme();
            if (usedByComp.length===2){
                var moveOption=['1','3','7','9'];
                move=Math.floor(Math.random()*4);
                move=moveOption[move];
                check();
            }
            if (usedByComp.length===2){
                move=Math.floor(Math.random()*8)+1+'';
                check();
            }
            break;
        case 6:
            console.log('case6');
            scheme();
            if (usedByComp.length===3){
                move=Math.floor(Math.random()*8)+1+'';
                check();
            }
            break;
        case 7:
            console.log('case7');
            scheme();
            if (usedByComp.length===3){
                move=Math.floor(Math.random()*8)+1+'';
                check();
            }
            break;
        case 8:
            console.log('case8');
            greenComputersMove();
            break;
    }
    
}

//Computers tactic
function scheme(){
    count=0;
    for (var i in win){
        for (var j in win[i]){
            if(usedByComp.indexOf(win[i][j])!==-1){
                count++;
            }
            if (count===2){
                for (var k in win[i]){
                    if(used.indexOf(win[i][k])===-1){
                        move=win[i][k];
                        used.push(move);
                        usedByComp.push(move);
                        $('#'+move).html(computersChoice);
                        //check if win
                        checkIfWin();
                        return;
                    }
                }
            }
        }
        count=0;
    }
    for (var i in win){
        for (var j in win[i]){
            if(usedByPlayer.indexOf(win[i][j])!==-1){
                count++;
            }
            if (count===2){
                for (var k in win[i]){
                    if(used.indexOf(win[i][k])===-1){
                        move=win[i][k];
                        used.push(move);
                        usedByComp.push(move);
                        $('#'+move).html(computersChoice);
                        count=0;
                        // check if win
                        checkIfWin();
                        return;
                    }
                }
            }
        }
        count=0;
    }
}



function check(){
    if(used.indexOf(move)===-1){
        used.push(move);
        usedByComp.push(move);
        $('#'+move).html(computersChoice);
    }
    else{
        if (difficulty==='red'){
            redComputersMove();
            return;
        }
        else {
            yellowComputersMove();
            return;
        }
    }
}

function checkIfWin(){
    count=0;
    for (var i in win){
        for (var j in win[i]){
            if (usedByComp.indexOf(win[i][j])!==-1){
                count++;
                if (count===3){
                    $('.modal-title').html('Computer won!');
                    $('#settings').modal('show');
                    return;
                }
            }
        }
        count=0;
    }
    for (var i in win){
        for (var j in win[i]){
            if (usedByPlayer.indexOf(win[i][j])!==-1){
                count++;
                if (count===3){
                    $('.modal-title').html('You won!');
                    $('#settings').modal('show');
                    return;
                }
            }
        }
        count=0;
    }
    if (used.length===9){
        $('.modal-title').html('Tie!');
        $('#settings').modal('show');
        return;
    }
}

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

Console