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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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 class='container-fluid'>
  <p id='title'>Tic-Tac-Toe!</p><br><p class='hidden' id='playerTurn'>It is your turn!</p><p class='hidden' id='computerTurn'>It is the computer's turn.</p><p id='choice'>Would you like to be X or O?</p>
  <div id='choiceXO'><i class="fa fa-times" aria-hidden="true" id='x'></i>or<i class="fa fa-circle-o" aria-hidden="true" id='o'></i></div>
  <div class='hidden' id='currentPlayer'></div>
  <p id='resetText'>Reset!</p><i class="fa fa-undo" aria-hidden="true" id="reset"></i>
  <div id='tictactoe'>
    <div class='box' id='ulBox'></div>
    <div class='box' id='umBox'></div>
    <div class='box' id='urBox'></div>
    <div class='box' id='mlBox'></div>
    <div class='box' id='mmBox'></div>
    <div class='box' id='mrBox'></div>
    <div class='box' id='llBox'></div>
    <div class='box' id='lmBox'></div>
    <div class='box' id='lrBox'></div>
  </div>
  <div class='hidden' id="xWinner"><i class='fa fa-times' area-hidden='true'></i> wins!</div>
  <div class='hidden' id="oWinner"><i class='fa fa-circle-o' area-hidden='true'></i> wins!</div>
  <div class='hidden' id="draw">It's a draw!</div>
  </div>
            
          
!
            
              * {
  height: 100%;
  width: 100%;
  box-sizing: border-box;
}

#title {
  font-size: 2em;
  height: auto;
  text-align: center;
  margin-bottom: 0;
}

#playerTurn, #computerTurn {
  position: relative;
  margin: 0 auto;
  height: auto;
  text-align: center;
}

#choice, #currentPlayer {
  font-size: 1.2em;
  height: auto;
  margin: 0;
  position: fixed;
  top: 150px;
  left: 100px;
}

#choiceXO {
  height: auto;
  margin: 0;
  position: fixed;
  top: 175px;
  left: 150px;
}

#resetText {
  font-size: 1.2em;
  width: auto;
  height: auto;
  margin: 0;
  position: fixed;
  top: 150px;
  right: 150px;
}

#reset {
  height: auto;
  width: auto;
  margin: 0;
  position: fixed;
  top: 175px;
  right: 150px;
  z-index: 9999;
}

.fa {
  font-size: 4em;
  display: inline;
}

#tictactoe {
  margin: 50px auto;
  width: 300px;
  position: relative;
  z-index: 998;
}

.box {
  height: 100px;
  width: 100px;
  float: left;
  text-align: center;
  padding-top: 15px;
}

#ulBox {
  border-bottom: 2px solid #000000;
  border-right: 2px solid #000000;
}

#umBox {
  border-bottom: 2px solid #000000;
}

#urBox {
  border-bottom: 2px solid #000000;
  border-left: 2px solid #000000;
}

#mlBox {
  border-right: 2px solid #000000;
}

#mrBox {
  border-left: 2px solid #000000;
}

#llBox {
  border-top: 2px solid #000000;
  border-right: 2px solid #000000;
}

#lmBox {
  border-top: 2px solid #000000;
}

#lrBox {
  border-left: 2px solid #000000;
  border-top: 2px solid #000000;
}

#xWinner, #oWinner, #draw {
  position: fixed;
  top: 50px;
  left: 730px;
  font-size: 2em;
  color: red;
  z-index: 999;
}

.hidden {
  visibility: hidden;
}
            
          
!
            
              var player = '';
var computer = '';
var winningLetter;
var isWinner = false;
var emptyCells = ['ulBox', 'umBox', 'urBox',
                 'mlBox', 'mmBox', 'mrBox',
                 'llBox', 'lmBox', 'lrBox'];

$('#x').click(function() {
  player = "<i class='fa fa-times' area-hidden='true'></i>";
  computer = "<i class='fa fa-circle-o' area-hidden='true'></i>";
  $('#choice').addClass('hidden');
  $('#choiceXO').addClass('hidden');
  $('#currentPlayer').removeClass('hidden');
  $('#currentPlayer').html('Great!  You are ' + player);
  $('#playerTurn').removeClass('hidden');
  $(".box").on('click', function() {
    $('#playerTurn').addClass('hidden');
    $('#computerTurn').removeClass('hidden');
    $('#' + this.id).html(player);
    emptyCells.splice(emptyCells.indexOf(this.id),1);
    compTurn();
  });
})

$('#o').click(function() {
  player = "<i class='fa fa-circle-o' area-hidden='true'></i>";
  computer = "<i class='fa fa-times' area-hidden='true'></i>";
  $('#choice').addClass('hidden');
  $('#choiceXO').addClass('hidden');
  $('#currentPlayer').removeClass('hidden');
  $('#currentPlayer').html('Great!  You are ' + player);
  $('#computerTurn').removeClass('hidden');
  compTurn();
})

$("#reset").click(function() {
  $('#choice').removeClass('hidden');
  $('#choiceXO').removeClass('hidden');
  $('#currentPlayer').addClass('hidden');
  $('#playerTurn').addClass('hidden');
  $('#computerTurn').addClass('hidden');
  $('#xWinner').addClass('hidden');
  $('#oWinner').addClass('hidden');
  $('#draw').addClass('hidden');
  player = '';
  computer = '';
  isWinner = false;
  winningLetter = null;
  $('.box').html('');
  $(".box").off('click');
  emptyCells = ['ulBox', 'umBox', 'urBox',
                 'mlBox', 'mmBox', 'mrBox',
                 'llBox', 'lmBox', 'lrBox'];
});

