css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              h1[id ='ticTacToe' class='w3-animate-top'] Tic Tac Toe
                 
div[id='menu' class='container w3-animate-top']
 button[id='ng' class='button'] New Game
 button[id='op' class='button choice w3-animate-left'] Singleplayer
 button[id='tp' class='button choice w3-animate-right'] Multiplayer
 div[id='side' class='container']
  button[id='X' class='button XO w3-animate-left' title='You start!']X
  button[id='O' class='button XO w3-animate-right' title='Player 2 starts!']O

div[id='board' class='container w3-animate-right']
 div[id='r1' class='row']
  div[id='c1' class='col col-xs-4 w3-animate-zoom']
  div[id='c2' class='col col-xs-4 v w3-animate-zoom']
  div[id='c3' class='col col-xs-4 w3-animate-zoom'] 
 div[id='r2' class='row']
  div[id='c4' class='col col-xs-4 h w3-animate-zoom'] 
  div[id='c5' class='col col-xs-4 v h w3-animate-zoom'] 
  div[id='c6' class='col col-xs-4 h w3-animate-zoom'] 
 div[id='r3' class='row']
  div[id='c7' class='col col-xs-4 w3-animate-zoom'] 
  div[id='c8' class='col col-xs-4 v w3-animate-zoom'] 
  div[id='c9' class='col col-xs-4 w3-animate-zoom']

div[id='win' class='container w3-animate-right']
 h1
 h2
 i[class='fa fa-repeat fa-4x']

div[id='icon' class='container w3-animate-top']
 i[class='fa fa-arrow-circle-left fa-4x']
  
div[id='turn' class='container']
 h1

div[id='footer']
 a[id='coder' href='https://morched23mj.github.io/Portfolio/' target='_blank'] Coded by Morchid Chellali
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Indie+Flower')

span
  width: 1000px
  height: 1000px

body
  background: #194037
  color: #5AE5C8
  text-align: center
  font-family: 'Indie Flower'
  margin-bottom: 20px

#ticTacToe
  font-size: 85px

#footer
  position: fixed
  bottom: 0
  right: 0
  left: 0
  margin-bottom: 0
  height: 40px
  
#coder
  font-size: 18px
  color: #5AE5C8
  
      
#menu
  margin-top: 100px
  margin-bottom: 30px
  height: 120px
  border-radius: 5px
  width: 400px
  background: #5AE5C8
  .button
    height: 60px
    background: #194037
    color: #5AE5C8
    border-radius: 5px
    border: 0
    -webkit-transition: all 0.1s ease-out
    -moz-transition: all 0.1s ease-out
    -ms-transition: all 0.1s ease-out
    -o-transition: all 0.1s ease-out
    transition: all 0.1s ease-out
  .button:hover, .button:focus, .button:active
    background: #194037
    color: #5AE5C8
    border: 0
    outline: none
    box-shadow: none
  .button:hover
    -webkit-transform: scale(1.05)
    -moz-transform: scale(1.05)
    -ms-transform: scale(1.05)
    -o-transform: scale(1.05)
    transform: scale(1.05)
    
  #ng
    font-size: 36px
    margin-top: 30px
    
  .choice
    font-size: 30px
    margin-top: 30px
    display: none
    
  #op
    margin-right: 20px
   
  #side
    display: none
  .XO
    width: 40px
    font-size: 42px
    margin-top: 30px
  #X
    margin-right: 20px

i
  cursor: pointer    

#icon
  display: none
    
#win
  margin-top: 70px
  display: none
  h1
    font-size: 60px
  h2
    font-size: 54px
    margin-bottom: 30px
  
#board
  height: 330px
  margin-bottom: 50px
  width: 330px
  display: none
  #turn
    font-size: 50px
    margin-top: 0
    margin-bottom: 15px
  .row
    background: #5AE5C8
    color: #194037
    height: 110px
  .col
    cursor: pointer
    font-size: 75px
    -webkit-transition: all 0.4s ease-out
    -moz-transition: all 0.4s ease-out
    -ms-transition: all 0.4s ease-out
    -o-transition: all 0.4s ease-out
    transition: all 0.4s ease-out
  .v
    border-left: 3px solid #194037
    border-right: 3px solid #194037
  .h
    border-top: 3px solid #194037
    border-bottom: 3px solid #194037
    
@media screen and (max-width: 600px)
  #ticTacToe
    font-size: 75px
  #board
    height: 270px
    width: 270px
    .row
      height: 90px
    .col
      font-size: 60px
  .fa-4x
    font-size: 32px
   


            
          
!
            
              //Booleans to check for 1/2 players, who starts and which div to mark with X/O
var players = true,
  x = true,
  turn = "x",
  change = 'o',
  c,
  p1 = 0,
  p2 = 0;

