Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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="title text-center">Tic Tac Toe</div>
<div class="game-board">
  <div id="top-left" data-row="0" data-col="0" class="col-xs-4"></div>
  <div id="top-middle" data-row="0" data-col="1" class="col-xs-4"></div>
  <div id="top-right" data-row="0" data-col="2" class="col-xs-4"></div>
  <div id="center-left" data-row="1" data-col="0" class="col-xs-4"></div>
  <div id="center-middle" data-row="1" data-col="1" class="col-xs-4"></div>
  <div id="center-right" data-row="1" data-col="2" class="col-xs-4"></div>
  <div id="bottom-left" data-row="2" data-col="0" class="col-xs-4"></div>
  <div id="bottom-middle" data-row="2" data-col="1" class="col-xs-4"></div>
  <div id="bottom-right" data-row="2" data-col="2" class="col-xs-4"></div>
</div>

<div class="symbol-choice-box">
  <div class="choice-desc text-center">Choose Either X or O!</div>
  <div class="X-or-O clearfix">
    <div class="X text-center">X</div><div class="O text-center">O</div>
  </div>
</div>

<div class="desc text-center">Click a box to make a move!</div>
              
            
!

CSS

              
                .title {
  font-size: 76px;
}

.game-board {
  margin: 25px auto;
  width: 500px;
  height: 500px;
  background: black;
  padding: 20px;
}

.game-board > div {
  width: 33.333333333%;
  height: 33.33333333%;
  background: black;
  border: 2px solid white;
  cursor: pointer;
  color: white;
  font-size: 150px;
  text-align: center;
  line-height: 1.05;
}

#top-left {
  border-left: none;
  border-top: none;
}

#top-middle {
  border-top: none;
}

#top-right {
  border-top: none;
  border-right: none;
}

#center-left {
  border-left: none;
}

#center-right {
  border-right: none;
}

#bottom-left {
  border-left: none;
  border-bottom: none;
}

#bottom-middle {
  border-bottom: none;
}

#bottom-right {
  border-right: none;
  border-bottom: none;
}

.desc {
  font-size: 75px;
}

.symbol-choice-box {
  font-size: 75px;
}

.X, .O {
  float: left;
  border: 5px solid black;
  width: 100px;
  cursor: pointer;
}

.X {
  margin-left: 35%;
}

.O {
  margin-left: 20%;
}
              
            
!

JS

              
                var TicTacToe = window.TicTacToe = {};
var playerSymbol = window.TicTacToe.playerSymbol = "";
var moves = window.TicTacToe.moves = 0;

var gameBoard = window.TicTacToe.gameBoard = [["", "", ""],
                                              ["", "", ""],
                                              ["", "", ""]];

var reset = TicTacToe.reset = function() {
  for(var i = 0; i <= 2; i++) {
    for(var j = 0; j <= 2; j++) {
      gameBoard[i][j] = "";
    }
  }
  
  moves = 0;
  updateBoard();
  playerSymbol = "";
  $('.symbol-choice-box').removeClass('hide');
};

var handleClick = TicTacToe.handleClick = function(e) {
  if(playerSymbol === "") { return; }
  if(moves >= 9) {
    reset();
    return;
  }
  var row = $(e.currentTarget).data('row');
  var col = $(e.currentTarget).data('col');
  
  if(!empty(row, col)) { return; }
  
  $('[data-row="' + row + '"][data-col="' + col + '"]').text(playerSymbol);
  gameBoard[row][col] = playerSymbol;
  moves++;
  updateBoard();
  
  if(moves < 9){
    playComputer();
  }
};

var playComputer = TicTacToe.playComputer = function() {
  var choice = false;
  var x;
  var y;
  
  
  while(!choice) {
    x = Math.floor(Math.random() * (2 - 0 + 1)) + 0;
    y = Math.floor(Math.random() * (2 - 0 + 1)) + 0;
    
    if(gameBoard[x][y] === "") {
      choice = true;
    }
  }
  
  makeComputerMove([x,y]);
}

var handleSymbolSelect = TicTacToe.handleSymbolSelect = function(e) {
  playerSymbol = $(e.target).text();
  $('.symbol-choice-box').addClass('hide');
}

var makeComputerMove = TicTacToe.makeComputeMove = function(move) {
  var i = move[0];
  var j = move[1];
  var mark;
  
  playerSymbol === "X" ? mark = "O" : mark = "X";
  
  gameBoard[i][j] = mark;
  moves++;
  updateBoard();
}

// var switchTurn = TicTacToe.switchTurn = function() {
//   playerTurn === true ? playerTurn = false : playerTurn = true;
// };

// var switchSymbol = TicTacToe.switchSymbol = function() {
//   playerSymbol === "X" ? playerSymbol = "O" : playerSymbol = "X"
// }

var updateBoard = TicTacToe.updateBoard = function() {
  for(var i = 0; i <= 2; i++) {
    for(var j = 0; j <= 2; j++) {
      $('[data-row="' + i + '"][data-col="' + j + '"]').text(gameBoard[i][j]);
    }
  }
};

var empty = TicTacToe.empty = function(x,y) {
  return gameBoard[x][y] === "";
}

$(document).ready(function() {
  $('.col-xs-4').on("click", handleClick);
  $('.X-or-O').on("click", handleSymbolSelect);
});
              
            
!
999px

Console