function check4win () {
  if ( //upper row
    $('#ulBox').has('i').length && 
    $('#umBox').has('i').length &&
    $('#urBox').has('i').length
  ) {
    if (
      $('#ulBox i').attr('class').length == 
      $('#umBox i').attr('class').length &&
      $('#umBox i').attr('class').length == 
      $('#urBox i').attr('class').length
    ) {
      winningLetter = $('#urBox i').attr('class').length;
      winner();
    }
  }
  if ( //middle row
    $('#mlBox').has('i').length && 
    $('#mmBox').has('i').length &&     
    $('#mrBox').has('i').length
  ) {
    if (
      $('#mlBox i').attr('class').length == 
      $('#mmBox i').attr('class').length &&
      $('#mmBox i').attr('class').length == 
      $('#mrBox i').attr('class').length
    ) {
      winningLetter = $('#mrBox i').attr('class').length;
      winner();
    }
  }
  if ( //lower row
    $('#llBox').has('i').length && 
    $('#lmBox').has('i').length &&
    $('#lrBox').has('i').length
  ) {
    if (
      $('#llBox i').attr('class').length == 
      $('#lmBox i').attr('class').length &&
      $('#lmBox i').attr('class').length == 
      $('#lrBox i').attr('class').length
    ) {
      winningLetter = $('#lrBox i').attr('class').length;
      winner();
    }
  }
  if ( //left column
    $('#ulBox').has('i').length && 
    $('#mlBox').has('i').length &&
    $('#llBox').has('i').length
  ) {
    if (
      $('#ulBox i').attr('class').length == 
      $('#mlBox i').attr('class').length &&
      $('#mlBox i').attr('class').length == 
      $('#llBox i').attr('class').length
    ) {
      winningLetter = $('#llBox i').attr('class').length;
      winner();
    }
  }
  if ( //middle column
    $('#umBox').has('i').length && 
    $('#mmBox').has('i').length &&
    $('#lmBox').has('i').length
  ) {
    if (
      $('#umBox i').attr('class').length == 
      $('#mmBox i').attr('class').length &&
      $('#mmBox i').attr('class').length == 
      $('#lmBox i').attr('class').length
    ) {
      winningLetter = $('#lmBox i').attr('class').length;
      winner();
    }
  }
  if ( //right column
    $('#urBox').has('i').length && 
    $('#mrBox').has('i').length &&
    $('#lrBox').has('i').length
  ) {
    if (
      $('#urBox i').attr('class').length == 
      $('#mrBox i').attr('class').length &&
      $('#mrBox i').attr('class').length == 
      $('#lrBox i').attr('class').length
    ) {
      winningLetter = $('#lrBox i').attr('class').length;
      winner();
    }
  }
  if ( //diagonal, upper left to lower right
    $('#ulBox').has('i').length && 
    $('#mmBox').has('i').length &&
    $('#lrBox').has('i').length
  ) {
    if (
      $('#ulBox i').attr('class').length == 
      $('#mmBox i').attr('class').length &&
      $('#mmBox i').attr('class').length == 
      $('#lrBox i').attr('class').length
    ) {
      winningLetter = $('#lrBox i').attr('class').length;
      winner();
    }
  }
  if ( //diagonal, lower left to upper right
    $('#llBox').has('i').length && 
    $('#mmBox').has('i').length &&
    $('#urBox').has('i').length
  ) {
    if (
      $('#llBox i').attr('class').length == 
      $('#mmBox i').attr('class').length &&
      $('#mmBox i').attr('class').length == 
      $('#urBox i').attr('class').length
    ) {
      winningLetter = $('#urBox i').attr('class').length;
      winner();
    }
  }
  if ( //draw
    $('#ulBox').has('i').length && 
    $('#umBox').has('i').length &&
    $('#urBox').has('i').length &&
    $('#mlBox').has('i').length && 
    $('#mmBox').has('i').length &&
    $('#mrBox').has('i').length &&
    $('#llBox').has('i').length && 
    $('#lmBox').has('i').length &&
    $('#lrBox').has('i').length
  ) {
    if (isWinner === false) {
      draw();
    }
  }
}

function compTurn () {
  $(".box").off('click');
  setTimeout(delayComp, 1000);
}

function delayComp() {
  $('#playerTurn').removeClass('hidden');
  $('#computerTurn').addClass('hidden');
  var randomPos = Math.floor((Math.random() * (emptyCells.length)));
  $('#' + emptyCells[randomPos]).html(computer);
  emptyCells.splice(randomPos,1);
  check4win();
  
  if (isWinner === false) {
    $(".box").on('click', function() {
      if ($('#' + this.id).has('i').length) {
        return;
      }
      $('#playerTurn').addClass('hidden');
      $('#computerTurn').removeClass('hidden');
      $('#' + this.id).html(player);
      emptyCells.splice(emptyCells.indexOf(this.id),1);
      check4win();
      if (isWinner === false){
        compTurn();
      }
    });
  }
}

function winner () {
  $(".box").off('click');
  $('#playerTurn').addClass('hidden');
  $('#computerTurn').addClass('hidden');
  isWinner = true;
  if (winningLetter == 11) {
    $('#xWinner').removeClass('hidden');
  }
  if (winningLetter == 14) {
    $('#oWinner').removeClass('hidden');
  }
}

function draw () {
  $(".box").off('click');
  $('#playerTurn').addClass('hidden');
  $('#computerTurn').addClass('hidden');
  $('#draw').removeClass('hidden');
}
            
          
!
999px
Loading ..................

Console