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

              
                <body>
  <div class = 'begin'>
    <h1>Choose Your Team:</h1>
    <div class = 'teamBtn'>
    <h2 class = 'btn btnXO'>X</h2>
    <h2 class = 'btn btnXO'>O</h2>
    </div>
  </div>
  <div class = 'sheet'>
    <div class = 'sheet2'>
    <h1>Player X Wins!</h1>
      <a href="#" class = 'btn btn-primary'>New Game</a>
    </div>
  </div>
  <div class = 'container-fluid wrapMe'>
    <h1 id = 'title'>Tic Tac Toe</h1>
    <div id = 'indicator'>
      <div class = 'row'>
        <div class = 'col-xs-6 playerTurn'>
          <h2>Player: X</h2>
        </div>
        <div class = 'col-xs-6 playerTurn'>
          <h2>Player: O</h2>
        </div>
      </div>
    </div>
    <div class = 'row' id = 'row1'>
      <a href="javascript:;"><div id = '0' class = 'col-md-4 open'>
        </div></a>
      <a href="javascript:;"><div id = '1' class = 'col-md-4 open'>
        </div></a>
      <a href="javascript:;"><div id = '2' class = 'col-md-4 open'>
        </div></a>
    </div>
    <div class = 'row' id = 'row2'>
      <a href="javascript:;"><div id = '3' class = 'col-md-4 open'>
        </div></a>
      <a href="javascript:;"><div id = '4' class = 'col-md-4 open'>
        </div></a>
      <a href="javascript:;"><div id = '5' class = 'col-md-4 open'>
        </div></a>
    </div>
    <div class = 'row' id = 'row3'>
      <a href="javascript:;"><div id = '6' class = 'col-md-4 open'>
        </div></a>
      <a href="javascript:;"><div id = '7' class = 'col-md-4 open'>
        </div></a>
      <a href="javascript:;"><div id = '8' class = 'col-md-4 open'>
        </div></a>
      </div>
    </div>
  </div>
</body>
              
            
!

CSS

              
                body {
  background-color: #115F3F;
}

.begin{
  background-color: rgba(0,0,0,.9);
  color: white;
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 1010px;
}

.teamBtn{
  text-align: center;
  margin: 50px;
}

.btnXO{
  font-size: 3em;
  border: 3px groove white;
  margin: 10px;
  padding: 0px 50px 0px 50px;
}
.btnXO:hover, btnXO:active{
  color: white;
  border: 3px groove white;
  background-color: rgba(50,150,50,.7);
}

.sheet{
  display: none;
  background-color: rgba(0,0,0,.4);
  color: white;
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 1010px;
}

.sheet2{
  background-color: rgba(0,0,0,.8);
  width: 60%;
  margin: auto;
  margin-top: 20%;
  padding: 20px;
  text-align: center;
}

h1{
  text-shadow: 0px 2px black;
  font-size: 8em;
  text-align: center;
}

.wrapMe {
  position: absolute;
  margin: 50px auto auto auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

#title{
  margin-bottom: 50px;
  color: white;
}

#indicator{
  width: 50%;
  margin: auto;
  margin-bottom: 50px;
}

#chooseTeam{
  text-align: center;
  color: white;
  text-shadow: 0px 3px black;
  font-size: 3em;
}

.playerTurn{
  text-align: center;
  background-color: #FFEEFF;
  box-shadow: inset 0px -2px 4px 2px;
}

.notYourTurn{
  text-align: center;
  background-color: rgba(0,0,0,.3);
  box-shadow: none;
}

#row1, #row2, #row3 {
  width: 600px;
  margin: auto;
}

#row3{
  margin-bottom: 50px;
}

.col-md-4 {
  height: 200px;
  border: 3px solid white;
  border-radius: 30px;
  text-align: center;
  color: white;
  font-size: 150px;
  background-color: #112F1F;
}
              
            
!

JS

              
                $(document).ready(function() {

  var human = '';
  var ai = '';

  var playerTurn;

  /*This array will allow us to decide what is open and/or closed -- AI will use a Math.random() of the length of the array, use that number as the index position, then place an 'X' or 'O' at that associated HTML ID number with the index -- We will then delete that index #, and rinse and repeat until array is empty or winning conditions met*/
  var openTiles = [0, 1, 2, 3, 4, 5, 6, 7, 8];

  var rows = {
  1: [1,2,3],
  2: [1,4,7],
  3: [1,5,9],
  4: [2,5,8],
  5: [3,5,7],
  6: [3,6,9],
  7: [4,5,6],
  8: [7,8,9]
}

  var tileNum;

  var chooseSide = function() {
    var dfd1 = $.Deferred();

    $('.btnXO').on('click', function() {
      human = $(this).html();
      $('.begin').hide();

      if (human == 'X') {
        playerTurn = true;
        ai = 'O';

      } else if (human == 'O') {
        playerTurn = false;
        ai = 'X';

      }
      dfd1.resolve();
    });
    return dfd1.promise();
  }

  chooseSide().done(function() {

    /*Tile Highlight*/
    if (playerTurn == true) {

      $('.playerTurn').first().removeClass('notYourTurn');
      $('.playerTurn').last().addClass('notYourTurn');

      $('.open').hover(function() {
          if ($(this).hasClass('open')) {
            $(this).html(human).css({
              color: 'rgba(100,100,100,.5)'
            });
          }
        },
        function() {
          if ($(this).hasClass('open')) {
            $(this).html('');
          }
        })

    } else {

      $('.playerTurn').first().addClass('notYourTurn');
      $('.playerTurn').last().removeClass('notYourTurn');

    }

    //Need to create player start IF and AI start if, then defer either for computer/player response.
    function whosTurn() {
      //AI functionality
      function aiFn(){setTimeout(function(){ 
          
          tileNum = JSON.parse(Math.floor(Math.random() * openTiles.length));
            $('.open#' + openTiles[tileNum]).removeClass('open').html(ai);
            openTiles.splice(tileNum, 1);
            playerTurn = true;
            console.log("AI: ", tileNum, openTiles);
          }, 800);
                     };
      
      function winCon(){
        if(humanTiles == [0,1,2] || humanTiles == [0,3,6]){
        alert('YOU WIN!');
      } else if(aiTiles == [0,1,2]){
        alert('AI wins :(');
      }
      };
      //Function will call itself until spaces filled or winning condition fulfilled.
      


        if (playerTurn == true) {
          
          $('.open').on('click', function() {
            if(playerTurn == true){
            tileNum = JSON.parse($(this).attr('id'));
            humanTiles.push(tileNum);
            console.log(humanTiles);
            
            if (openTiles.indexOf(tileNum) != -1) {
              $('#' + tileNum).removeClass('open').html(human);
              openTiles.splice(openTiles.indexOf(tileNum), 1);
              playerTurn = false;
              console.log("Human: ", tileNum, openTiles);
            }
            }else{
             aiFn();
            
              
            }
            
            return;
          });

        } else {
          aiFn().promise().done(function(){
            whosTurn();
          });
         
        }

    };
    whosTurn();

  });

  function reset() {

  }

});
              
            
!
999px

Console