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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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">
  <div id="sound"></div>
    <div class="col-md-6 col-md-offset-3 col-sm-7 col-sm-offset-2 col-xs-12 col-xs-offset-1" id="appContainer">          
      <div id="winMsg" class="col-md-12 col-sm-12 text-center hide"></div>        
       <div class="row">          
          <div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-0 text-center border-red"><h3 id="title">&#9876; Tic Tac Toe &#9876;</h3></div>          
       </div>          
       <div class="row" id="buttons">          
             <div class="col-md-8 col-md-offset-4 col-sm-8 col-sm-offset-2 col-xs-12">           
                <button class="btn btn-primary chooseSides" id="X">X</button>
                <button class="btn btn-primary chooseSides" id="O">O</button>
                <button class="btn btn-primary reset" id="reset">reset</button>
               <input id="versusBattle" class="battle" data-toggle="toggle" type="checkbox" data-size="small" data-onstyle="primary" data-offstyle="primary" data-on="2 player game" data-off="1 player game"/>              
             </div>          
        </div>
          
        <br/>
        <br/>
         
        <div class="row" id="board1">
            <div class="col-md-8 col-md-offset-4 col-sm-8 col-sm-offset-2 col-xs-12"> 
                <div id="1" class="boardSpot"><input id="1"  type="radio"><label for="1">&nbsp&nbsp</label></input></div>
                <div id="2" class="boardSpot"><input id="2" type="radio"><label for="2">&nbsp&nbsp</label></input></div>
                <div id="3" class="boardSpot"><input id="3" type="radio"><label for="3">&nbsp&nbsp</label></input></div>
              </div>                  
        </div> 

        <br/>
        <br/>

        <div class="row" id="board2">
             <div class="col-md-8 col-md-offset-4 col-sm-8 col-sm-offset-2 col-xs-12"> 
                  <div id="4" class="boardSpot"><input id="4" type="radio"><label for="4">&nbsp&nbsp</label></input></div>
                  <div id="5" class="boardSpot"><input id="5" type="radio"><label for="5">&nbsp&nbsp</label></input></div>
                  <div id="6" class="boardSpot"><input id="6" type="radio"><label for="6">&nbsp&nbsp</label></input></div>
              </div>                     
        </div>

        <br/>
        <br/>

        <div class="row" id="board3">
           <div class="col-md-8 col-md-offset-4 col-sm-8 col-sm-offset-2 col-xs-12">          
                  <div id="7" class="boardSpot"><input id="7" type="radio"><label for="7">&nbsp&nbsp</label></input></div>
                  <div id="8" class="boardSpot"><input id="8" type="radio"><label for="8">&nbsp&nbsp</label></input></div>
                  <div id="9" class="boardSpot"><input id="9" type="radio"><label for="9">&nbsp&nbsp</label></input></div>
               </div>                
        </div>

        <br/>

        <div class="row" id="scoreBoard">                     
             <div class="col-md-8 col-md-offset-4 col-sm-10 col-sm-offset-2 col-xs-12">   
                <div id="playerOneScore">Player1: 0</div>
                <div id="playerTwoScore">Player2: 0</div>
                <div id="AIScore">Computer: 0</div>
             </div>          
        </div>            
    
  </div> <!-- end appContainer -->                  
</div> <!-- end container-fluid -->
              
            
!

