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

              
                <div class="container">
  <h1 id="heading">Play</h1>
  <h2 id="strategy"></h2>
  <button id="restart">Restart</button>
  <div id="board">
    <div class="box" id="0"></div>
    <div class="box" id="1"></div>
    <div class="box" id="2"></div>
    <div class="box" id="3"></div>
    <div class="box" id="4"></div>
    <div class="box" id="5"></div>
    <div class="box" id="6"></div>
    <div class="box" id="7"></div>
    <div class="box" id="8"></div>
  </div>
</div>
              
            
!

CSS

              
                :root {
  --text: #dbdfac;
  --bg: #2a1e5c;
  --btn-bg: #d7f171;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  color: var(--text);
  background-color: var(--bg);
  font-family: "Nerko One", cursive;
}

h1 {
  font-size: 80px;
  margin-bottom: 0;
  text-transform: uppercase;
}

.container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

#board {
  width: 60vw;
  display: flex;
  flex-wrap: wrap;
  font-size: 100px;
  margin-top: 40px;
}

.box {
  height: 20vh;
  width: 20vw;
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--text);
}

#restart {
  padding: 10px 20px;
  background-color: var(--btn-bg);
  border: none;
  border-radius: 20px;
  color: var(--bg);
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  text-transform: uppercase;
}

              
            
!

JS

              
                const boxes = document.querySelectorAll(".box");
const text = document.querySelector("#heading");
const strategy = document.querySelector("#strategy");
const restartBtn = document.querySelector("#restart");

const spaces = [];
const tick_circle = "O";
const tick_x = "X";
let currentPlayer = tick_circle;

const drawBoard = () => {
  boxes.forEach((box, i) => {
    let styleString = "";
    if (i < 3) {
      styleString += "border-bottom: 3px solid var(--text);";
    }
    if (i % 3 === 0) {
      styleString += "border-right: 3px solid var(--text);";
    }
    if (i % 3 === 2) {
      styleString += "border-left: 3px solid var(--text);";
    }
    if (i > 5) {
      styleString += "border-top: 3px solid var(--text);";
    }
    box.style = styleString;
    box.addEventListener("click", boxClicked);
  });
};

const boxClicked = (e) => {
  const id = e.target.id;
  console.log(e);
  if (!spaces[id]) {
    console.log(spaces[id]);
    spaces[id] = currentPlayer;
    e.target.innerText = currentPlayer;

    if (playerWon()) {
      text.innerText = `${currentPlayer} has won!`;
      restart();
      return;
    }

    if (playerDraw()) {
      return;
    }
    currentPlayer = currentPlayer === tick_circle ? tick_x : tick_circle;
  }
};

const playerWon = () => {
  if (spaces[0] === currentPlayer) {
    if (spaces[1] === currentPlayer && spaces[2] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins up to top`;
      return true;
    }
    if (spaces[3] === currentPlayer && spaces[6] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins on the left`;
      return true;
    }
    if (spaces[4] === currentPlayer && spaces[8] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins diagonally`;
      return true;
    }
  }
  if (spaces[8] === currentPlayer) {
    if (spaces[2] === currentPlayer && spaces[5] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins on the right`;
      return true;
    }
    if (spaces[6] === currentPlayer && spaces[7] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins on the bottom`;
      return true;
    }
  }
  if (spaces[4] === currentPlayer) {
    if (spaces[1] === currentPlayer && spaces[7] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins vertically on middle`;
      return true;
    }
    if (spaces[3] === currentPlayer && spaces[5] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins horizontally on the middle`;
      return true;
    }
    if (spaces[2] === currentPlayer && spaces[6] === currentPlayer) {
      strategy.innerText = `${currentPlayer} wins diagonally`;
      return true;
    }
  }
};

const playerDraw = () => {
  let draw = 0;
  spaces.forEach((space, i) => {
    if (spaces[i] !== null) draw++;
  });
  if (draw === 9) {
    text.innerText = `Draw`;
    restart();
  }
};

const restart = () => {
  setTimeout(() => {
    spaces.forEach((space, i) => {
      spaces[i] = null;
    });
    boxes.forEach((box) => {
      box.innerText = "";
    });
    text.innerText = `Play`;
    strategy.innerText = ``;
  }, 1000);
};
restartBtn.addEventListener("click", restart);
restart();
drawBoard();

              
            
!
999px

Console