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

              
                header.top
  #init-wrapper
    input#name-input(type="text", placeholder="Your name")
    button#set-name.blue.ghost(type="button")
      | Set Name
    button#show-host.hide.inline-block.blue.ghost
      | Host Game
    #host.hide.inline-block
      #your-id
        span
          | your id is:
    button#show-join.hide.inline-block.blue.ghost
      | Join Game
    #join.hide.inline-block
      form
        label(for="id")
          | Friends ID:
        input#friends-peer-id(name="id", type="text")
        button#join-host.blue.ghost(type="button")
          | join
  #connected.hide
section.inner
  h2#output.text-center
  button#restart.hide(type="button")
    | Restart
  #game.disabled
    #tile-1.tile
    #tile-2.tile
    #tile-3.tile
    #tile-4.tile
    #tile-5.tile
    #tile-6.tile
    #tile-7.tile
    #tile-8.tile
    #tile-9.tile
              
            
!

CSS

              
                $blue: #2196F3;
$dingy: #495B6A;
$white: #ffffff;
$purple: #673AB7;
$yellow: #FFEB3B;
$red: #F44336;
$orange: #FF5722;
$black: #333333;

$nav: 100;
$navHeight: 64px;
$trans: 0.3s ease;

$z10:  0px 3px 3px 0px rgba(50, 50, 50, 0.1);
$z50:  0px 4px 3px 0px rgba(50, 50, 50, 0.2);
$z100: 0px 5px 3px 0px rgba(50, 50, 50, 0.3);
$inktrans: 0.2;
$darkred: #d32f2f;
$inkred: transparentize(#b71c1c, $inktrans);

$ghost:    transparent;

html {
  box-sizing: border-box;

  * {
    box-sizing: inherit;
  }
}

body {
  min-height: 100vh;
  width: 100%;
}

.inner {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.name {
  text-align: center;
  padding-top: 11px;
}

.top {
  background: $blue;
  padding: 20px;
  color: $white;
  box-shadow: 
    0 1.5px 4px rgba(0,0,0,0.24),
    0 1.5px 6px rgba(0,0,0,0.12);
}

#game {
  display: flex;
  flex-flow: row wrap;
  height: 200px;
  width: 200px;
  margin: 50px auto;

  .tile {
    border: 1px solid black;
    cursor: pointer;
    display: flex;
    flex: 1 30%;
    transition: background-color 300ms ease;

    &:hover {
      background-color: rgba(0,0,0,.1);
    }

    &.x,
    &.o {
      cursor: default;

      &:after {
        font-size: 50px;
        line-height: 0;
        margin: auto;
      }

      &:hover {
        background-color: #FFF;
      }
    }

    &.x:after {
      content: 'x';
    }
    &.o:after {
      content: 'o';
    }
  }
}

input {
  color: #999999;
}

button {
  text-decoration: none;
  color: $white;
  border-radius: 1px;
  background-color: $blue;
  border: none;
  -webkit-box-shadow: $z10;
  -moz-box-shadow:    $z10;
  box-shadow:         $z10;
  display: inline-block;
  text-align: center;
  overflow: hidden;
  position: relative;
  box-sizing: border-box;

  &:hover {
    background-color: darken($blue, 5%);
    -webkit-box-shadow: $z100;
    -moz-box-shadow:    $z100;
    box-shadow:         $z100;
    cursor: pointer;

    -webkit-transition: $trans;
    transition: $trans;
  }

  &.blue {
    background-color: $white;
    color: $blue;

    &:active, &:focus {
      background-color: darken($white, 5%);
    }

    &:hover {
      background-color: darken($white, 5%);
    }
  }
}

#restart {
  width: 200px;
  margin: 0 auto;
}

.hide {
  display: none !important;
}

.inline-block {
  display: inline-block;
}

.text-center {
  text-align: center;
}

              
            
!

JS

              
                let peer = new Peer({key: 'n0ei2j1souk57b9'}),
    nameEl = document.getElementById('name-input'),
    setNameButton = document.getElementById('set-name'),
    showHostButton = document.getElementById('show-host'),
    showJoinButton = document.getElementById('show-join'),
    joinHostButton = document.getElementById('join-host'),
    peerId = document.getElementById('friends-peer-id'),
    connectedEl = document.getElementById('connected'),
    gameTile = makeArray(document.getElementsByClassName('tile')),
    gameBoard = document.getElementById('game'),
    outputEl = document.getElementById('output'),
    restartButton = document.getElementById('restart'),
    yourMove = true,
    isHost = true,
    playerconnection,
    name;


/**
  * Peerjs setup stuff
  */
peer.on('open', function(id) {
  document.getElementById('your-id').innerHTML = `<span>Your id is: <kbd>${id}</kbd></span>`;
});

peer.on('connection', function(playerconnection, name){
  gameBoard.classList.remove('disabled');
  connectBack(playerconnection.peer);
  playerconnection.on('open', function(){
    renderConnectedTo(playerconnection.peer);

    playerconnection.on('data', function(data){
      if (data.move === 'restart' && data.name !== name) {
        restart();
      } else {
        renderMove(data, 'them');
      }
    });
  });
  myMove();
});


function renderConnectedTo(peer) {
  connectedEl.innerHTML = `You're connected to <span id="friendID">${peer}</span>`;
  connectedEl.classList.remove('hide');
  document.getElementById('init-wrapper').classList.add('hide');
}

function connectBack(id) {
  // recieve a connection and connect back
  if (typeof playerconnection === 'undefined') {
    // we need to connect back;
    playerconnection = peer.connect(id);
  }
  myMove();
}


/**
  * Setup things (name, join host whatevs)
  */
setNameButton.addEventListener('click', function () {
  setName();
});

