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

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">
    <div class="board">
      <div class="square" id="0"></div>
      <div class="square" id="1"></div>
      <div class="square" id="2"></div>
      <div class="square" id="3"></div>
      <div class="square" id="4"></div>
      <div class="square" id="5"></div>
      <div class="square" id="6"></div>
      <div class="square" id="7"></div>
      <div class="square" id="8"></div>
    </div>
    <footer>
      <span>For FreeCodeCamp by <a href="https://github.com/alexisnotalexa">@alexisnotalexa</a><span>
    </footer>
  </div>

  <div class="modal start-game">
    <span>Choose wisely:</span>
    <div class="modal__selector">
      <button class="selector" id="x">
        <i class="fa-inverse fa fa-times fa-5x"></i>
      </button>
      <button class="selector" id="o">
        <i class="fa-inverse fa fa-circle-o fa-5x"></i>
      </button>
    </div>
  </div>
  <div class="modal end-game" style="display:none;">
    <span id="emoji"></span>
    <span id="winner"></span>
    <button id="restart-game">Play Again</button>
  </div>
  <div class="modal-backdrop"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Open+Sans:300,700');

html,
body {
  margin: 0;
  padding: 0;
  background-color: #c3e9e2;
  font-family: 'Open Sans', sans-serif;
}

a, a:link, a:hover,
a:visited {
  color: white;
  font-size: 11px;
  letter-spacing: 1px;
}

i {
  text-shadow: 0 0 15px rgba(0,0,0,0.1);
}

footer {
  color: white;
  font-size: 11px;
  text-align: center;
  margin: .5rem;
}

.board {
  width: 500px;
  height: 500px;
  background-color: white;
  border-radius: 1rem;
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  grid-gap: .5rem;
  padding: 1rem;
}

.container {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.square {
  background-color: gainsboro;
  border-radius: .5rem;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background-color .3s linear;
}

.modal {
  background-color: white;
  border-radius: .5rem;
  box-shadow: 0 0 20px rgba(0,0,0,0.2);
  position: absolute;
  z-index: 2;
  padding: 3rem;
  width: 200px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  display: flex;
  flex-direction: column;
  text-align: center;
}

.modal > span {
  font-weight: 700;
  text-transform: uppercase;
  font-size: 1.5rem;
}

.modal-backdrop {
  background-color: rgba(0,0,0,0.2);
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 100%;
  opacity: 1;
  animation: fadein .5s ease-in;
}

.modal__selector {
  display: flex;
  justify-content: space-around;
  padding-top: 1rem;
}

.selector, .selector:focus {
  border: 0;
  border-radius: .5rem;
  cursor: pointer;
  width: 5rem;
  height: 5rem;
  outline: none;
}

.start-game {
  animation: slide-in .5s ease-in-out 1;
}

.symbol {
  animation: pop-in .5s ease-in-out 1;
}

#restart-game, #restart-game:focus {
  background-color: gold;
  border: 0;
  border-radius: 1rem;
  box-shadow: 0px 2px 0 rgba(0,0,0,0.05);
  font-size: .5rem;
  padding: .5rem;
  text-transform: uppercase;
  outline: none;
}

#restart-game:hover {
  background-color: #f2ce0d;
}

#winner {
  margin-bottom: 1.5rem;
}

#emoji {
  font-size: 3rem;
  animation: wave 1s ease-in-out 1;
}

#x {
  background-color: coral;
  transition: background-color 0.5s ease;
}
#o {
  background-color: cornflowerblue;
  transition: background-color 0.5s ease;
}

#x:hover { background-color: #e7593f; }
#o:hover { background-color: #5984d3; }

.red { background-color: coral; }
.blue { background-color: cornflowerblue; }

/*CSS ANIMATIONS*/
@keyframes pop-in {
  0% {transform: scale(0);}
  75% {transform: scale(1.3);}
  100% {transform: scale(1);}
}

@keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes slide-in {
  0% { margin-top: 30%; }
  25% { margin-top: -5%; }
  75% { margin-top: 2%; }
  100% { margin-top: 0; }
}

@keyframes wave {
  0%   { transform: rotate(0deg); }
  25%  { transform: rotate(-20deg); }
  50%  { transform: rotate(0deg); }
  75%  { transform: rotate(20deg); }
  100% { transform: rotate(0deg); }
}
              
            
!

