123

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.

            
              <link href="https://fonts.googleapis.com/css?family=Rock Salt"
rel="stylesheet" type="text/css">

<div class='container-fluid text-center'>
  <br>
  <br>
  <h1>TiC-tAc-ToE</h1>
  
  <br>
  <br>
  
  <h3 id='result'>cHOosE:</h3><br>

  <a href='#' class='btn btn-primary turnBig' id='turnX'>X</a>
  <a href='#' class='btn btn-secondary turnSmall' id='turnO'>O</a>

  <br><br>
  
  <div id='board'>
  <div class='row justify-content-center'>
    <div class='col-xs-4'>
      <button class='tic t1' id='0'>#</button><button class='tic t2' id='1'>#</button><button class='tic t3' id='2'>#</button>
    </div>
    <!-- end col 1 -->
  </div>
  <div class='row justify-content-center'>
    <div class='col-xs-4'>
      <button class='tic t4' id='3'>#</button><button class='tic t5' id='4'>#</button><button class='tic t6' id='5'>#</button>
    </div>
    <!-- end col 2 -->
  </div>
  <div class='row justify-content-center'>
    <div class='col-xs-4'>
      <button class='tic t7' id='6'>#</button><button class='tic t8' id='7'>#</button><button class='tic t9' id='8'>#</button>
    </div>
    <!-- end col 3 -->
  </div>
  <!-- end of row  -->
  </div>
  <br>

  <a href='#' class = 'btn btn-danger bottom' id='reset'>rEseT</a>
  <br>
  <br><br>
  <p>cReaTEd by JoSh FilIpPi</p><br>
</div>
<!-- end of container -->
            
          
!
            
              body{
  font-family: Rock Salt;
}

p{
  font-size:13px;
}

.btn-secondary{
  color: #006dcc;
  border-style: none;
}

.btn-primary:visited{
  color:white;
}

.btn-warning:visited{
  color:white;
}

.btn-danger:visited{
  color:white;
}

#reset{
  font-size: 20px;
}

.turnBig{
  font-size: 33px;
  margin: 4px;
}

.turnSmall{
  font-size: 19px;
  margin: 4px;
}

#board{
  padding: 20px;
}

button{
  font-family:Rock Salt;
  line-height: 30px;
  font-size: 40px;
  background: white;
  color: white;
  border-color:black;
  border-radius: 0px;
  border-style: solid;
  border-width: 3px;
  margin: 0px;
  cursor: pointer;
  cursor: hand;
  width: 70px;
  height: 80px;

}

button:focus {outline:0;}


.clickedOn{
  color: black
}

.unClicked{
  color:White;
}

.t1{
  border-style: none;
}

.t2{
  border-top-style: none;
  border-bottom-style: none;
}

.t3{
  border-style: none;
}

.t4{
  border-left-style: none;
  border-right-style: none;
}

.t6{
  border-left-style: none;
  border-right-style: none;
}

.t7{
  border-style: none;
}

.t8{
  border-top-style: none;
  border-bottom-style: none;
}

.t9{
  border-style: none;
}

.bottom{
  width: 105px;
}



            
          
