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">
  <h1>TicTacToe</h1>
  <div class="row">
    <div class="col-xs-4">
      <button class="0"></button>
    </div>
    <div class="col-xs-4">
      <button class="1"></button>
    </div>
    <div class="col-xs-4">
      <button class="2"></button>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-4">
      <button class="3"></button>
    </div>
    <div class="col-xs-4">
      <button class="4"></button>
    </div>
    <div class="col-xs-4">
      <button class="5"></button>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-4">
      <button class="6"></button>
    </div>
    <div class="col-xs-4">
      <button class="7"></button>
    </div>
    <div class="col-xs-4">
      <button class="8"></button>
    </div>
  </div>
  <h1 class="result"></h1>
  <button class="btn btn-primary">Play again</button>
</div>
              
            
!

CSS

              
                body {
  background-color: #000;
  background-image: url("http://subtlepatterns2015.subtlepatterns.netdna-cdn.com/patterns/stardust.png");
}

h1 {
  color: #ecf0f1;
  text-shadow: 1px 1px 5px black;
}

.result {
  text-align: center;
}

.container {
  max-width: 500px;
  margin-top: 50px;
}

button:not(.btn) {
  width: 110%;
  height: 150px;
  border: 4px solid #7f8c8d;
  border-radius: 10px;
  margin-bottom: 18px;
  background-color: #bdc3c7;
  -webkit-box-shadow: 3px 3px 13px 10px rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 3px 3px 13px 10px rgba(0, 0, 0, 0.5);
  box-shadow: 3px 3px 13px 10px rgba(0, 0, 0, 0.5);
  font-size: 100px;
  font-family: Helvetica, sans-serif;
}

.btn {
  display: block;
  margin: 0 auto;
}
              
            
!

JS

              
                var userTurn = false; // Initialize computer's turn
var winningCombos = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6]
]; // All possible winning combinations
var availableSpaces = []; // Used during computer's turn
var fillCount = 0; // Indicates if board is full
var guess = ""; // Used during computer's turn
var gameInProgress = true;

var computerGuess = function() {
  for (var j = 0; j < 9; j++) { // For all spaces (0-8)
    if ($("." + j).text() === "") { // If empty space
      availableSpaces.push(j); // Add available spaces to array
    }
  }
  guess = Math.floor(Math.random() * availableSpaces.length); // Random index
  $("." + availableSpaces[guess]).text("O"); // Computer's guess
  fillCount++; // Add one to fillCount
  availableSpaces = []; // Reset array
  checkForDraw();
  checkForWin();
  if (gameInProgress) {
    userTurn = true;
  }
}

var userGuess = function() {
  if ($(this).text() === "" && userTurn) { // If it is the user's turn and it's an empty space
    $(this).text("X"); // Play in the box
    fillCount++; // Adds one to fillCount
    userTurn = false; // End user turn
    checkForDraw();
    checkForWin();
    if (gameInProgress) {
      computerGuess();
    }
  }
}

var checkForWin = function() {
  winningCombos.forEach(function(combo) { // Check for three in a row
    if ($("." + combo[0]).text() == "X" && $("." + combo[1]).text() == "X" && $("." + combo[2]).text() == "X") { // If "X" wins
      $(".result").text('You won!'); // Report winner
      gameInProgress = false;
      userTurn = false;
      $(".btn").css("visibility", "visible"); // Display button
    } else if ($("." + combo[0]).text() == "O" && $("." + combo[1]).text() == "O" && $("." + combo[2]).text() == "O") { // If "O" wins
      $(".result").text('The computer won!'); // Report winner
      gameInProgress = false;
      userTurn = false;
      $(".btn").css("visibility", "visible"); // Display button
    }
  });
}

var checkForDraw = function() {
  if (fillCount === 9) { // If the board is full
    $(".result").text('It\'s a draw!'); // Report draw
    $(".btn").css("visibility", "visible"); // Display button
  }
}

$(document).ready(function() { // When the page loads
  computerGuess(); // Generate a computer guess
  $(".btn").css("visibility", "hidden"); // Hide button
});

for (var i = 0; i < 9; i++) { // Initialize buttons
  $("." + i).click(function() {
    if ($(this).text() === "" && userTurn) { // If it is the user's turn and it's an empty space
      $(this).text("X"); // Play in the box
      fillCount++; // Adds one to fillCount
      userTurn = false; // End user turn
      checkForDraw();
      checkForWin();
      if (gameInProgress) {
        computerGuess();
      }
    }
  });
}

$(".btn").click(function() { // Restart game when button is clicked
  $(".btn").css("visibility", "hidden");
  for(var t = 0; t < 9; t++) {
    $("." + t).text("");
  }
  $(".result").text("");
  gameInProgress = true;
  fillCount = 0;
  computerGuess();
});
              
            
!
999px

Console