Copied from flowroom

December, 2020
Photo by Flickr

Interesting!

import 'markup.html';
import 'styles.css';

// 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://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png';
    currentBeastStatusImg.alt = 'unicorn';
  } else {
    currentBeastStatusImg.src = 'https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.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://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png' : 'https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.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();
import 'markup.html';
import 'styles.css';

// 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://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png';
    currentBeastStatusImg.alt = 'unicorn';
  } else {
    currentBeastStatusImg.src = 'https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.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://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png' : 'https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.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();
Copy
<div class="wrapper">
    <h1>Tic Tac Toe <br/> Biden vs Trump</h1>
    <div class="current-status" id="currentStatus">
      <img src="https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.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>
<div class="wrapper">
    <h1>Tic Tac Toe <br/> Biden vs Trump</h1>
    <div class="current-status" id="currentStatus">
      <img src="https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.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>
Copy
/* --- Universal Box Sizing with Inheritance --- */
@import url("https://fonts.googleapis.com/css2?family=Bungee+Inline&display=swap");
html {
  box-sizing: border-box;
}

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

: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;
}

@media (min-width: 600px) {
  :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;
  }
}
@media (min-width: 1024px) {
  :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://images.unsplash.com/photo-1510265382668-7b564935d7b5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2090&q=80");
  background-size: cover;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  font-family: 'Bungee Inline', cursive;
  color: #f5f5f5;
}

h1 {
  position: relative;
  max-width: 100%;
  margin: 15px auto 5%;
  text-align: center;
  font-size: 2em;
  text-shadow: 3px 3px #55acee;
}
h1::before, h1::after {
  content: '';
  display: block;
  position: absolute;
  background-repeat: no-repeat;
  background-size: 100%;
  top: 50%;
  transform: translateY(-50%);
}
h1::before {
  background-image: url("https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.png");
  left: 0;
  width: calc( 93px / 2.1 );
  height: calc( 96px / 2.1 );
}
@media (min-width: 1024px) {
  h1::before {
    left: -64px;
  }
}
h1::after {
  background-image: url("https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png");
  right: 0;
  width: calc( 91px / 2 );
  height: calc( 88px / 2 );
}
@media (min-width: 1024px) {
  h1::after {
    right: -64px;
  }
}
h1 span {
  display: block;
  font-size: var(--h1-span-font-size);
}
@media (min-width: 1024px) {
  h1 span {
    display: inline;
  }
}

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

.current-status {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 25px;
}
@media (min-width: 1024px) {
  .current-status {
    margin-bottom: 35px;
  }
}
.current-status p {
  margin: 0 5px 0 0;
  font-size: var(--current-status-font-size);
}
.current-status 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;
}
.board.unicorn .cell:not(.dragon):not(.unicorn):hover::before, .board.dragon .cell:not(.dragon):not(.unicorn):hover::before {
  content: '';
  width: 70%;
  height: 70%;
  display: block;
  position: absolute;
  background-repeat: no-repeat;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  background-size: contain;
  opacity: 50%;
}
.board.unicorn .cell:not(.dragon):hover::before {
  background-image: url("https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png");
}
.board.dragon .cell:not(.unicorn):hover::before {
  background-image: url("https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.png");
}

.cell {
  cursor: pointer;
  position: relative;
  background-color: #f5f5f5;
  width: var(--cell-size);
  height: var(--cell-size);
  opacity: 0.5;
  transition: opacity 0.2s ease-in-out;
}
.cell:hover {
  opacity: 1;
}
.cell.dragon, .cell.unicorn {
  opacity: 1;
  position: relative;
  cursor: not-allowed;
}
.cell.dragon::before, .cell.unicorn::before {
  content: '';
  width: 70%;
  height: 70%;
  display: block;
  position: absolute;
  background-repeat: no-repeat;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  background-size: contain;
}
.cell.dragon::before {
  background-image: url("https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.png");
}
.cell.unicorn::before {
  background-image: url("https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png");
}

