Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="wrapper">
    <h1>dragons <span>vs</span> unicorns</h1>
    <div class="current-status" id="currentStatus">
      <img src="https://assets.codepen.io/2558758/unicorn.png" alt="" id="currentBeastImg">
      <p>'s turn</p>
    </div>
    <div class="board" id="board">
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
      <div class="cell" data-cell></div>
    </div>
    <div class="game-end-overlay" id="gameEndOverlay">
      <div class="winning-message" data-winning-message>
        <p></p>
      </div>
      <div class="btn-container">
        <button class="reset-button" id="resetButton">play again</button>
      </div>
    </div>
  </div>

              
            
!

CSS

              
                /* --- Universal Box Sizing with Inheritance --- */
html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

$blue: #55acee;
$purple: #a186be;
$white: #f5f5f5;
$navy: #0d1021;

:root {
  --cell-size: 90px;
  --grid-gap: 12px;
  --h1-font-size: 32px;
  --h1-span-font-size: 24px;
  --current-status-font-size: 24px;
  --current-beast-img-height: 32px;
  --winning-beast-img-width: 100px;
  --winning-msg-font-size: 48px;
  --reset-btn-font-size: 30px;
}

@import url('https://fonts.googleapis.com/css2?family=Bungee+Inline&display=swap');

@mixin mq( $size ) {
  @if $size == tablet {
    @media ( min-width: 600px ) { @content; }
  } @else if $size == desktop {
    @media ( min-width: 1024px ) { @content; }
  }
}

@mixin pseduo-div( $w, $h ) {
  content: '';
  width: $w;
  height: $h;
  display: block;
  position: absolute;
  background-repeat: no-repeat;
}

@mixin align-center() {
  top: 50%;
  left: 50%;
  transform: translate3d( -50%, -50%, 0 );
}

@include mq( tablet ) {
  :root {
    --cell-size: 155px;
    --grid-gap: 15px;
    --h1-font-size: 56px;
    --h1-span-font-size: 40px;
    --current-status-font-size: 36px;
    --current-beast-img-height: 45px;
  }
}

@include mq( desktop ) {
  :root {
    --h1-font-size: 64px;
    --h1-span-font-size: 48px;
    --winning-msg-font-size: 72px;
    --reset-btn-font-size: 48px;
    --winning-beast-img-width: 150px;
  }
}

/* --- Styles --- */
body {
  margin: 0;
  padding: 0;
  background-image: url( 'https://raw.githubusercontent.com/anniebombanie/tic-tac-toe/master/img/fantasy-bg.jpg' );
  background-size: cover;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  font-family: 'Bungee Inline', cursive;
  color: $white;
}

h1 {
  position: relative;
  max-width: 100%;
  margin: 15px auto 5%;
  text-align: center;
  font-size: var( --h1-font-size );
  text-shadow: 3px 3px $blue;

  &::before,
  &::after {
    @include pseduo-div( null, null );
    background-size: 100%;
    top: 50%;
    transform: translateY( -50% );
  }

  &::before {
    background-image: url( 'https://assets.codepen.io/2558758/fire.png' );
    left: 0;
    width: calc( 93px / 2.1 ) ;
    height: calc( 96px / 2.1 );

    @include mq( desktop ) {
      left: -64px;
    }
  }

  &::after {
    background-image: url( 'https://assets.codepen.io/2558758/stars.png' );
    right: 0;
    width: calc( 91px / 2 );
    height: calc( 88px / 2 );

    @include mq( desktop ) {
      right: -64px;
    }
  }

  span {
    display: block;
    font-size: var( --h1-span-font-size );

    @include mq( desktop ) {
      display: inline;
    }
  }
}

h1,
p {
  letter-spacing: 1px;
}

.current-status {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 25px;

  @include mq( desktop ) {
    margin-bottom: 35px;
  }

  p {
    margin: 0 5px 0 0;
    font-size: var( --current-status-font-size );
  }

  img {
    width: auto;
    height: var( --current-beast-img-height );
  }
}

.board {
  display: grid;
  grid-template-columns: repeat( 3, minmax( 90px, 1fr ));
  grid-template-rows: repeat( 3, minmax( 90px, 1fr ));
  grid-gap: var( --grid-gap );
  width: 100%;
  height: 100%;
  max-width: 495px;
  margin: 0 auto 15px;

  &.unicorn,
  &.dragon {
    .cell:not( .dragon ):not( .unicorn ):hover::before {
      @include pseduo-div( 70%, 70% );
      @include align-center();
      background-size: contain;
      opacity: 50%;
    }
  }

  &.unicorn {
    .cell:not( .dragon ):hover::before {
      background-image: url( 'https://assets.codepen.io/2558758/unicorn.png' );
    }
  }

  &.dragon {
    .cell:not( .unicorn ):hover::before {
      background-image: url( 'https://assets.codepen.io/2558758/dragon.png' );
    }
  }
}

