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 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.

HTML

              
                <!--chosing sign box-->
<div id="dark" class="hidden">
  <div id="box">
    <p id="playX" data-sign="x">Play X</p>
    <p id="message" class="hidden"></p>
    <p id="playO" data-sign="o">Play O</p>
  </div>
</div>

<!-- title -->
<header>
  <h1>Tic Tac Toe</h1>
</header>

<!--OXO-->
<section class="main">
  <div class="playable play" id="0"></div>
  <div class="playable play" id="1"></div>
  <div class="playable play" id="2"></div>
  <div class="playable play" id="3"></div>
  <div class="playable play" id="4"></div>
  <div class="playable play" id="5"></div>
  <div class="playable play" id="6"></div>
  <div class="playable play" id="7"></div>
  <div class="playable play" id="8"></div>
</section>

<!-- footer -->
<footer>
  <p>Created by <a href="https://remybeumier.be" target="_blank">Rémy Beumier</a></p>
</footer>

              
            
!

CSS

              
                
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  text-align: center;
  background: #f1c40f;
  background-image: linear-gradient(to top left, #d35400, #f1c40f);
}

/* Modal window */
#dark {
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.5);
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 100;
}

#dark #box {
  display: flex;
  flex-flow: column;
  justify-content: space-around;
  align-items: center;
  width: 220px;
/*   height: 200px; */
  padding: 20px;
  background: whitesmoke;
  border-radius: 2px;
}

#box #playX, #box #playO, #box #message {
  min-width: 120px;
  margin: 10px;
  padding: 10px 30px;
  border: solid #EAA80B 1.5px;
  border-radius: 2px;
  color: #EAA80B;
  font-size: 20px;
  cursor: pointer;
  white-space: nowrap;
}
#box #playX:hover, #box #playO:hover {
  background: #EAA80B;
  color: whitesmoke;
}

/* title */
header h1 {
  letter-spacing: 0.2em;
  color: whitesmoke;
  font-size: 34px;
  padding: 20px 10px;
  font-weight: normal;
}
@media only screen and (min-width: 768px) {
  header h1 {
    font-size: 42px;
  }
}

/* main square */
.main {
  width: 262px;
  height: 262px;
  padding: 5px;
  margin: auto;
  background: whitesmoke;
  border-radius: 2px;
}
@media only screen and (min-width: 768px) {
  .main {
    width: 332px;
    height: 332px;
    padding: 10px;
  }
}

