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.

            
              <!--Acknowledgments to Dylan Israel; CodingTutorials 360 -->

<div class="container text-center">
  <h1>TicTacToe</h1>
  <h2>Choose either X or O!</h2>
  <h3>Hit button to switch either as 'X' or 'O'.</h3>
    
  <a class ="btn btn-primary" id="turnX">X</a>
  <a class ="btn btn-secondary" id="turnO">O</a>
  <div class="row">
    <div class="col-md-6">
      <a class="btn btn-primary tic" id="0">#</a>
      <a class="btn btn-primary tic" id="1">#</a>
      <a class="btn btn-primary tic" id="2">#</a>
    </div>
  </div>
      <div class="row">
      <div class="col-md-6">
      <a class="btn btn-primary tic" id="3">#</a>
      <a class="btn btn-primary tic" id="4">#</a>
      <a class="btn btn-primary tic" id="5">#</a>
        </div>
      </div>
        <div class="row">
        <div class="col-md-6">
      <a class="btn btn-primary tic" id="6">#</a>
      <a class="btn btn-primary tic" id="7">#</a>
      <a class="btn btn-primary tic" id="8">#</a>
          </div>
        </div>
        </div>

            
          
!
            
              body{
  background-color:black;
}
h1 {
  color : white;
}
h2 {
  color : yellow;
}
h3{
  color : yellow
}
            
          
!
            
              //Acknoweledgments to CodingTutorials 360; Dylan Israel//

$(document).ready(function(){
  //defaults player's turn to x
  var turn="X"
  //Array stores values that we will check later for a winner
  var turns=["#","#","#","#","#","#","#","#","#"];
  //Default computer 
  var computersTurn="O";
  //Keeps track if it is the computer's turn
  var gameOn=false;
  //No loop
  var count = 0;
  //Change player's turn to x and computer's to O
  $('#turnX').click(function(){
    turn = "O";
    computersTurn = "X";
    $("#turnX").removeClass("btn-primary");
    $("#turnO").addClass("btn-primary"); 
    reset();
  });
  $('#turnO').click(function(){
    turn = "X";
    computersTurn = "O";
    $("#turnO").removeClass("btn-primary");
    $("#turnX").addClass("btn-primary");     
    reset();
  });
  
  function computerTurn(){
    //Used to break the while loop
    var taken=false;
    while(taken===false && count !==5){
      //generate computer's random turn
      var computersMove= (Math.random()*10).toFixed();
      var move= $("#"+computersMove).text();
      if(move==="#"){
        $("#"+computersMove).text(computersTurn);
           taken=true;
        turns[computersMove]= computersTurn;
      }
    }
  }
  
  function playerTurn(turn, id){
    var spotTaken = $("#"+id).text();
    if(spotTaken ==="#"){
      count++;
      turns[id]= turn;
      $("#"+id).text(turn);
      
      winCondition(turns, turn);
      if(gameOn===false){
        computerTurn();
        winCondition(turns, computersTurn);
        
      }
   
    }
  }
 //7:41
   function winCondition(turnArray, currentTurn){
     if(turnArray[0] === currentTurn &&
       turnArray[1]===currentTurn && turnArray[2] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (Top across 0,1, and 2 spots)");
     }else if(turnArray[2] === currentTurn &&
       turnArray[4]===currentTurn && turnArray[6] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (Top across 2,4, and 6 spots)");
   }else if(turnArray[0] === currentTurn &&
       turnArray[3]===currentTurn && turnArray[6] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (1st row down 0,3, and 6 spots)");
   }else if(turnArray[0] === currentTurn &&
       turnArray[4]===currentTurn && turnArray[8] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (Top across 0,4, and 8 spots)");
   }else if(turnArray[1] === currentTurn &&
       turnArray[4]===currentTurn && turnArray[7] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (2nd row down1,4 and 7 spots)");
   }else if(turnArray[2] === currentTurn &&
       turnArray[5]===currentTurn && turnArray[8] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (3rd row down2,5, and 8 spots)");
   }else if(turnArray[2] === currentTurn &&
       turnArray[5]===currentTurn && turnArray[8] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (3rd row 2,5 and 8 spots)");
   }else if(turnArray[3] === currentTurn &&
       turnArray[4]===currentTurn && turnArray[5] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (3rd row across 2,4, and 6 spots)");
   }else if(turnArray[6] === currentTurn &&
       turnArray[7]===currentTurn && turnArray[8] === currentTurn){
       gameOn = true;
       reset();
       alert("Player " + currentTurn + " wins! (Bottom row6,7, and 8 spots)");
   }else {
     gameOn =false;
   }
   }
  $(".tic").click(function(){
    var slot = $(this).attr('id');
    playerTurn(turn, slot);
    
                  });
  function reset(){
    turns = ["#","#","#","#","#","#","#","#","#"];
    count=0;
    $(".tic").text("#");
    gameOn = false;
  }
});
            
          
!
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