CSS

              
                @media (max-width: 800px) {
  #appContainer {
    margin-top: 0px; 
  }
  #title {    
    font-size: 1.8em;
  }  
  #appContainer { 
    font-size:1em; 
  }
  #winMsg { 
    font-size: 1em;     
  }
}
@media (min-width: 768px) and (max-width: 1600px) {
  #appContainer {
    margin-top: 10em; 
  }
  #title {
    font-size: 1.8em;   
  }
  #clockText{
    font-size:1.4em;
  }
  #clockBtns { 
    font-size: 1.2em;
  }
  #winMsg { 
    font-size: 1em;     
  }
}
@media (min-width: 1600px) {
  #appContainer {
    margin-top: 12em; 
  }
  #title {
   font-size: 2.88em;   
  }
  #clockText{
   font-size:2em;
  }
  #clockBtns {
    font-size: 1.6em;
  }
  #winMsg { 
    font-size: 1em;   
  }
}    
body {
  background-color: black; /*in case image server goes down*/
  background-image: url("https://nickmcoyle.com:8080/quote/img/medievalcopy.jpg");   
  color: white;
  z-index:-1;
}
#appContainer {      
  border-radius: 5em; 
  z-index: 1;  
}
.border-red {
  color: white;
  background-color: #CF3418;
  border-radius: 0.5em;
  margin-bottom: 0.4em;
}
#board{
  margin-top: 60px;
}
.boardSpot {
  background-color: #004d99;
  display:inline;  
  padding-top: 22px;
  padding-bottom: 20px; 
  margin: 3px;  
  color: white;
  font-size:2em;
  overflow: hidden;
  border-radius: 0.45em;
}
.boardSpot label {   
  margin-right: 25px;  
  line-height: 8px;
}
.boardSpotX {
  width:22.3px;
}
.boardSpot:hover {
  cursor: pointer;  
}
.hide {
  display: none;
}
#winMsg {
  position: absolute;
  top: 5em;   
  font-size: 3em; 
  vertical-align: middle;
  background-color: #dff0d8;
  border: 1px solid #d6e9c6;
  color: #468847;
  border-radius: 2%;
  z-index:1;
}
#reset {
  margin-left:10px;
  margin-right:10px; 
}
.winner {
  color: green;
}
#playerOneScore {
  display: inline-block;
  margin-left:7px;
}
#playerTwoScore {
  margin-left: 20px;
  display: inline-block;
}
#AIScore {
  margin-left: 20px;
  display: inline-block;
}
.activeTurn { 
  color: white;
}
.btn:hover {
  cursor: pointer;
}
input[type="radio"]:checked { 
  min-width: 33px;
}
input[type="radio"]:not(:checked) { 
  min-width: 40px;
}
input[type="radio"] {
  visibility: hidden;
  cursor: pointer;   
}
              
            
!