!
            
              $(document).ready(function() {
  
  
  var turn = 'x'; // player default symbol
  var compTurn = 'o'; // computer default symbol
  var turns = ['#', '#', '#', '#', '#', '#', '#', '#', '#'];
  var gameOn = false; // checks if the game is over
  var count = 0;
  
                          
  $('#turnX').click(function() { // change player symbol button

    turn = 'x';
    compTurn = 'o';
    $('#turnX').addClass('btn-primary');
    $('#turnX').removeClass('turnSmall');
    $('#turnX').addClass('turnBig');
    $('#turnO').removeClass('btn-primary');
    $('#turnO').removeClass('turnBig');
    $('#turnO').addClass('turnSmall');
  }); // end of turnX function

  $('#turnO').click(function() { // change player symbol button

    turn = 'o';
    compTurn = 'x';
    $('#turnX').removeClass('btn-primary');
    $('#turnX').removeClass('turnBig');
    $('#turnX').addClass('turnSmall');
    $('#turnO').removeClass('turnSmall');
    $('#turnO').addClass('turnBig');
    $('#turnO').removeClass('btn-secondary');
    $('#turnO').addClass('btn-primary');
  }); // end of turnO function

  function computerTurn() {

    var computerMove = (Math.random() * 10).toFixed(); // gets random number
    var move = $('#' + computerMove).text();
    if (count === 5 || gameOn === false) {
      return
    }

    //console.log($('#4').text())
       if(turns[4] === turn && count === 1){
         $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          return;
       }
    
       if (turns[4] === '#') {
          $('#4').text(compTurn);
          $('#4').removeClass('unClicked');
          $('#4').addClass('clickedOn');
          turns[4] = compTurn;
          return;
        } 
        
          
          if(turns[0] === compTurn && turns[1] === compTurn && turns[2] === '#'){
          $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === compTurn && turns[1] === compTurn && turns[0] === '#'){
          $('#0').text(compTurn);
          $('#0').removeClass('unClicked');
          $('#0').addClass('clickedOn');
          turns[0] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === compTurn && turns[0] === compTurn && turns[1] === '#'){
          $('#1').text(compTurn);
          $('#1').removeClass('unClicked');
          $('#1').addClass('clickedOn');
          turns[1] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[3] === compTurn && turns[4] === compTurn && turns[5] === '#'){
          $('#5').text(compTurn);
          $('#5').removeClass('unClicked');
          $('#5').addClass('clickedOn');
          turns[5] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[4] === compTurn && turns[5] === compTurn && turns[3] === '#'){
          $('#3').text(compTurn);
          $('#3').removeClass('unClicked');
          $('#3').addClass('clickedOn');
          turns[3] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[6] === compTurn && turns[7] === compTurn && turns[8] === '#'){
          $('#8').text(compTurn);
          $('#8').removeClass('unClicked');
          $('#8').addClass('clickedOn');
          turns[8] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[8] === compTurn && turns[7] === compTurn && turns[6] === '#'){
          $('#6').text(compTurn);
          $('#6').removeClass('unClicked');
          $('#6').addClass('clickedOn');
          turns[6] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[6] === compTurn && turns[8] === compTurn && turns[7] === '#'){
          $('#7').text(compTurn);
          $('#7').removeClass('unClicked');
          $('#7').addClass('clickedOn');
          turns[7] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[0] === compTurn && turns[3] === compTurn && turns[6] === '#'){
          $('#6').text(compTurn);
          $('#6').removeClass('unClicked');
          $('#6').addClass('clickedOn');
          turns[6] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[0] === compTurn && turns[6] === compTurn && turns[3] === '#'){
          $('#3').text(compTurn);
          $('#3').removeClass('unClicked');
          $('#3').addClass('clickedOn');
          turns[3] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[6] === compTurn && turns[3] === compTurn && turns[0] === '#'){
          $('#0').text(compTurn);
          $('#0').removeClass('unClicked');
          $('#0').addClass('clickedOn');
          turns[0] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[1] === compTurn && turns[4] === compTurn && turns[7] === '#'){
          $('#7').text(compTurn);
          $('#7').removeClass('unClicked');
          $('#7').addClass('clickedOn');
          turns[7] = compTurn;
          winCondition(turns, compTurn);
          return;
        }

          if(turns[7] === compTurn && turns[4] === compTurn && turns[1] === '#'){
          $('#1').text(compTurn);
          $('#1').removeClass('unClicked');
          $('#1').addClass('clickedOn');
          turns[1] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === compTurn && turns[5] === compTurn && turns[8] === '#'){
          $('#8').text(compTurn);
          $('#8').removeClass('unClicked');
          $('#8').addClass('clickedOn');
          turns[8] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === compTurn && turns[8] === compTurn && turns[5] === '#'){
          $('#5').text(compTurn);
          $('#5').removeClass('unClicked');
          $('#5').addClass('clickedOn');
          turns[5] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[8] === compTurn && turns[5] === compTurn && turns[2] === '#'){
          $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[0] === compTurn && turns[4] === compTurn && turns[8] === '#'){
          $('#8').text(compTurn);
          $('#8').removeClass('unClicked');
          $('#8').addClass('clickedOn');
          turns[8] = compTurn;
          winCondition(turns, compTurn);
          return;
        }  
          if(turns[8] === compTurn && turns[4] === compTurn && turns[0] === '#'){
          $('#0').text(compTurn);
          $('#0').removeClass('unClicked');
          $('#0').addClass('clickedOn');
          turns[0] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === compTurn && turns[4] === compTurn && turns[6] === '#'){
          $('#6').text(compTurn);
          $('#6').removeClass('unClicked');
          $('#6').addClass('clickedOn');
          turns[6] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[6] === compTurn && turns[4] === compTurn && turns[2] === '#'){
          $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          
          
          
    
          
          if(turns[0] === turn && turns[1] === turn && turns[2] === '#'){
          $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          winCondition(turns, compTurn);
          return;
        } 
          if(turns[0] === turn && turns[2] === turn && turns[1] === '#'){
          $('#1').text(compTurn);
          $('#1').removeClass('unClicked');
          $('#1').addClass('clickedOn');
          turns[1] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[1] === turn && turns[2] === turn && turns[0] === '#'){
          $('#0').text(compTurn);
          $('#0').removeClass('unClicked');
          $('#0').addClass('clickedOn');
          turns[0] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[3] === turn && turns[4] === turn && turns[5] === '#'){
          $('#5').text(compTurn);
          $('#5').removeClass('unClicked');
          $('#5').addClass('clickedOn');
          turns[5] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[4] === turn && turns[5] === turn && turns[3] === '#'){
          $('#3').text(compTurn);
          $('#3').removeClass('unClicked');
          $('#3').addClass('clickedOn');
          turns[3] = compTurn;
          winCondition(turns, compTurn);
          return;
        }

        
          if(turns[6] === turn && turns[7] === turn && turns[8] === '#'){
          $('#8').text(compTurn);
          $('#8').removeClass('unClicked');
          $('#8').addClass('clickedOn');
          turns[8] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[7] === turn && turns[8] === turn && turns[6] === '#'){
          $('#6').text(compTurn);
          $('#6').removeClass('unClicked');
          $('#6').addClass('clickedOn');
          turns[6] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[6] === turn && turns[8] === turn && turns[7] === '#'){
          $('#7').text(compTurn);
          $('#7').removeClass('unClicked');
          $('#7').addClass('clickedOn');
          turns[7] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[0] === turn && turns[3] === turn && turns[6] === '#'){
          $('#6').text(compTurn);
          $('#6').removeClass('unClicked');
          $('#6').addClass('clickedOn');
          turns[6] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[0] === turn && turns[6] === turn && turns[3] === '#'){
          $('#3').text(compTurn);
          $('#3').removeClass('unClicked');
          $('#3').addClass('clickedOn');
          turns[3] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[3] === turn && turns[6] === turn && turns[0] === '#'){
          $('#0').text(compTurn);
          $('#0').removeClass('unClicked');
          $('#0').addClass('clickedOn');
          turns[0] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[1] === turn && turns[4] === turn && turns[7] === '#'){
          $('#7').text(compTurn);
          $('#7').removeClass('unClicked');
          $('#7').addClass('clickedOn');
          turns[7] = compTurn;
          winCondition(turns, compTurn);
          return;
        }

        
          if(turns[4] === turn && turns[7] === turn && turns[1] === '#'){
          $('#1').text(compTurn);
          $('#1').removeClass('unClicked');
          $('#1').addClass('clickedOn');
          turns[1] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === turn && turns[5] === turn && turns[8] === '#'){
          $('#8').text(compTurn);
          $('#8').removeClass('unClicked');
          $('#8').addClass('clickedOn');
          turns[8] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === turn && turns[8] === turn && turns[5] === '#'){
          $('#5').text(compTurn);
          $('#5').removeClass('unClicked');
          $('#5').addClass('clickedOn');
          turns[5] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[5] === turn && turns[8] === turn && turns[2] === '#'){
          $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[4] === turn && turns[8] === turn && turns[0] === '#'){
          $('#0').text(compTurn);
          $('#0').removeClass('unClicked');
          $('#0').addClass('clickedOn');
          turns[0] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[0] === turn && turns[4] === turn && turns[8] === '#'){
          $('#8').text(compTurn);
          $('#8').removeClass('unClicked');
          $('#8').addClass('clickedOn');
          turns[8] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[2] === turn && turns[4] === turn && turns[6] === '#'){
          $('#6').text(compTurn);
          $('#6').removeClass('unClicked');
          $('#6').addClass('clickedOn');
          turns[6] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if(turns[6] === turn && turns[4] === turn && turns[2] === '#'){
          $('#2').text(compTurn);
          $('#2').removeClass('unClicked');
          $('#2').addClass('clickedOn');
          turns[2] = compTurn;
          winCondition(turns, compTurn);
          return;
        }
          if (move === '#') { // if square is empty
          $('#' + computerMove).text(compTurn);
          $('#' + computerMove).removeClass('unClicked');
          $('#' + computerMove).addClass('clickedOn');
          turns[computerMove] = compTurn;
        } else if (move !== '#') { // if square is not empty restart function
          computerTurn();
        } 
    winCondition(turns, compTurn); // checks for winning combinations
  }

  function playerTurn(turn, id) {

    var spotTaken = $('#' + id).text(); // value of square
    if (spotTaken === '#') { // if square is empty
      count++;
      $('#' + id).text(turn); // add player turn to square
      turns[id] = turn; // adds player turn to corresponding index of turns array
      winCondition(turns, turn); // call win condition function
      if (gameOn === true) {
        setTimeout(function() {
          computerTurn();
        }, 500);
      }
    }
  }

  function winCondition(turnArray, currentTurn) {

    if (turnArray[0] === currentTurn && turnArray[1] === currentTurn && turnArray[2] === currentTurn) { // if top 3 squares are all same
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[3] === currentTurn && turnArray[4] === currentTurn && turnArray[5] === currentTurn) { // middle 3 squares
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[6] === currentTurn && turnArray[7] === currentTurn && turnArray[8] === currentTurn) { // and so on
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[0] === currentTurn && turnArray[3] === currentTurn && turnArray[6] === currentTurn) {
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[1] === currentTurn && turnArray[4] === currentTurn && turnArray[7] === currentTurn) {
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[2] === currentTurn && turnArray[5] === currentTurn && turnArray[8] === currentTurn) {
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[0] === currentTurn && turnArray[4] === currentTurn && turnArray[8] === currentTurn) {
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else if (turnArray[2] === currentTurn && turnArray[4] === currentTurn && turnArray[6] === currentTurn) {
      $('#result').text("plAyeR '" + currentTurn + "' wInS!");
      $('#result').addClass("animated zoomIn");
      gameOn = false;
      setTimeout(function() {
        reset()
      }, 1700);
    } else {
      gameOn = true;
      if(turns[0] !== '#' && turns[1] !== '#' && turns[2] !== '#' && turns[3] !== '#' && turns[4] !== '#' && turns[5] !== '#' && turns[6] !== '#' && turns[7] !== '#' && turns[8] !== '#' && gameOn === true){
    $('#result').text('DrAW');
    $('#result').removeClass("animated fadeOut");
    $('#result').addClass("animated zoomIn");
    setTimeout(function() {
        reset()
      }, 1700);
     }
    }
  } // end of winCondition function

  $('.tic').click(function() { // identifies square clicked on by player
    //console.log($(this).text());
    $('#result').addClass('animated fadeOut');
    $(this).removeClass('unClicked');
    $(this).addClass('clickedOn');
    var slot = $(this).attr('id'); // identifies square clicked on by player
    playerTurn(turn, slot); // adds player turn symbol to square

  });

  function reset() { // reset game

    turns[0] = '#';
    turns[1] = '#';
    turns[2] = '#';
    turns[3] = '#';
    turns[4] = '#';
    turns[5] = '#';
    turns[6] = '#';
    turns[7] = '#';
    turns[8] = '#'; // reset turns array
    count = 0;
    $('.tic').removeClass('clickedOn');
    $('.tic').addClass('unClicked');
    $('.tic').text('#');
    gameOn = false;
    $('#result').text('cHOosE:');
    $('#result').removeClass("animated zoomIn");
    $('#result').removeClass("animated fadeOut");
  }

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

    reset(); // calls reset function when reset button is clicked
  });
 
}); // end of document ready
            
          
!
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