/* inside orange squares */
.main div {
  width: 80px;
  height: 80px;
  margin: 2px;
  background: #f1c40f;
  background-image: linear-gradient(to top left, #d35400, #f1c40f);
  background-size: 200%;
  float: left;
  border-radius: 2px;
  color: whitesmoke;
  font-size: 60px;
  line-height: 80px;
  font-family: monospace;
  text-transform: uppercase;
}
@media only screen and (min-width: 768px) {
  .main div {
    width: 100px;
    height: 100px;
    margin: 2px;
    line-height: 100px;
  }
}
.main .playable:hover {
  opacity: 0.6;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

footer {
  clear: both;
  padding: 5px;
  text-align: center;
  color: whitesmoke;
  font-size: 14px;
}
footer a {
  color: #333;
  text-decoration: none;
}
footer a:hover {
  color: whitesmoke;
  text-decoration: underline;
}

              
            
!

JS

              
                
// TODO create AI

var playerSign = "",
    compSign = "",
    grid = ["e","e","e", "e","e","e", "e","e","e"],
    turn = 0,
    next = 1;

var dark = document.querySelector("#dark"),
    message = document.querySelector("#message"),
    playX = document.querySelector("#playX"),
    playO = document.querySelector("#playO"),
    plays = document.querySelectorAll(".play");

playX.addEventListener("click", startGame);
playO.addEventListener("click", startGame);

// choose sign
function startGame(e) {
  clearInterval(checkPageInterval);
  dark.classList.add("hidden");
  playerSign = e.target.getAttribute("data-sign");
  compSign = playerSign == "x" ? "o" : "x";
  for (var i=0; i<plays.length; i++) {
    plays[i].addEventListener("click", playerMove);
  }
}

// player move
function playerMove(e) {
  e.target.innerHTML = playerSign;
  e.target.classList.remove("playable");
  grid[e.target.id] = playerSign;
  turn++;
  checker("player");
  if (turn < 8 && next) {
    setTimeout(function() {
      compMove();
    }, 200);
  }
  e.target.removeEventListener("click", playerMove);
}

// AI move
function compMove() {
  var randd = rand(plays.length);
  while (grid[randd] === playerSign || grid[randd] === compSign) {
    randd = rand(plays.length);
  }
  plays[randd].innerHTML = compSign;
  plays[randd].classList.remove("playable");
  plays[randd].removeEventListener("click", playerMove);
  grid[randd] = compSign;
  turn++;
  setTimeout(function() {
    checker("comp");
  }, 100);
}

// end game checker (win, lose, tie)
function checker(whoo) {
  // win
  if (    (whoo === "player") 
      && ((grid[0]===playerSign && grid[1]===playerSign && grid[2]===playerSign)
      ||  (grid[3]===playerSign && grid[4]===playerSign && grid[5]===playerSign)
      ||  (grid[6]===playerSign && grid[7]===playerSign && grid[8]===playerSign)
      ||  (grid[0]===playerSign && grid[3]===playerSign && grid[6]===playerSign)
      ||  (grid[1]===playerSign && grid[4]===playerSign && grid[7]===playerSign)
      ||  (grid[2]===playerSign && grid[5]===playerSign && grid[8]===playerSign)
      ||  (grid[0]===playerSign && grid[4]===playerSign && grid[8]===playerSign)
      ||  (grid[2]===playerSign && grid[4]===playerSign && grid[6]===playerSign))
    ) {
    next = 0;
    setTimeout(function() {
      reset("You win !");
    }, 500);
  }
  // lose
  else if ( (whoo === "comp") 
        && ((grid[0]===compSign && grid[1]===compSign && grid[2]===compSign)
        ||  (grid[3]===compSign && grid[4]===compSign && grid[5]===compSign)
        ||  (grid[6]===compSign && grid[7]===compSign && grid[8]===compSign)
        ||  (grid[0]===compSign && grid[3]===compSign && grid[6]===compSign)
        ||  (grid[1]===compSign && grid[4]===compSign && grid[7]===compSign)
        ||  (grid[2]===compSign && grid[5]===compSign && grid[8]===compSign)
        ||  (grid[0]===compSign && grid[4]===compSign && grid[8]===compSign)
        ||  (grid[2]===compSign && grid[4]===compSign && grid[6]===compSign))
    ) {
    setTimeout(function() {
      reset("You lose !");
    }, 500);
  }
  // tie
  else if (turn === 9) {
    setTimeout(function() {
      reset("You tied !");
    }, 500);
  }
}

// reset func
function reset(msg) {
  playX.classList.add("hidden");
  playO.classList.add("hidden");
  message.textContent = msg;
  message.classList.remove("hidden");
  dark.classList.remove("hidden");
  grid = ["e","e","e", "e","e","e", "e","e","e"];
  turn = 0;
  next = 1;
  for (var j=0; j<plays.length; j++) {
    plays[j].innerHTML = "";
    plays[j].classList.add("playable");
    plays[j].removeEventListener("click", playerMove);
  }
  setTimeout(function() {
    playX.classList.remove("hidden");
    playO.classList.remove("hidden");
    message.classList.add("hidden");
  }, 1500);
}

// random func
function rand(num) {
  return Math.floor(Math.random() * num); 
}

// handle focus of the page
function checkPageFocus() {
  if (document.hasFocus()) {
    dark.classList.remove("hidden");
  }
  else {
    dark.classList.add("hidden");
  }
}
var checkPageInterval = setInterval(checkPageFocus, 300);

              
            
!
999px

Console