.game-end-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #0d1021;
}
.game-end-overlay.show {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.winning-message {
  margin: -50px 0 20px;
}
.winning-message img {
  width: var(--winning-beast-img-width);
}
.winning-message p {
  font-size: var(--winning-msg-font-size);
  margin: 0;
}

.btn-container {
  position: relative;
}

.reset-button {
  color: #f5f5f5;
  font-family: 'Bungee Inline', cursive;
  font-size: var(--reset-btn-font-size);
  white-space: nowrap;
  border: none;
  padding: 10px 20px;
  background-color: #a186be;
  box-shadow: 5px 5px 0 #55acee;
  cursor: pointer;
  transition: transform 0.1s ease-in-out;
  position: relative;
}
.reset-button:hover {
  transform: scale(1.2);
}
.reset-button:active {
  top: 6px;
  left: 6px;
  box-shadow: none;
  background-color: #9475b5;
}
/* --- Universal Box Sizing with Inheritance --- */
@import url("https://fonts.googleapis.com/css2?family=Bungee+Inline&display=swap");
html {
  box-sizing: border-box;
}

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

: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;
}

@media (min-width: 600px) {
  :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;
  }
}
@media (min-width: 1024px) {
  :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://images.unsplash.com/photo-1510265382668-7b564935d7b5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2090&q=80");
  background-size: cover;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  font-family: 'Bungee Inline', cursive;
  color: #f5f5f5;
}

h1 {
  position: relative;
  max-width: 100%;
  margin: 15px auto 5%;
  text-align: center;
  font-size: 2em;
  text-shadow: 3px 3px #55acee;
}
h1::before, h1::after {
  content: '';
  display: block;
  position: absolute;
  background-repeat: no-repeat;
  background-size: 100%;
  top: 50%;
  transform: translateY(-50%);
}
h1::before {
  background-image: url("https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.png");
  left: 0;
  width: calc( 93px / 2.1 );
  height: calc( 96px / 2.1 );
}
@media (min-width: 1024px) {
  h1::before {
    left: -64px;
  }
}
h1::after {
  background-image: url("https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png");
  right: 0;
  width: calc( 91px / 2 );
  height: calc( 88px / 2 );
}
@media (min-width: 1024px) {
  h1::after {
    right: -64px;
  }
}
h1 span {
  display: block;
  font-size: var(--h1-span-font-size);
}
@media (min-width: 1024px) {
  h1 span {
    display: inline;
  }
}

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

.current-status {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 25px;
}
@media (min-width: 1024px) {
  .current-status {
    margin-bottom: 35px;
  }
}
.current-status p {
  margin: 0 5px 0 0;
  font-size: var(--current-status-font-size);
}
.current-status 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;
}
.board.unicorn .cell:not(.dragon):not(.unicorn):hover::before, .board.dragon .cell:not(.dragon):not(.unicorn):hover::before {
  content: '';
  width: 70%;
  height: 70%;
  display: block;
  position: absolute;
  background-repeat: no-repeat;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  background-size: contain;
  opacity: 50%;
}
.board.unicorn .cell:not(.dragon):hover::before {
  background-image: url("https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png");
}
.board.dragon .cell:not(.unicorn):hover::before {
  background-image: url("https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.png");
}

.cell {
  cursor: pointer;
  position: relative;
  background-color: #f5f5f5;
  width: var(--cell-size);
  height: var(--cell-size);
  opacity: 0.5;
  transition: opacity 0.2s ease-in-out;
}
.cell:hover {
  opacity: 1;
}
.cell.dragon, .cell.unicorn {
  opacity: 1;
  position: relative;
  cursor: not-allowed;
}
.cell.dragon::before, .cell.unicorn::before {
  content: '';
  width: 70%;
  height: 70%;
  display: block;
  position: absolute;
  background-repeat: no-repeat;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  background-size: contain;
}
.cell.dragon::before {
  background-image: url("https://politico.com/interactives/uploads/2020-elections/headshots/png/300/joe-biden.png");
}
.cell.unicorn::before {
  background-image: url("https://pngimg.com/uploads/donald_trump/donald_trump_PNG29.png");
}

.game-end-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #0d1021;
}
.game-end-overlay.show {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.winning-message {
  margin: -50px 0 20px;
}
.winning-message img {
  width: var(--winning-beast-img-width);
}
.winning-message p {
  font-size: var(--winning-msg-font-size);
  margin: 0;
}

.btn-container {
  position: relative;
}

.reset-button {
  color: #f5f5f5;
  font-family: 'Bungee Inline', cursive;
  font-size: var(--reset-btn-font-size);
  white-space: nowrap;
  border: none;
  padding: 10px 20px;
  background-color: #a186be;
  box-shadow: 5px 5px 0 #55acee;
  cursor: pointer;
  transition: transform 0.1s ease-in-out;
  position: relative;
}
.reset-button:hover {
  transform: scale(1.2);
}
.reset-button:active {
  top: 6px;
  left: 6px;
  box-shadow: none;
  background-color: #9475b5;
}
Copy

More by
the same passionate developer