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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 id="pageContainer" class="container">
    <!--Game field-->
    <div id="gameContainer" class="row">
      <div id="title" class="col-xs-12">
        <h1>Tic Tac Toe</h1> </div>
      <div id="A1" class="gameSquare col-xs-4">
        <h2></h2> </div>
      <div id="B1" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="C1" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="A2" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="B2" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="C2" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="A3" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="B3" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div id="C3" class="gameSquare col-xs-4">
        <h2></h2></div>
      <div class="col-xs-6"> <img id="compThink" src="https://i.imgsafe.org/586f8a7b8e.png">
        <h3 id="turn">
            </h3></div>
      <div class="col-xs-6">
        <h3 id="score"></h3></div>
    </div>
    <!--Piece selection-->
    <div id="selection" class="row">
      <h1>Tic Tac Toe</h1>
      <h3>Difficulty:</h3>
      <div id="noob" class="col-xs-6 selection">
        <h3>Noob</h3></div>
      <div id="medium" class="col-xs-6 selection difficulty">
        <h3>Medium</h3></div>
      <br>
      <br>
      <h3>Choose Your Weapon!</h3>
      <div id="X" class="col-xs-6 selection">
        <h2>X</h2></div>
      <div id="O" class="col-xs-6 selection">
        <h2>O</h2></div>
    </div>
    <!-- Play Again-->
    <div id="playAgain" class="row">
      <h1>Tic Tac Toe</h1>
      <h3 id="win_loseMessage"></h3> <img id="win_loseFace">
      <div id="anotherPlay">
        <h3>Let's play again!</h3></div>
    </div>
  </div>
  <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
  <script src="js/js.js"></script>
</body>
              
            
!

CSS

              
                 @import url('https://fonts.googleapis.com/css?family=Pangolin');
 html,
 body {
   height: 100%;
 }
 
 #pageContainer {
   background-image: url(https://i.imgsafe.org/1712a0baf3.png);
   min-width: 100%;
   min-height: 100%;
   background-size: cover;
 }
 
 h1 {
   font-size: 2em;
   font-family: 'Pangolin', cursive;
   color: white;
 }
 
 h2 {
   font-size: 8em;
   font-family: 'Pangolin', cursive;
   color: white;
 }
 
 h3 {
   font-size: 1.25em;
   font-family: 'Pangolin', cursive;
   color: white;
 }
 
 #gameContainer {
   display: none;
   width: 450px;
   margin-left: auto;
   margin-right: auto;
   margin-top: 20px;
   background-color: rgba(0, 0, 0, .85);
   border-radius: 20px;
   text-align: center;
 }
 
 .gameSquare {
   width: 150px;
   height: 150px;
   border: 1px solid rgb(0, 0, 0);
 }
 
 .gameSquare:hover {
   background-color: rgba(0, 0, 0, 1);
   cursor: pointer;
 }
 
 #selection {
   width: 450px;
   margin-left: auto;
   margin-right: auto;
   margin-top: 20px;
   background-color: rgba(0, 0, 0, .85);
   border-radius: 20px;
   text-align: center;
 }
 
 .selection:hover {
   background-color: rgba(0, 0, 0, 1);
   cursor: pointer;
   border-radius: 20px;
 }
 
 .difficulty {
   background-color: rgba(255, 0, 0, 1);
   border-radius: 20px;
 }
 
 #playAgain {
   display: none;
   width: 450px;
   margin-left: auto;
   margin-right: auto;
   margin-top: 20px;
   background-color: rgba(0, 0, 0, .85);
   border-radius: 20px;
   text-align: center;
 }
 
 #anotherPlay {
   height: 60px;
 }
 
 #anotherPlay > h3 {
   line-height: 60px;
 }
 
 #anotherPlay:hover {
   background-color: rgba(0, 0, 0, 1);
   cursor: pointer;
   border-radius: 20px;
 }
 
 #compThink {
   display: none;
   margin-top: 10px;
   margin-bottom: 10px;
   margin-left: auto;
   margin-right: auto;
 }
              
            
