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 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.

            
              <div class = "dialog" id="dialog-confirm" title="Play as X's or O's?">
  
</div>
<!--div container for all-->
<div class = "container" id = "all">

<!-- div container for board -->
<div class = "container" id = "wholeBoard">
<!-- top row-->
<div class = "row">
<button class = "btn board" id = "top1"></button>
<button class = "btn board" id = "top2"></button>
<button class = "btn board" id = "top3"></button>
</div>

<!-- middle row-->
<div class = "row">
<button class = "btn board" id = "mid1"></button>
<button class = "btn board" id = "mid2"></button>
<button class = "btn board" id = "mid3"></button>
</div>

<!-- bottom row-->
<div class = "row">
<button class = "btn board" id = "btm1"></button>
<button class = "btn board" id = "btm2"></button>
<button class = "btn board" id = "btm3"></button>
</div>
  </div>  

<p id = "plTurn">Whose Turn?</p>
<button class = "btn btn-primary" id = "first">Hit it!</button>
<button class = "btn btn-danger" id = "reset">Reset</button>

 
  </div>
  
<div class = "dialog" id="dialog-winner">
  <p id = "winner"></p>
</div>
            
          
!
            
              body{background-color: rgb(39,84,158);
color: white;}

#all{width: 60%;
margin: 0em auto;
}

.btn {height: 100px;
width: 100px;
margin: 1px;}

.board {
 background-color: white;
}