//FUNCTIONS TO SHOW/HIDE THE DIFFERENT DIVS
function XO() {
  $("#side").css("display", "block");
  icon();
}
function menu() {
  $("#menu").css("display", "block");
}

function choice() {
  $(".choice").css("display", "inline");
  $('#icon').fadeOut(200);
}

function board() {
  $("#board").css("display", "block");
  $("#icon").show(400, firstPlay);
}

function firstPlay() {
  if (!players && !x) {
    $('#c5').hide().text('x').show(100);
    turn = turn == 'x' ? 'o' : 'x';
}
}

function icon() {
  $("#icon").css("display", "block");
}

$("#ng").click(function() {
  $(this).fadeOut(300, choice);
});

$(".choice").click(function() {
  $(".choice").fadeOut(300, XO);
});

$(".XO").click(function() {
  $("#menu").fadeOut(300);
  $("#icon").hide(400, board);
});

function empty() {
  for(var i = 0; i < 10; i++){
    if (sH('c'+i) !== '') return false;
  }
  return true;
}

//MANAGING THE CLICK ON RETURN ICON
$(".fa-arrow-circle-left").click(function() {
  if ($("#board").css("display") != "none") {
    $("#board").fadeOut(300, menu);
    $("#icon").fadeOut(400, icon);
    p1 = 0;
    p2 = 0;
    turn = 'x';
    clear();
  } else $("#side").fadeOut(300, choice);
});

//FUNCTION TO CLEAR THE BOARD
function clear(){
  for(var i = 1; i < 10; i++) {
    $('#c' + i).empty();
  }
}

//Set players to false/true if there is/are 1/2 players
$("#op").on("click", function() {
  players = false;
});

$("#tp").on("click", function() {
  players = true;
});

//Set x to true if X is chosen by P1, if it's false, means P2 starts
$('.XO').on('click', function(){
  if ($(this).attr('id') == 'O') x = false;
  else x = true;
});


//Append X/O when players click, only if it's empty
$(".col").on("click", function() {
  //if there are two players
  if (players && $(this).text() === '') {
    $('#' + this.id).html(function() {
      var t = turn;
      $('#icon').hide(400);
      if ($('#board').css('display') != 'none') {
        if ((x && t == 'x') || (!x && t == 'o')) {
          $('#turn').fadeOut(250, turn2);
          function turn2() {
            $('#turn h1').empty().append("Player 2, it's your turn!");
            if (!anyWins() && !gameDraw()) $('#turn').fadeIn(250);
          }
        }
        else {
          $('#turn').fadeOut(250, turn1);
          function turn1() {
            $('#turn h1').empty().append("Player 1, it's your turn!");
            if (!anyWins() && !gameDraw()) $('#turn').fadeIn(250);
          }
        }
      }
      turn = turn == "x" ? "o" : "x";
      return t;
    });
    if (anyWins() || gameDraw()) {
    turn = 'x';
    $('#turn').fadeOut();
    $('#icon').css('display', 'none');
    if (anyWins()) win();
    else if (gameDraw()) draw();
  }
  }
  
  
  
  //if there is only one player
  else if (!players && $(this).text() === '') {
    console.log(turn);
    if ((x && turn == 'x') || (!x && turn == 'o')) {
      $('#' + this.id).html(function(){
        var t = turn;
        $('#icon').hide(400);
      turn = turn == "x" ? "o" : "x";
        console.log(turn);
      return t;
      });
    }
      if (((x && turn == 'o') || (!x && turn == 'x')) && (!anyWins() && !gameDraw())) predictPlayerWin();
    
    if (anyWins() || gameDraw()) {
    turn = 'x';
    $('#turn').fadeOut();
    $('#icon').css('display', 'none');
    if (anyWins()) win();
    else if (gameDraw()) draw();
  }
    }
}); 

function predictTool(u, v, w) {
  if (((sH('c'+u) === sH('c'+v)) && (sH('c'+u) === '')) || ((sH('c'+w) === sH('c'+v)) && (sH('c'+w) === '')) || ((sH('c'+u) === sH('c'+w)) && (sH('c'+w) === ''))) {
    //debugger;
    return false;
  }
  else if (sH('c'+v) !== '' && (sH('c'+v) === sH('c'+w)) && (sH('c'+u) === '')) {
    //debugger;
    return u;
  }
  else if (sH('c'+w) !== '' && (sH('c'+u) === sH('c'+w)) && (sH('c'+v) === '')) {
    //debugger;
    return v;
  }
  else if (sH('c'+u) !== '' && (sH('c'+v) === sH('c'+u)) && (sH('c'+w) === '')) {
    //debugger;
    return w;
  }
  else {
    return false;
    //debugger;
}
}