JS

              
                /*
  Tic Tac Toe Game by Alexis Okamura
*/
var tictactoe = (function() {
  var compMove; // stores the index of the computer's move
  var player = { id: 1 }, comp = { id: -1 };
  var board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  var winningLines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
  ];

  /* Assigns player + comp's symbol according to what symbol they click */
  $('#x').on('click', function() {
    player.symbol = '<i class="fa-inverse fa fa-times fa-5x symbol"></i>';
    comp.symbol = '<i class="fa-inverse fa fa-circle-o fa-5x symbol"></i>';
    player.color = 'red';
    comp.color = 'blue';
    $('.modal-backdrop').hide();
    $('.start-game').hide();
    startGame();
  });

  /* Assigns player + comp's symbol according to what symbol they click */
  $('#o').on('click', function() {
    player.symbol = '<i class="fa-inverse fa fa-circle-o fa-5x symbol"></i>';
    comp.symbol = '<i class="fa-inverse fa fa-times fa-5x symbol"></i>';
    player.color = 'blue';
    comp.color = 'red';
    $('.modal-backdrop').hide();
    $('.start-game').hide();
    startGame();
  });

  function startGame() {
    resetGame();
    playersTurn();
  }

  /* Checks the current state of the board passed in */
  function check(state) {
    // counts how many moves have been played already
    var moves = state.reduce(function(prev, cur) {
      return Math.abs(prev) + Math.abs(cur);
    });

    // totals up and finds the current highest score
    var result = parseInt(winningLines.map(function(lines) {
      return lines.map(function(line) { // get value in each square
        return state[line];
      }).reduce(function(prev, cur) { // add up values on each line
        return prev + cur;
      });
    }).filter(function(total) { // find + return winning score
      return Math.abs(total) === 3;
    }).join());

    // checks moves + result
    if(result === 3) {
      return 'win';
    } else if(result === -3) {
      return 'lose';
    } else if(moves === 9) {
      return 'tie';
    } else {
      return false;
    }
  }

  /* Displays the outcome of the game */
  function gameOver(winner) {
    if(winner === 'win') {
      $('#winner').html('You won!');
      $('#emoji').html('🎉');
    } else if(winner === 'lose') {
      $('#winner').html('You lost!');
      $('#emoji').html('👎');
    } else {
      $('#winner').html('It was a tie');
      $('#emoji').text('🤷‍');
    }
    $('.modal-backdrop').show();
    $('.end-game').show();
    $('#restart-game').on('click', function() {
      $('.modal-backdrop').hide();
      $('.modal').hide();
      startGame();
    });
  }

  /* Resets the board to its original state */
  function resetGame() {
    board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
    // clears anything in each square
    $('.square').each(function() {
      $(this).empty();
      $(this).removeClass('blue red');
    });
  }

  function playersTurn() {
    $('.square').on('click', function(e) {
      if(board[e.target.id] === 0) {
        board[e.target.id] = player.id;
        $(this).addClass(player.color).html(player.symbol);
        check(board) ? gameOver(check(board)) : setTimeout(computersTurn, 300);
      }
    });
  }

  function computersTurn() {
    minmax(board, 'comp'); // runs minmax to find optimal move
    board[compMove] = comp.id;
    $(`#${compMove}`).addClass(comp.color).html(comp.symbol);
    check(board) ? gameOver(check(board)) : playersTurn();
  }

  /* Returns an array of available/empty squares */
  function emptySquares(state) {
    // return indexes of empty squares
    return state.map(function(square, index) {
      return square === 0 ? index : false;
    }).filter(function(index) {
      // filter through the indexes and only return valid ones
      return index !== false;
    });
  }

  // referenced min-max algorithm from here:
  // https://www.neverstopbuilding.com/blog/2013/12/13/tic-tac-toe-understanding-the-minimax-algorithm13/
  // and big help from: https://codepen.io/thepeted/full/qOJVyK/
  function minmax(state, player) {
    var result = check(state);
    if(result === 'lose') {
      return 10;
    } else if(result === 'win') {
      return -10;
    } else if(result === 'tie') {
      return 0;
    }

    var moves = [], scores = [];
    emptySquares(state).forEach(function(index) {
      state[index] = player === 'comp' ? -1 : 1;
      scores.push(minmax(state, player === 'comp' ? 'player' : 'comp'));
      moves.push(index);
      state[index] = 0;
    });

    // return the best move
    if(player === 'comp') {
      compMove = moves[scores.indexOf(Math.max.apply(Math, scores))];
      return Math.max.apply(Math, scores);
    } else {
      compMove = moves[scores.indexOf(Math.min.apply(Math, scores))];
      return Math.min.apply(Math, scores);
    }
  }
})();
              
            
!
999px

Console