nameEl.addEventListener('keydown', function(e) {
  let key = e.which || e.keyCode;
  if (key === 13) { // enter key
    e.preventDefault();
    setName();
  }
});

showHostButton.addEventListener('click', function(){
  document.getElementById('host').classList.toggle('hide');
});

showJoinButton.addEventListener('click', function(){
  document.getElementById('join').classList.toggle('hide');
});

joinHostButton.addEventListener('click', function(){
  yourMove = false;
  isHost = false;
  playerconnection = peer.connect(peerId.value);
  document.getElementById('join').classList.toggle('hide');
});

peerId.addEventListener('keydown', function(e) {
  let key = e.which || e.keyCode;
  if (key === 13) { // enter key
    e.preventDefault();
    yourMove = false;
    isHost = false;
    landline = peer.connect(peerId.value);
    document.getElementById('join').classList.toggle('hide');
  }
});

function setName() {
  let nameInput = document.getElementById('name-input');
  name = nameInput.value;
  nameInput.classList.add('hide');
  setNameButton.classList.add('hide');
  showHostButton.classList.remove('hide');
  showJoinButton.classList.remove('hide');
}


/**
  * Gameplay section
  */
for (let i=0, ii=gameTile.length; i<ii; i++) {
  console.log('in loop!');
  gameTile[i].addEventListener('click', function(e) {
    console.log(e.target.id);
    if (!e.target.classList.contains('disabled') && !game.classList.contains('disabled')) {
      sendMove(e.target.id);
    }
  });
}

restartButton.addEventListener('click', function(e) {
  e.preventDefault();
  let data = {
    "move": "restart",
    "name": name
  };
  playerconnection.send(data);
  restart();
})

function sendMove(tile) {
  yourMove = false;
  let tileEl = document.getElementById(tile);
  tileEl.classList.add(markerMe());
  tileEl.classList.add('disabled');
  game.classList.add('disabled');
  let data = {
    "move": tile,
    "name": name
  };
  playerconnection.send(data);
  if (gameWin(makeArray(document.getElementsByClassName(markerMe())))){
    outputEl.innerHTML = 'You Won!';
    restartButton.classList.remove('hide');
  } else {
    myMove();
  }
}

function renderMove(data) {
  yourMove = true;
  if (document.getElementById('friendID').innerHTML !== data.name) {
    renderConnectedTo(data.name);
  }
  let tileEl = document.getElementById(data.move);
  tileEl.classList.add(markerThem());
  tileEl.classList.add('disabled');
  game.classList.remove('disabled');
  if (gameWin(makeArray(document.getElementsByClassName(markerThem())))) {
    outputEl.innerHTML = 'You Lost...';
    restartButton.classList.remove('hide');
  } else {
    myMove();
  }
}

function gameWin(moves) {

  if (moves.length >= 3) {
    // let tilesWon = [];
    let tilesWon = [];
    for (let i=0, ii=moves.length; i<ii; i++) {
      tilesWon.push(moves[i].id);
    }
    console.log(tilesWon);
    if (
      (tilesWon.indexOf('tile-1') > -1 && tilesWon.indexOf('tile-2') > -1 && tilesWon.indexOf('tile-3') > -1) ||
      (tilesWon.indexOf('tile-4') > -1 && tilesWon.indexOf('tile-5') > -1 && tilesWon.indexOf('tile-6') > -1) ||
      (tilesWon.indexOf('tile-7') > -1 && tilesWon.indexOf('tile-8') > -1 && tilesWon.indexOf('tile-9') > -1) ||
      (tilesWon.indexOf('tile-1') > -1 && tilesWon.indexOf('tile-5') > -1 && tilesWon.indexOf('tile-9') > -1) ||
      (tilesWon.indexOf('tile-3') > -1 && tilesWon.indexOf('tile-5') > -1 && tilesWon.indexOf('tile-7') > -1) ||
      (tilesWon.indexOf('tile-1') > -1 && tilesWon.indexOf('tile-4') > -1 && tilesWon.indexOf('tile-7') > -1) ||
      (tilesWon.indexOf('tile-2') > -1 && tilesWon.indexOf('tile-5') > -1 && tilesWon.indexOf('tile-8') > -1) ||
      (tilesWon.indexOf('tile-3') > -1 && tilesWon.indexOf('tile-6') > -1 && tilesWon.indexOf('tile-9') > -1)
    ) {
      return true;
    } else {
      if (document.getElementsByClassName('tile disabled').length == 9) {
        outputEl.innerHTML = 'Tie Game';
        game.classList.add('disabled');
        restartButton.classList.remove('hide');
      } else {
        return false;
      }
    }
  } else {
    return false;
  }
}

function myMove() {
  if (yourMove) {
    outputEl.innerHTML = 'Your Move';
    game.classList.remove('disabled');
  } else {
    outputEl.innerHTML = 'Opponent Move';
    game.classList.add('disabled');
  }
}

function markerMe() {
  if (isHost) {
    return 'x';
  } else {
    return 'o';
  }
}

function markerThem() {
  if (isHost) {
    return 'o';
  } else {
    return 'x';
  }
}

function restart() {
  console.log('in restart');
  // for each tile remove letters
  for (let i=0, ii=gameTile.length; i<ii; i++) {
    console.log(i, ii);
    gameTile[i].classList.remove('disabled');
    gameTile[i].classList.remove('x');
    gameTile[i].classList.remove('o');
    console.log('removing tiles');
  }

  // disable game for host now
  if (isHost) {
    yourMove = false;
  } else {
    yourMove = true;
  }
  myMove();

  restartButton.classList.add('hide');
}

/**
  * My fancy makeArray helper function
  */
function makeArray(r){return[].slice.call(r,0)}

              
            
!
999px

Console