function predictPlayerWin() {
  var stopWin, xo;
  xo = x ? 'o' : 'x';
  console.log(xo);
  
    if ((predictTool('1', '2', '3') === false) && (predictTool('1', '4', '7') === false) && (predictTool('1', '5', '9') === false) && (predictTool('2', '5', '8') === false) && (predictTool('3', '6', '9') === false) && (predictTool('3', '5', '7') === false) && (predictTool('4', '5', '6') === false) && (predictTool('7', '8', '9') === false)) {
      //debugger;
      if (sH('c5') === '') {
        $('#c5').hide().text(xo).show(50);
      }
      else for (var i = 1; i <= 9; i++) {
        if (sH('c'+i.toString()) === '') {
          //debugger;
          $('#c'+i.toString()).text(xo);
          break;
        }
      }
      turn = turn == "x" ? "o" : "x";
    }
    else {
      var wins = [['1', '2', '3'], ['1', '4', '7'], ['1', '5', '9'], ['2', '5', '8'], ['3', '6', '9'], ['3', '5', '7'], ['4', '5', '6'], ['7', '8', '9']];
      for (var i = 0; i <= 8; i++) {
        if (predictTool(wins[i][0], wins[i][1], wins[i][2]) !== false) {
          //debugger;
          stopWin = predictTool(wins[i][0], wins[i][1], wins[i][2]);
          console.log(stopWin);
          break;
        }
      }
      $('#c'+stopWin).text(xo);
      turn = turn == "x" ? "o" : "x";
    }
  
  
}







//MANAGE THE PLAY AGAIN ICON
$('.fa-repeat').click(function(){
  $('#win').hide(400, again);
});

function again(){
  $('#board').css('display', 'block');
  icon();
  clear();
  turn = change;
  change = change == 'o' ? 'x' : 'o';
  if (!players && !x && turn == 'x') {
    $('#c5').hide().text('x').delay(400).show(30);
    turn = turn == 'x' ? 'o' : 'x';
}
  if (!players && x && turn == 'o') {
    $('#c5').hide().text('o').delay(400).show(30);
    turn = turn == 'x' ? 'o' : 'x';
}
}

//FUNCTION TO EXECUTE WHEN A PLAYER WINS
var p; //To store the x or o in it, in order to know who won, used in anyWinner()
function winner() {
  var wp; //winning player message
    if ((x && p == 'x') || (!x && p == 'o')) {
      if (players) wp = 'Player 1 won! :DD';
      else wp = 'You won! :DD';
      p1 += 1;
    }
    else {
      if (players) wp = 'Player 2 won! :DD';
      else wp = 'You lost :l';
      p2 += 1;
    } 
    $('#win h1').empty().append(wp);
    $('#win h2').empty().append(p1 + ' - ' + p2);
    $('#win').css('display', 'block');
  }
function noWinner(){
  $('#win h1').empty().append('Draw... (-.-)');
  $('#win').css('display', 'block');
}
function draw() {
  $('#board').fadeOut(400, noWinner);
}
function win(){
  $('#board').fadeOut(400, winner);
}

//Shorthand function for targetting the columns, used in anyWins(), yes again :D
function sH(x){ 
  return $('#'+x).text();
}

//Check the eight cases of a win
function anyWins(){
  if ((sH('c1') == sH('c2')) && (sH('c2') == sH('c3')) && sH('c1') !== '') {
       p = sH('c1'); 
       return true;
       }
  else if ((sH('c1') == sH('c5')) && (sH('c5') == sH('c9')) && sH('c1') !== '') { 
       p = sH('c1');
       return true;
       }
  else if ((sH('c1') == sH('c4')) && (sH('c4') == sH('c7')) && sH('c1') !== '') { 
       p = sH('c1');
       return true;
       } 
  else if ((sH('c2') == sH('c5')) && (sH('c5') == sH('c8')) && sH('c2') !== '') { 
       p = sH('c5');
       return true;
       }
  else if ((sH('c3') == sH('c6')) && (sH('c6') == sH('c9')) && sH('c3') !== '') { 
       p = sH('c9');
       return true;
       }
  else if ((sH('c3') == sH('c5')) && (sH('c5') == sH('c7')) && sH('c3') !== '') { 
       p = sH('c5');
       return true;
       }
  else if ((sH('c4') == sH('c5')) && (sH('c5') == sH('c6')) && sH('c4') !== '') { 
       p = sH('c5');
       return true;
       }
  else if ((sH('c7') == sH('c8')) && (sH('c8') == sH('c9')) && sH('c7') !== '') { 
       p = sH('c9');
       return true;
       }
}


//If all columns are filled, and anyWins didn't show the winner, then this function does the rest
function gameDraw(){
  for(var k = 1; k < 10; k++){
    if ($('#c' + k).text() === '') return false;
    }
  return true;
}
            
          
!
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