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

              
                <head>
<script src="//use.edgefonts.net/rammetto-one.js"></script>
</head>

<div class="all">
<div class="display">

  <!--   row 1 -->
  <div class="row">
    <div class="block" id="block1"><button id="block1"></button></div>
    <div class="block" id="block4"><button id="block4"></button></div>
    <div class="block" id="block7"><button id="block7"></button></div>
  </div>

  <!--   row 2 -->
  <div class="row">
    <div class="block" id="block2"><button id="block2"></button></div>
    <div class="block" id="block5"><button id="block5"></button></div>
    <div class="block" id="block8"><button id="block8"></button></div>
  </div>

  <!--   right  column -->
  <div class="row">
    <div class="block" id="block3"><button id="block3"></button></div>
    <div class="block" id="block6"><button id="block6"></button></div>
    <div class="block" id="block9"><button id="block9"></button></div>
  </div>

</div>
</div>
              
            
!

CSS

              
                .all {
   display: flex;
   justify-content: center;
}

.display {
  background-color: black;
  display: inline-flex;
  box-sizing: border-box;
  padding: 2px 4px;
  margin: 50px auto;
}

.block {
  font-family: rammetto-one, sans-serif;
  font-size: 5em;
  width: 150px;
  height: 150px;
  border-radius: 2px;
  background-color: yellow;
  margin: 4px 2px;
  text-align: center;
}

button {
  width: 150px;
  height: 150px;
  background-color: Transparent;
  background-repeat:no-repeat;
  border: none;
  cursor: pointer;
  overflow: hidden;
  outline: none;
}
              
            
!

JS

              
                /*
---------------
ENJOY
---------------
*/

//X or O, X === true, (human's)
var decision = true,
  //computer move
  move = "",
  //board squares
  outs = [1, 3, 7, 9],
  ins = [2, 4, 6, 8],
  //check to see if firstMove function was used
  firstDone = false,
  gameOver = false;

function addX(id) {
  $(id).html("X");
}

function addO(id) {
  $(id).html("O");
}

function firstMove() {
  //flag for for loop
  var found = false;
  if (decision) {
    move = "O";
  } else {
    move = "X";
  }
  for (var i = 0; i < outs.length; i++) {
    if ($("#block" + outs[i]).html() === "X" || $("#block" + outs[i]).html() === "O") {
      $("#block5").html(move);
      found = true;
    }
  }
  if (!found && $("#block5").html() !== "X" && $("#block5").html() !== "O") {
    $("#block5").html(move);
  } else if (!found) {
    ranIndex = Math.floor(Math.random() * ins.length);
    $("#block" + ins[ranIndex]).html(move);
  }
  firstDone = true;
}

//add computer choice to random -
//- open spot
function nextMoves() {
  //if X is human move
  var allBlocks = ins.concat(outs);
  for (var k = 0; k <= 100; k++) {
    var potentialBlock = allBlocks[Math.floor(Math.random() * allBlocks.length)]
    if ($("#block" + potentialBlock).html() !== "X" && $("#block" + potentialBlock).html() !== "O") {
      $("#block" + potentialBlock).html(move);
      break;
    }
  }
}