.ui-dialog{
  color: black;
  background-color: rgb(232,236,242);
  border-style: solid;
border-color: black;
border-radius: 15px;
text-align: center;}
            
          
!
            
              $(document).ready(function(){
  //declare squares - values = 0

  var top1 = '';
  var top2 = '';
  var top3 = '';
  var mid1 = '';
  var mid2 = '';
  var mid3 = '';
  var btm1 = '';
  var btm2 = '';
  var btm3 = '';
  
  var player1Sym = 'X';
  var compSym = "O";
  var plGo = "Player1";
  
  var plSym = player1Sym;
  var complete ="";

    //reset game function to start again
function reset() {
  top1 = '';
   top2 = '';
   top3 = '';
   mid1 = '';
   mid2 = '';
   mid3 = '';
   btm1 = '';
   btm2 = '';
   btm3 = '';
  $(".board").html("");

   player1Sym = 'X';
   compSym = "O";
   plGo = "Player1";
  
   plSym = 'X';
   complete = '';
};
 
  function newStart(){
        var rand = Math.random();
    console.log(rand);
    if(rand<0.5){
      plGo = "Player1";
      plSym = player1Sym;
      
      $("#plTurn").html(plGo);
    } else {
      plGo = "Computer";
      plSym = compSym;
      
      $("#plTurn").html(plGo);
      setTimeout(compTurn,1000);};
  };
  
$("#reset").click(function(){
   reset();
});

  //switches players status
function switchPl(){
  if(plGo=="Player1"){
     plGo = "Computer";
     plSym = compSym} 
    else{
    plGo = "Player1";
    plSym = player1Sym;
  };
  $("#plTurn").html(plGo);
};
  
  //test win:
  //if rows, or columns, or diags not equal to blank but all equal to each other - winner - + alert winner + restart, else continue

  function test(){
    if((btm1===btm2 && btm2===btm3 && btm1!=="")||
      (mid1===mid2 && mid2===mid3 && mid1!=="")||
      (top1===top2 && top2===top3 && top1!=="")||
      (btm1===mid1 && mid1===top1 && btm1!=="")||
      (btm2===mid2 && mid2===top2 && btm2!=="")||
      (btm3===mid3 && mid3===top3 && btm3!=="")||  
      (btm1===mid2 && mid2===top3 && btm1!=="")||
      (btm3===mid2 && mid2===top1 && btm3!==""))
    {
      
      console.log(plGo+" - Winner!!!");
      complete = 1;
    
   $(function() {
      $("#winner").html(plGo + " wins!!!");

    $( "#dialog-winner" ).dialog({
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
        "Start Again": function() {
   reset();
   newStart();
   $(this).dialog("close");

        }
        
      }
    });
   });
    
    
    }
  else{console.log("Carry on");
      switchPl();}
  };
  //ends Test for win
  
  
  
  //ask what icon to play
  
  $( function() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
        "X's": function() {
          player1Sym = "X";
          newStart();
          $( this ).dialog( "close" );
        },
        "O's": function() {
          player1Sym = "O";
          newStart();
          $( this ).dialog( "close" );
        }
      }
    });
   });

  
  
  //Click buttong functionality
  //places value, switches player, tests for win
  function clickFn(button, btnVal){

  if(btnVal!==""){}else{  
  $(button).html(plSym);

  }
   };
  
  //functionality for computer to pick turn
  //based on random number generation for now
  function compTurn(){ 
    if(complete!==""){}else{
    var rand2 = Math.random();
    
  switch(true){
    case (rand2<1/9):
      if(btm1!==""){
        compTurn();
      break;}else{
     clickFn("#btm1",btm1);
     btm1 = plSym;
        test();

      break;}
      
      case (rand2<2/9):
      if(btm2!==""){
        compTurn();
      break;
      }else{
      clickFn("#btm2",btm2);
      btm2 = plSym;
        test();

      break;}
      
      case (rand2<3/9):
      if(btm3!==""){
        compTurn();
      break;
      }else{
      clickFn("#btm3",btm3);
      btm3 = plSym;
        test();

      break;}
      
      case (rand2<4/9):
      if(mid1!==""){
        compTurn();
      break;
      }else{
      clickFn("#mid1",mid1);
      mid1 = plSym;
        test();

      break;}
      
      case (rand2<5/9):
      if(mid2!==""){
        compTurn();
      break;
      }else{
      clickFn("#mid2",mid2);
      mid2 = plSym;
        test();

      break;}
      
      case (rand2<6/9):
      if(mid3!==""){
        compTurn();
      break;
      }else{
      clickFn("#mid3",mid3);
      mid3 = plSym;
        test();

      break;}
      
      case (rand2<7/9):
      if(top1!==""){
        compTurn();
      break;
      }else{
      clickFn("#top1",top1);
      top1 = plSym;
        test();

      break;}
      
      case (rand2<8/9):
      if(top2!==""){
        compTurn();
      break;
      }else{
      clickFn("#top2",top2);
      top2 = plSym;
        test();

      break;}
      
      case (rand2<9/9):
      if(top3!==""){
        compTurn();
      break;
      }else{
      clickFn("#top3",top3);
      top3 = plSym;
        test();

      break;}
 } //ends switch
    }; //ends if completed
 };  //ends compTurn
 
   //random number for who starts first
  $("#first").click(function(){
    reset();
    newStart();
  }); //ends first player button
  
  
  
  //player1 moves:
  //click button to choose
$("#btm1").click(function(){
  clickFn("#btm1",btm1);
  btm1 = plSym;
  test();

  setTimeout(compTurn,1000);
});
 
$("#btm2").click(function(){
  clickFn("#btm2",btm2);
  btm2 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#btm3").click(function(){
  clickFn("#btm3",btm3);
  btm3 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#mid1").click(function(){
  clickFn("#mid1",mid1);
  mid1 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#mid2").click(function(){
  clickFn("#mid2",mid2);
  mid2 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#mid3").click(function(){
  clickFn("#mid3",mid3);
  mid3 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#top1").click(function(){
  clickFn("#top1",top1);
  top1 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#top2").click(function(){
  clickFn("#top2",top2);
  top2 = plSym;
  test();

  setTimeout(compTurn,1000);
});
  
$("#top3").click(function(){
  clickFn("#top3",top3);
  top3 = plSym;
  test();

  setTimeout(compTurn,1000);
});

  
  
  
  
  
  


  
  
  
  
  
  
}); //ends 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.
Loading ..................

Console