cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

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.

            
              <div class="content">

  <div class="dialogue" id="dialogue">Play as <button class='pick' value='X'> X </button> or <button class='pick' value='O'> O </button></div>

  <div class="result" id="result"></div>
  <div class="board" id="board">

    <div class="square vline hline" id="square00"></div>
    <div class="square vline hline" id="square01"></div>
    <div class="square hline" id="square02"></div>

    <div class="square vline hline" id="square10"></div>
    <div class="square vline hline" id="square11"></div>
    <div class="square hline" id="square12"></div>

    <div class="square vline" id="square20"></div>
    <div class="square vline" id="square21"></div>
    <div class="square" id="square22"></div>
  </div>

</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=PT+Mono');
* {
  text-align: center;
  font-family: 'PT Mono';
  text-transform: uppercase;

}

body {
  background-color: cornflowerblue;
}

.content {
  background-color: gainsboro;
  height: 380px;
  width: 300px;
  margin: 40px auto;
  border: 3px solid black;
}

.square {
  float: left;
  height: 40px;
  width: 60px;
  font-size: 60px;
  padding: 10px;
  padding-bottom: 30px;
  background-color: white;
}

.square:hover,
button:hover {
  background-color: lightgreen;
}

.dialogue {
  padding: 10px 0 0 0;
  font-size: 20px;
  
}

.result {
  height: 20px;
  font-size: 20px;
  padding-bottom: 20px;
  
}

.board {
  width: 312px;
  margin: 10px auto;
  padding-left: 28px;
  
}

button {
  outline: none;
  border: none;
  width: 50px;
  height: 50px;
  padding: 5px;
  background-color: white;
  font-size: 30px;
  border: 3px solid black;
}

.vline {
  border-right: 3px solid black;
}

.hline {
  border-bottom: 3px solid black;
}

.clicked:hover {
  background-color: white !important;
}
            
          
!
            
              $(document).ready(function() {
  var squares = $(".square");
  var dialogue = $("#dialogue");
  var userSymbol = "";
  var aiSymbol;
  var userTurn = false;

  // shows the result and clears the board
  function endGame(result, symbol) {
    switch (result) {
      case 1:
        result = symbol + " won!";
        break;
      case 0:
        result = "Draw.";
    }
    $("#result").html(result);
    setTimeout(function() {
      for (var k = 0; k < 9; k++) {
        squares[k].innerHTML = "";
        $(squares[k]).removeClass("clicked");
      }
      userTurn = false;
      $(".dialogue").css("visibility", "visible");
      $("#result").html('');
    }, 2000);
  }

  // Once the user has picked their symbol play may begin
  $(".pick").click(function() {
    userSymbol = this.value;
    if (userSymbol == "X") {
      aiSymbol = "O";
      userTurn = true;
    } else {
      aiSymbol = "X";
      ai();
    }
    $(".dialogue").css("visibility", "hidden");
  });

  // checks for winning boards
  function checkWon(symbol, isTest) {
    var result;
    if (
      squares[0].innerHTML == symbol &&
      squares[1].innerHTML == symbol &&
      squares[2].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[3].innerHTML == symbol &&
      squares[4].innerHTML == symbol &&
      squares[5].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[6].innerHTML == symbol &&
      squares[7].innerHTML == symbol &&
      squares[8].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[0].innerHTML == symbol &&
      squares[3].innerHTML == symbol &&
      squares[6].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[1].innerHTML == symbol &&
      squares[4].innerHTML == symbol &&
      squares[7].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[2].innerHTML == symbol &&
      squares[5].innerHTML == symbol &&
      squares[8].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[0].innerHTML == symbol &&
      squares[4].innerHTML == symbol &&
      squares[8].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[2].innerHTML == symbol &&
      squares[4].innerHTML == symbol &&
      squares[6].innerHTML == symbol
    ) {
      result = 1;
    } else if (
      squares[0].innerHTML !== "" &&
      squares[1].innerHTML !== "" &&
      squares[2].innerHTML !== "" &&
      squares[3].innerHTML !== "" &&
      squares[4].innerHTML !== "" &&
      squares[5].innerHTML !== "" &&
      squares[6].innerHTML !== "" &&
      squares[7].innerHTML !== "" &&
      squares[8].innerHTML !== ""
    ) {
      result = 0;
    }
    if (
      (isTest === false && result == 1) ||
      (isTest === false && result === 0)
    ) {
      endGame(result, symbol);
    }
    return result;
  }

  // computer's go
  function ai() {
    // testMoves looks at possible moves to see if winning move is available. If it is available, makes the move and returns true. Else returns false;
    function testMoves() {
      for (var test = 0; test < 9; test++) {
        if (squares[test].innerHTML === "") {
          squares[test].innerHTML = aiSymbol;
          $(squares[test]).addClass("clicked");
          if (checkWon(aiSymbol, true) == 1) {
            checkWon(aiSymbol, false);
            return true;
          } else {
            squares[test].innerHTML = "";
            $(squares[test]).removeClass("clicked");
          }
        }
      }
      return false;
    }

    //if block is possible, block
    function testBlocks() {
      for (var test = 0; test < 9; test++) {
        if (squares[test].innerHTML === "") {
          squares[test].innerHTML = userSymbol;
          
          if (checkWon(userSymbol, true) == 1) {
            squares[test].innerHTML = aiSymbol;
            $(squares[test]).addClass("clicked");
            checkWon(aiSymbol, false);
            return true;
          } else {
            squares[test].innerHTML = "";
            $(squares[test]).removeClass("clicked");
          }
        }
      }
      return false;
    }
  
    // If win or block is not possible, check for next best move depending on AI symbol.
    function bestMove() {
      var moves = [4, 0, 8, 2, 6];
      if (aiSymbol == "X") {
        for (var c = 1; c < moves.length; c++) {
          if (squares[moves[c]].innerHTML === "") {
            squares[moves[c]].innerHTML = aiSymbol;
            $(squares[moves[c]]).addClass("clicked");
            checkWon(aiSymbol, false);
            return true;
          }
        }
      } else {
        for (var c = 0; c < moves.length; c++) {
          if (squares[moves[c]].innerHTML === "") {
            squares[moves[c]].innerHTML = aiSymbol;
            $(squares[moves[c]]).addClass("clicked");
            checkWon(aiSymbol, false);
            return true;
          }
        }
      }
      return false;
    }

    // If win, block or best move is not possible, just move in next available space.
    if (!testMoves() && !testBlocks() && !bestMove()) {
      for (var t = 0; t < 9; t++) {
        if (squares[t].innerHTML === "") {
          squares[t].innerHTML = aiSymbol;
          $(squares[t]).addClass("clicked");
          checkWon(aiSymbol, false);
          break;
        }
      }
    }
    if (checkWon(aiSymbol, false) == 1) {
      return;
    }
    userTurn = true;
  }

  // user's go
  $(".square").click(function() {
    if (userTurn === true && this.innerHTML === "") {
      this.innerHTML = userSymbol;     
      $(this).addClass("clicked");
      if (checkWon(userSymbol, false) == 1) {
        userTurn = false;
        return;
      }
      userTurn = false;
      ai();
    }
  });
});
            
          
!
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