Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container-fluid text-center">
  <h1 style="color:white">Tic Tac Toe</h1>
  <a href="#" class="btn btn-primary" id="TurnX">Turn X</a>
  <a href="#" class="btn" id="TurnO">Turn O</a>
  <div class="row">
    <div class="col-md-6">
      <a href="#" class="btn btn-primary tic" id="0">#</a>
      <a href="#" class="btn btn-primary tic" id="1">#</a>
      <a href="#" class="btn btn-primary tic" id="2">#</a>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6">
      <a href="#" class="btn btn-primary tic" id="3">#</a>
      <a href="#" class="btn btn-primary tic" id="4">#</a>
      <a href="#" class="btn btn-primary tic" id="5">#</a>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6">
      <a href="#" class="btn btn-primary tic" id="6">#</a>
      <a href="#" class="btn btn-primary tic" id="7">#</a>
      <a href="#" class="btn btn-primary tic" id="8">#</a>
    </div>
  </div>
  <div id="p1score" style="color:white">
    <p>Player X's score : </p>
  </div>
  <div id="p2score" style="color:white">
    <p>Player O's score : </p>
  </div>
  <button type="button" class="btn btn-danger" id="reset">Reset</button>

</div>
              
            
!

CSS

              
                body{
background-color:black;
}

h1{
  color: white;
}
#logo{
  display:block;
  margin-right:auto;
  margin-left:auto;
}
#patreon{
 width:15%;
}
              
            
!

JS

              
                $(document).ready(function(){
  //Set Current turn to X by default 
  var currentTurn = "X"; 
  //Array storing original value of board
  var board = ["#","#","#","#","#","#","#","#","#"];
  
  var oscore = 0, xscore = 0; 
  //All non jQuery function definitions
  function reset(){
    board = ["#","#","#","#","#","#","#","#","#"];
    $(".tic").html("#");
    currentTurn = "X";
    xscore = 0; 
    $("#xscore").html(xscore);
    oscore = 0; 
    $("#oscore").html(oscore);
  }
  
  function isWin(board,currentTurn){
    if(board[0] === currentTurn && board[1] === currentTurn && board[2] === currentTurn){
     if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      } 
      reset();
      alert("Player " + currentTurn + " has won! (Row 1)"); 
    }else if(board[3] === currentTurn && board[4] === currentTurn && board[5] === currentTurn){
     if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      }
      reset();
      alert("Player " + currentTurn + " has won! (Row 2)"); 
    }else if(board[6] === currentTurn && board[7] === currentTurn && board[8] === currentTurn){
      if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      }
      reset();
      alert("Player " + currentTurn + " has won! (Row 3)"); 
    }else if(board[0] === currentTurn && board[3] === currentTurn && board[6] === currentTurn){
      if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      }
      reset();
      alert("Player " + currentTurn + " has won! (Column 1)"); 
    }else if(board[1] === currentTurn && board[4] === currentTurn && board[7] === currentTurn){
      if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      }
      reset();
      alert("Player " + currentTurn + " has won! (Column 2)"); 
    }else if(board[2] === currentTurn && board[5] === currentTurn && board[8] === currentTurn){
      if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      }
      reset();
      alert("Player " + currentTurn + " has won! (Column 3)"); 
    }else if(board[0] === currentTurn && board[4] === currentTurn && board[8] === currentTurn){
      if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      }
      reset();
      alert("Player " + currentTurn + " has won! (Diagonal)"); 
    }else if(board[2] === currentTurn && board[4] === currentTurn && board[6] === currentTurn){
      if(currentTurn === "X"){
        xscore++; 
        $("#p1score").append(xscore);
      }
      else {
        oscore++; 
        $("#p2score").append(oscore);
      } 
      reset();
      alert("Player " + currentTurn + " has won! (Diagonal)"); 
    }else{
      reset();
      alert("Game is drawn!");
    }
  }
 
    
  
  
  //jQuery functions
  $("#TurnX").click(function(){
    currentTurn = "O";
    $("#TurnX").removeClass("btn-primary");
    $("#TurnO").addClass("btn-primary");
  });
  
  $("#TurnO").click(function(){
    currentTurn = "X"; 
    $("#TurnO").removeClass("btn-primary");
    $("#TurnX").addClass("btn-primary");
  });
  
  //Select position
  $(".tic").click(function(){
    if(currentTurn === "O"){
      currentTurn = "X";
      bool = false;
    }else if(currentTurn === "X"){
      currentTurn = "O";
    }
    if($(this).html() === "#"){
      var index = $(this).attr('id');
      board[index] = currentTurn;
      $(this).html(currentTurn); 
      isWin(board,currentTurn);
    }
  });
  
  $("#reset").click(function(){
    reset();
  });
 
})
              
            
!
999px

Console