!

JS

              
                $(document).ready(function() {
  var difficulty = "medium";
  var playerPiece;
  var computerPiece;
  var playerTurn;
  var win;
  var squareChosen;
  var playerScore = 0;
  var computerScore = 0;
  var playerTiles = [];
  var computerTiles = [];
  var winningCombos = [
    ["A1", "A2", "A3"],
    ["B1", "B2", "B3"],
    ["C1", "C2", "C3"],
    ["A1", "B1", "C1"],
    ["A2", "B2", "C2"],
    ["A3", "B3", "C3"],
    ["A1", "B2", "C3"],
    ["A3", "B2", "C1"]
  ];
  var randomChoice = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
  //PIECE SELECTION
  $("#X").click(function() {
    playerPiece = "X";
    computerPiece = "O";
    $("#selection").fadeOut(200);
    setTimeout(function() {
      $("#gameContainer").fadeIn(200);
    }, 200);
    firstMove();
  });
  $("#O").click(function() {
    playerPiece = "O";
    computerPiece = "X";
    $("#selection").fadeOut(200);
    setTimeout(function() {
      $("#gameContainer").fadeIn(200);
    }, 200);
    firstMove();
  });
  //DIFFICULTY
  $("#noob").click(function() {
    difficulty = "noob";
    $("#medium").removeClass("difficulty");
    $("#noob").addClass("difficulty");
  });
  $("#medium").click(function() {
    difficulty = "medium";
    $("#noob").removeClass("difficulty");
    $("#medium").addClass("difficulty");
  });
  //FIRST MOVE
  function firstMove() {
    if (playerPiece === "X") {
      playerTurn = true;
      turnUpdate();
    } else {
      turnUpdate();
      setTimeout(computerMove, 1500);
    }
  }
  //PLAYER MOVE
  $(".gameSquare").click(function(event) {
    if (playerTurn) {
      if ($("#" + event.target.id + " > h2").html() !== "") {
        alert("Choose an empty square!");
      } else {
        squareChosen = event.target.id;
        $("h2", this).html(playerPiece);
        updateRandomChoice();
        playerTiles.push(squareChosen);
        checkWin();
        if (!win) {
          playerTurn = false;
          turnUpdate();
          setTimeout(computerMove, 1500);
        }
      }
    } else {
      alert("Wait for your turn!");
    }
  });
  //COMPUTER MOVE
  function computerMove() {
    if (difficulty === "noob") {
      // NOOB MODE
      squareChosen = randomChoice[Math.floor(Math.random() * randomChoice.length)];
    } else {
      //MEDIUM MODE WORK IN PROGRESS
      var moveMade = false;
      for (var i = 0; i < winningCombos.length; i++) {
        var comp2x = winningCombos[i].filter(function(val) {
          return computerTiles.indexOf(val) < 0;
        });
        //IF COMP HAS 2 IN ROW WITH AN EMPTY SQUARE IT TAKES THAT SQUARE
        if (comp2x.length < 2 && $("#" + comp2x + " > h2").html() === "") {
          squareChosen = comp2x[0];
          moveMade = true;
          break;
        }
      }
      //IF IT DOESN'T HAVE 2 IN ROW NEXT WILL CHECK IF PLAYER DOES AND TRY TO BLOCK
      if (!moveMade) {
        for (var j = 0; j < winningCombos.length; j++) {
          var player2x = winningCombos[j].filter(function(val) {
            return playerTiles.indexOf(val) < 0;
          });
          if (player2x.length < 2 && $("#" + player2x + " > h2").html() === "") {
            squareChosen = player2x[0];
            moveMade = true;
            break;
          }
        }
      }
      //IF PLAYER DOES NOT HAVE 2 IN ROW NEXT WILL CHECK IF IT HAS 1 WITH 2 FREE SPACES
      if (!moveMade) {
        for (var k = 0; k < winningCombos.length; k++) {
          var comp1x = winningCombos[k].filter(function(val) {
            return computerTiles.indexOf(val) < 0;
          });
          if (comp1x.length < 3 && $("#" + comp1x[0] + " > h2").html() === "" && $("#" + comp1x[1] + " > h2").html() === "") {
            squareChosen = comp1x[0];
            moveMade = true;
            break;
          }
        }
      }
      if (!moveMade) {
        squareChosen = randomChoice[Math.floor(Math.random() * randomChoice.length)];
      }
    }
    // execute this code last
    computerTiles.push(squareChosen);
    $("h2", "#" + squareChosen).html(computerPiece);
    updateRandomChoice();
    checkWin();
    if (!win) {
      playerTurn = true;
      turnUpdate();
    }
  }
  //UPDATE RANDOMCHOICE
  function updateRandomChoice() {
    var index = randomChoice.indexOf(squareChosen);
    randomChoice.splice(index, 1);
  }
  //TURN INDICATOR
  function turnUpdate() {
    if (playerTurn) {
      $("#turn").html("Your turn " + playerPiece);
      $("#compThink").fadeOut(200);
    } else {
      $("#turn").html("Computer's turn " + computerPiece);
      $("#compThink").fadeIn(200);
    }
  }
  //PLAY AGAIN
  $("#anotherPlay").click(function() {
    $("#playAgain").fadeOut(200);
    setTimeout(function() {
      $("#gameContainer").fadeIn(200);
    }, 200);
    resetBoard();
  });
  //FUNCTION TO CHECK FOR WINNER
  function checkWin() {
    var tiles;
    if (playerTurn) {
      tiles = playerTiles;
    } else {
      tiles = computerTiles;
    }
    for (var i = 0; i < winningCombos.length; i++) {
      var remainingTiles = winningCombos[i].filter(function(val) {
        return tiles.indexOf(val) < 0;
      });
      if (remainingTiles.length === 0) {
        win = true;
        scoreUpdate();
      }
    }
    if (!win && randomChoice.length === 0) {
      tie();
    }
  }
  //SCORE UPDATE
  $("#score").html("You: " + playerScore + "<br>Computer: " + computerScore);

  function scoreUpdate() {
    if (playerTurn) {
      playerScore++;
      $("#score").html("You: " + playerScore + "<br>Computer: " + computerScore);
      $("#win_loseMessage").html("You are too good!");
      $("#win_loseFace").attr("src", "https://i.imgsafe.org/ce47747fde.png");
      setTimeout(function() {
        $("#gameContainer").fadeOut(200);
        setTimeout(function() {
          $("#playAgain").fadeIn(200);
        }, 200)
      }, 1500);
    } else {
      computerScore++;
      $("#score").html("You: " + playerScore + "<br>Computer: " + computerScore);
      $("#win_loseMessage").html("Better luck next time!");
      $("#win_loseFace").attr("src", "https://i.imgsafe.org/ce4781cceb.png");
      setTimeout(function() {
        $("#gameContainer").fadeOut(200);
        setTimeout(function() {
          $("#playAgain").fadeIn(200);
        }, 200)
      }, 1500);
    }
  }
  //TIE GAME
  function tie() {
    $("#win_loseMessage").html("Tie game!");
    $("#win_loseFace").attr("src", "https://i.imgsafe.org/ce4765d1a3.png");
    setTimeout(function() {
      $("#gameContainer").fadeOut(200);
      setTimeout(function() {
        $("#playAgain").fadeIn(200);
      }, 200)
    }, 1500);
  }
  //RESET BOARD
  function resetBoard() {
    randomChoice = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
    playerTiles = [];
    computerTiles = [];
    win = "";
    $("#A1 > h2").html("");
    $("#A2 > h2").html("");
    $("#A3 > h2").html("");
    $("#B1 > h2").html("");
    $("#B2 > h2").html("");
    $("#B3 > h2").html("");
    $("#C1 > h2").html("");
    $("#C2 > h2").html("");
    $("#C3 > h2").html("");
    turnUpdate();
    firstMove();
  }
});
              
            
!
999px

Console