function checkWin() {
  if (decision)  {
    switch (true) {
      case $("#block1").html() === "X" && $("#block2").html() === "X" && $("#block3").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block4").html() === "X" && $("#block5").html() === "X" && $("#block6").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block7").html() === "X" && $("#block8").html() === "X" && $("#block9").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block1").html() === "X" && $("#block4").html() === "X" && $("#block7").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block2").html() === "X" && $("#block5").html() === "X" && $("#block8").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block3").html() === "X" && $("#block6").html() === "X" && $("#block9").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block1").html() === "X" && $("#block5").html() === "X" && $("#block9").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block3").html() === "X" && $("#block5").html() === "X" && $("#block7").html() === "X":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block1").html() === "O" && $("#block2").html() === "O" && $("#block3").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block4").html() === "O" && $("#block5").html() === "O" && $("#block6").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block7").html() === "O" && $("#block8").html() === "O" && $("#block9").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block1").html() === "O" && $("#block4").html() === "O" && $("#block7").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block2").html() === "O" && $("#block5").html() === "O" && $("#block8").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block3").html() === "O" && $("#block6").html() === "O" && $("#block9").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block1").html() === "O" && $("#block5").html() === "O" && $("#block9").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block3").html() === "O" && $("#block5").html() === "O" && $("#block7").html() === "O":
        confirm("You lose!");
        gameOver = true;
        break;
    }
  } else {
    switch (true) {
      case $("#block1").html() === "O" && $("#block2").html() === "O" && $("#block3").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block4").html() === "O" && $("#block5").html() === "O" && $("#block6").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block7").html() === "O" && $("#block8").html() === "O" && $("#block9").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block1").html() === "O" && $("#block4").html() === "O" && $("#block7").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block2").html() === "O" && $("#block5").html() === "O" && $("#block8").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block3").html() === "O" && $("#block6").html() === "O" && $("#block9").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block1").html() === "O" && $("#block5").html() === "O" && $("#block9").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block3").html() === "O" && $("#block5").html() === "O" && $("#block7").html() === "O":
        confirm("You win!");
        gameOver = true;
        break;
      case $("#block1").html() === "X" && $("#block2").html() === "X" && $("#block3").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block4").html() === "X" && $("#block5").html() === "X" && $("#block6").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block7").html() === "X" && $("#block8").html() === "X" && $("#block9").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block1").html() === "X" && $("#block4").html() === "X" && $("#block7").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block2").html() === "X" && $("#block5").html() === "X" && $("#block8").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block3").html() === "X" && $("#block6").html() === "X" && $("#block9").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block1").html() === "X" && $("#block5").html() === "X" && $("#block9").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
      case $("#block3").html() === "X" && $("#block5").html() === "X" && $("#block7").html() === "X":
        confirm("You lose!");
        gameOver = true;
        break;
    }
  }
}

function checkDraw() {
  //adds every block that has X
  // or O to the array. if array len
  // is 9, then all are full and
  // the game is a draw.
  if (!gameOver) {
  var drawCheckArray = [];
  for (var m = 1; m < 10; m++) {
    if ($("#block" + m).html() === "X" || $("#block" + m).html() === "O") {
      drawCheckArray.push(m);
    }
  }
  if (drawCheckArray.length === 9) {
    confirm("Draw!");
    gameOver = true;
  } else {
    drawCheckArray = [];
  }
  } else {
    drawCheckArray = [];
  }
}

function restartGame() {
  if (gameOver) {
    if (confirm("Would you like to play again?")) {
      firstDone = false,
      move = "",
      gameOver = false;
      for (var z = 1; z < 10; z++) {
        $("#block" + z).html("<button id=\"block" +  z + "\"></button>");
      }
      if (confirm("Press OK to be Xs, or Cancel to be Os")) {
  $("button").on("click", function() {
    addX("#" + this.id);
    if (!firstDone) {
      firstMove();
    } else {
      nextMoves();
      checkWin();
      checkDraw();
      restartGame();
    }
  })
} else {
  decision = false;
  $("button").on("click", function() {
    addO("#" + this.id);
    if (!firstDone) {
      firstMove();
    } else {
      nextMoves();
      checkWin();
      checkDraw();
      restartGame();
    }
  })
}
    }
  }
}

$(document).ready(function() {
  if (confirm("Press OK to be Xs, or Cancel to be Os")) {
  $("button").on("click", function() {
    addX("#" + this.id);
    if (!firstDone) {
      firstMove();
    } else {
      nextMoves();
      checkWin();
      checkDraw();
      restartGame();
    }
  })
} else {
  decision = false;
  $("button").on("click", function() {
    addO("#" + this.id);
    if (!firstDone) {
      firstMove();
    } else {
      nextMoves();
      checkWin();
      checkDraw();
      restartGame();
    }
  })
}
})
              
            
!
999px

Console