.cell {
  cursor: pointer;
  position: relative;
  background-color: $white;
  width: var( --cell-size );
  height: var( --cell-size );
  opacity: 0.5;
  transition: opacity 0.2s ease-in-out;

  &:hover {
    opacity: 1;
  }

  &.dragon,
  &.unicorn {
    opacity: 1;
    position: relative;
    cursor: not-allowed;

    &::before {
      @include pseduo-div( 70%, 70% );
      @include align-center();
      background-size: contain;
    }
  }

  &.dragon::before {
    background-image: url( 'https://assets.codepen.io/2558758/dragon.png' );
  }

  &.unicorn::before {
    background-image: url( 'https://assets.codepen.io/2558758/unicorn.png' );
  }
}

.game-end-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: $navy;

  &.show {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
}

.winning-message {
  margin: -50px 0 20px;

  img {
    width: var( --winning-beast-img-width );
  }

  p {
    font-size: var( --winning-msg-font-size );
    margin: 0;
  }
}

.btn-container {
  position: relative;
}

.reset-button {
  color: $white;
  font-family: 'Bungee Inline', cursive;
  font-size: var( --reset-btn-font-size );
  white-space: nowrap;
  border: none;
  padding: 10px 20px;
  background-color: $purple;
  box-shadow: 5px 5px 0 $blue;
  cursor: pointer;
  transition: transform 0.1s ease-in-out;
  position: relative;

  &:hover {
    transform: scale( 1.2 );
  }

  &:active {
    top: 6px;
    left: 6px;
    box-shadow: none;
    background-color: darken( $purple, 5 );
  }
}

              
            
!

JS

              
                // Twitter @anniebombanie_

// -- HTML elements --
const board = document.getElementById( 'board' );
const cells = document.querySelectorAll( '[data-cell]' );
const currentStatus = document.getElementById( 'currentStatus' );
const resetButton = document.getElementById( 'resetButton' );
const gameEndOverlay = document.getElementById( 'gameEndOverlay' );
const currentBeastStatusImg = document.getElementById( 'currentBeastImg' );
const winningMessage = document.querySelector( '[data-winning-message]' );
const winningMessageText = document.querySelector( '[data-winning-message] p' );
const winningMessageImg = document.createElement( 'img' );

// -- Game Variables --
let gameIsLive = true;
let unicornTurn = true;
let winner = null;
const winningCombinations = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6]
];

// -- Functions --
const setBoardHoverClass = () => {
  board.classList.remove( 'unicorn' );
  board.classList.remove( 'dragon' );

  if ( unicornTurn ) {
    board.classList.add( 'unicorn' );
  } else {
    board.classList.add( 'dragon' );
  }
}

const placeBeastImg = ( cell, currentBeast ) => {
  cell.classList.add( currentBeast );
}

const swapTurns = () => {
  unicornTurn = !unicornTurn;
}

const updateCurrentStatus = () => {
  if ( unicornTurn ) {
    currentBeastStatusImg.src = 'https://assets.codepen.io/2558758/unicorn.png';
    currentBeastStatusImg.alt = 'unicorn';
  } else {
    currentBeastStatusImg.src = 'https://assets.codepen.io/2558758/dragon.png';
    currentBeastStatusImg.alt = 'dragon';
  }
}

const checkWin = ( currentBeast ) => {
  return winningCombinations.some( combination => {
    return combination.every( i => {
      return cells[i].classList.contains( currentBeast );
    })
  });
}

const isDraw = () => {
  return [...cells].every( cell => {
    return cell.classList.contains( 'unicorn' ) || cell.classList.contains( 'dragon' );
  })
}

const startGame = () => {
  cells.forEach( cell => {
    winningMessageImg.remove();
    cell.classList.remove( 'unicorn' );
    cell.classList.remove( 'dragon' );
    cell.removeEventListener( 'click', handleCellClick );
    cell.addEventListener( 'click', handleCellClick, { once: true });
  });

  setBoardHoverClass();
  gameEndOverlay.classList.remove( 'show' );
}

const endGame = ( draw ) => {
  if ( draw ) {
    winningMessageText.innerText = `draw!`;
  } else {
    winningMessageImg.src = unicornTurn ? 'https://assets.codepen.io/2558758/unicorn.png' : 'https://assets.codepen.io/2558758/dragon.png';
    winningMessageImg.alt = unicornTurn ? 'unicorn' : 'dragon';
    winningMessage.insertBefore( winningMessageImg, winningMessageText );
    winningMessageText.innerText = `wins!!!`
  }

  gameEndOverlay.classList.add( 'show' );
}

// -- Event Handler --
const handleCellClick = ( e ) => {
  const cell = e.target;
  const currentBeast = unicornTurn ? 'unicorn' : 'dragon';

  placeBeastImg( cell, currentBeast );
  if ( checkWin( currentBeast )) {
    endGame( false );
  } else if ( isDraw()) {
    endGame( true );
  } else {
    swapTurns();
    updateCurrentStatus();
    setBoardHoverClass();
  }
}

// -- Event Listener --
resetButton.addEventListener( 'click', startGame );

// -- Start Game --
startGame();

              
            
!
999px

Console