JS

              
                var toe = {
  //versusBattle true = 2 player game, false = computer game
  versusBattle: false,
  winner: false,
  turn: "playerOne",  
  playerOne: "X",
  P1Score: 0,
  playerTwo: "O",  
  P2Score: 0,
  AI: "O",
  AIScore: 0,
  chooseSides: function(side) {    
    toe[toe.turn] = side;     
    if(toe.versusBattle === true) {      
        if(toe.turn === "playerOne") {
          (toe.playerOne === "X") ? toe.playerTwo = "O": toe.playerTwo = "X";
        }
        if(toe.turn === "playerTwo") {
          (toe.playerTwo === "X") ? toe.playerOne = "O": toe.playerOne = "X";
        }     
    } else {
      (toe.playerOne === "X") ? toe.AI = "O": toe.AI = "X";
    }
    },
  availableSpots: [1,2,3,4,5,6,7,8,9],    
  firstMoves: [1,3,5,7,9],
  secondMoves: [1,3,5,7,9],
  getRandom: function(scenario) {     
     var items = toe[scenario];                
     var random = items[Math.floor(Math.random()*items.length)];
     return ( random );    
  },
  X: [],
  O: [],
  win: [
        [1,2,3],
        [1,4,7],
        [1,5,9],
        [2,5,8],
        [3,6,9],
        [3,5,7],  
        [4,5,6],
        [7,8,9]     
       ],
 styleActiveTurn: function() {
   $(".activeTurn").removeClass("activeTurn");                
   $("#"+toe.turn+"Score").addClass("activeTurn").fadeIn(200).fadeOut(200).fadeIn(200);   
 },
  testWin: function() {     
    for(var i =0; i < toe.win.length; i++) {
      var arr = toe.win[i];
      var currentPlayer = toe[toe.turn];
      var test = toe[currentPlayer].filter(function (elem) {
          return arr.indexOf(elem) > -1;
      }).length;
     if ( test === 3) {
       var winner = toe.turn;       
         switch(winner) 
         {
            case "playerOne": toe.P1Score++;  break;
            case "playerTwo": toe.P2Score++; break;
            case "AI": toe.AIScore++; break;        
         };
       toe.showWinner(toe.turn, false, arr);
      }
   }    
  },
  testBlock: function() {
     //AI will check if playerone is about to win
     for(var i =0; i < toe.win.length; i++) {
            var arr = toe.win[i];   
            var test = toe[toe["playerOne"]].filter(function (elem) {
            return arr.indexOf(elem) > -1;
         }).length;
          if (test === 2) {
            var block = arr.filter(function (elem) {
              return toe[toe["playerOne"]].indexOf(elem) === -1;
            });
            toe.win.splice(toe.win.indexOf(arr),1);       
            var id = block[0];
              if ( !($("#"+id).find('input').is(':checked')))  {
                 return id;
              }
          } 
     }
  },
  testAIWin: function() {
     //AI will check if it can win to determine best next move
     for(var i =0; i < toe.win.length; i++) {
            var arr = toe.win[i];   
            var test = toe[toe["AI"]].filter(function (elem) {
               return arr.indexOf(elem) > -1;
            }).length;      
        if (test === 2) {
          var winningMove = arr.filter(function (elem) {
             return toe[toe["AI"]].indexOf(elem) === -1;
          });
        var id = winningMove[0];
          if ( !($("#"+id).find('input').is(':checked')))  {
             return id;
          }
        } 
    }
  },
  playSound: function(filename) {        
       document.getElementById("sound").innerHTML='<audio autoplay="autoplay"><source src="' + filename +'" type="audio/mpeg" /><embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3" /></audio>';    
  },
  doAITurn: function() {        
      var id;             
      var AIWin = toe.testAIWin();
        if (typeof(AIWin) == 'number') { 
            // best move is to win           
            toe.takeTurn(AIWin);
            return;
        };
       var block = toe.testBlock();        
         if (typeof(block) == 'number') { 
           // best move is to block           
           toe.takeTurn(block);
           return;
          };
       var move = (toe.X.length + toe.O.length);
       switch(move) {           
         case 0:
          id = toe.getRandom('firstMoves');
          break;
         case 1:  
          (!($("#5").find('input').is(':checked')))? id = 5: id = toe.getRandom('secondMoves');
          break;
         default: id = toe.getRandom('availableSpots');
        }         
    toe.takeTurn(id);    
  },
  takeTurn: function(that) {    
    //disable the choosesides buttons if a game is happening
    $("#X").prop("disabled", true);
    $("#O").prop("disabled", true);    
    var checkbox;    
    
      if(typeof(that) === 'object') {      
         checkbox = that.find('input');  
         var id = that.prop("id");      
      } else {
        //it was a computer turn     
        var id = that;
        checkbox = $("#"+id).find('input');        
      }  
    
      if( !checkbox.is(':checked')) {
        checkbox.prop("checked", true);
        id = Number(id);         
        toe.availableSpots.splice(toe.availableSpots.indexOf(id),1);
        toe.secondMoves.splice(toe.secondMoves.indexOf(id),1);   
      } else {
        //its already checked so make them choose again
        // console.log("already checked", id);
        return; 
      }    
    
      if(toe.turn == "playerOne") {
        $("label[for="+id+"]").text(toe.playerOne);  
      } else if (toe.turn == "playerTwo") {
        $("label[for="+id+"]").text(toe.playerTwo);
      } else if (toe.turn == "AI") {
        $("label[for="+id+"]").text(toe.AI);
      }
   
      if ( $("label[for="+id+"]").text() == "X" ) {
        $("label[for="+id+"]").addClass("boardSpotX");
      } 
    
      var currentPlayer = toe[toe.turn];
      (currentPlayer === "X") ? toe.X.push(Number(id)): toe.O.push(Number(id));
  
      //else draw
      var drawCallBack = function() {
        if(toe.X.length + toe.O.length === 9 ) {
          toe.showWinner("draw", true);
        }
      }  
   toe.testWin(drawCallBack());    
   toe.switchTurns();
},
switchTurns: function() {
     if( toe.winner === true) { 
       return; 
     }
     //switch turns
    if(toe.versusBattle === true) {
      (toe.turn === "playerOne") ? toe.turn = "playerTwo" : toe.turn = "playerOne";
    } else if (toe.versusBattle === false ) {          
       (toe.turn === "playerOne") ? toe.turn = "AI" : toe.turn = "playerOne";
          if(toe.turn === "AI") {
            //its the computer's turn so get a number to play     
          toe.doAITurn();    
          }       
    }
    toe.styleActiveTurn();
},  
reset: function() {  
    (toe.playerOne === "X") ? toe.AI = "O": toe.AI = "X";
    $("#X").prop("disabled", false);
    $("#O").prop("disabled", false);
    toe.winner = false;
    toe.availableSpots = [1,2,3,4,5,6,7,8,9];
    toe.secondMoves = [1,3,5,7,9];
    toe.win = [
                [1,2,3],
                [1,4,7],
                [1,5,9],
                [2,5,8],
                [3,6,9],
                [3,5,7],  
                [4,5,6],
                [7,8,9]     
               ];
     toe.X = [];
     toe.O = [];  
     //remove the extra width on X played boardspots
      for(var id =1; id<10; id++) {
        $("label[for="+id+"]").removeClass("boardSpotX");
      }
    
      //uncheck all the boxes 
      $(':input[type="radio"]').prop("checked", false);
 
      //remove the x and o
      var radios = $( ".boardSpot input:radio" );
 
      $.each(radios, function(index, element){ 
          var radioID = element.id;
          $(".boardSpot label").each(function(ind, elm){
             $(this).html("&nbsp&nbsp");
          });
      });
     if (toe.versusBattle === false ) {          
       (toe.turn === "playerOne") ? toe.turn = "AI" : toe.turn = "playerOne";
          if(toe.turn === "AI") {
              //its the computer's turn so get a number to play     
              toe.doAITurn();    
          } 
     }
     toe.styleActiveTurn();
},  
showWinner: function(winner, draw, arr) {     
     toe.winner = true;
     //if computer wins play a sound
       if (winner === "AI")  {   
          var filename = "http://www.moviewavs.com/0053148414/MP3S/Movies/Star_Wars/haveyou.mp3"
          toe.playSound(filename); 
        }
    
    (draw === true) ? (winner = "draw"): (winner = toe[winner] + " wins!");
      
    $("#playerOneScore").html("Player1: " +toe.P1Score);
    $("#playerTwoScore").html("Player2: " +toe.P2Score);
    $("#AIScore").html("Computer: " +toe.AIScore);
    
    //animate the winning 3 in a row
    if(arr) {     
      arr.forEach(function(id) {
        $("#"+id).addClass("winner").fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
      });
    }
    
    //show winner message
     $("#winMsg").removeClass("hide").html(winner).fadeIn(300);
     
    var callBack = function() {
      $("#winMsg").fadeOut(300);
      $(".boardSpot").removeClass('winner');     
      toe.reset();      
    }; 
    
    setTimeout(callBack, 2000);   
},  
};

$('#versusBattle').on("click").change(function() {
    (toe.versusBattle === true ) ? toe.versusBattle = false: toe.versusBattle = true;
    toe.switchTurns();
    toe.reset();  
});

$(".chooseSides").on("click", function() {
    var side = ($(this).prop("id"));
    toe.chooseSides(side); 
});

$(".boardSpot").on("click", function() {
    if(toe.winner === true) {
      return;
    }
    var that = $(this);   
    toe.takeTurn(that);  
});

$("#reset").on("click", function() {
    toe.P1Score = 0;  toe.P2Score = 0; toe.AIScore = 0;
    $("#playerOneScore").html("P1: " +toe.P1Score);
    $("#playerTwoScore").html("P2: " +toe.P2Score);
    $("#AIScore").html("Computer: " +toe.AIScore);
    toe.reset();
}); 

$(document).ready(function(){
   toe.styleActiveTurn();
});
              
            
!
999px

Console