css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
            
          
!
            
              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;
}
            
          
!
            
              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
🕑 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