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

              
                <!--Container-->
<div id="container">
  <!--Row-->
  <div class='row'>
    <span id="music-control" class="btn btn-danger">
      <i id='music-icon' class="fa fa-volume-up" aria-hidden="true"></i>
      <span id="sound-tooltip"class='tooltip'>Toggle Sound</span>
     </span>
    <h1>Star Wars</h1>
    <h3>Tic-Tac-Toe</h3>
    <span id="reset" class="btn btn-primary">
      Reset
      <span id="sound-tooltip"class='tooltip'>Reset Game</span>
    </span>
  </div>
  <!--End Row-->

  <!--Row-->
  <div class='row'>

    <!--Wrapper-->
    <div id='wrapper'>
      <!--Game Board-->
      <div id='board'>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
        <div class='box'></div>
      </div>
      <!--End Game Board-->
    </div>
    <!--End Wrapper-->
  </div>
  <!--End Row-->

  <!-- Music theme song -->
  <audio id="theme-song">
    <source src='http://www.digitpress.com/dpsoundz/mp3/dp_starwars.mp3'>
  </audio>
  <audio id="darkside-audio">
    <source src="http://www.digitpress.com/dpsoundz/mp3/dp_starwars_darkside.mp3">
  </audio>
  <audio id="lightside-audio">
    <source src="http://www.digitpress.com/dpsoundz/mp3/dp_starwars_theme.mp3">
  </audio>
  <!-- End Music theme song -->
  
</div>
<!--End Container-->


<!-- Modals -->
<div id="overlay"></div>
<div id="modal">
  <span id="close-modal"><i class="fa fa-times" aria-hidden="true"></i>
  </span>
</div>
              
            
!

CSS

              
                // VARIABLES
@bg-image : 'https://mnmlwalls.files.wordpress.com/2015/01/star-wars.png';
@primary-color : #607d8b;
@danger-color: #f44336;
@light-gray: #d3d3d3;
@yellow: #ffc107;
@orange: #FF9800;
@black: #111;
@no-color: rgba(0, 0, 0, 0);
@box-bg-color: rgba(95, 108, 116, 0.44);
@box-hover-color: rgba(227, 227, 227, 0.5);
@overlay-color: rgba(0, 0, 0, 0.35);

// GENERAL
body {
  font-family: 'Roboto', sans-serif;
  background: #302f2f;
  color: white;
  .row {
    width: 100%;
    &:after {
      content: "";
      display: table;
      clear: both;
    }
  }
  .btn {
    width: 75px;
    padding: 5px;
    transition: all .25s ease-in-out;
    border: 2px solid @no-color;
    &:hover {
      background: none;
      border: 2px solid @light-gray;
      color: @light-gray;
    }
  }
  .btn-primary {
    background: @primary-color;
  }
  .btn-danger {
    background: @danger-color ;
  }
}

// MAIN CONTENT
#container {
  width: 960px; 
  margin: 0 auto;
  text-align: center;
  position: relative;
  h1, h3 {
    text-transform: uppercase;
    color: @yellow;
    margin: 10px 0 0 0;
  }
  h3 {
    margin-bottom: 10px
  }
  #reset, #music-control {
    position: absolute;
    top: 0;
    .tooltip{
      position: absolute;
      top: 35px;
      left: 5px;
      border-radius: 5px;
      background: @light-gray;
      color: @black;
      font-size: .75em;
      font-weight: bold;
      padding: 5px;
      opacity: 0;
      transition: all .25s ease-in-out;
      &:after {
        content: " ";
        position: absolute;
        left: 50%;
        bottom: 100%;
        border-style: solid;
        border-width: 5px;
        margin-left: -5px;
        border-color: transparent transparent @light-gray transparent;
      }
    }
    &:hover .tooltip{
      opacity: 1;
    }
  }
  #reset {
    right: 25%;
  }
  #music-control {
    left: 25%;
  }
}


// GAME BOARD
#board {
  background: url(@bg-image) no-repeat center center;
  background-size: cover;
  width: 500px;
  height: 500px;
  margin: 0 auto;
  .box {
    background-color: @box-bg-color;
    margin: 5px;
    height: 155px;
    width: 155px;
    float: left;
    vertical-align: top;
    text-align: center;
    transition: all .25s ease-in-out;
    &:hover {
      background: @box-hover-color;
      transform: scale(1.1);
    }
    img {
      width: 100%;
    }
  }
  #luke-img img,
  #vader-img img {
    width: 25%;
  }
}


// MODAL MESSAGE
#overlay {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  background: @overlay-color;
  display: none;
}

#modal {
  width: 25%;
  position: absolute;
  top: 15%;
  left: 0;
  right: 0;
  margin: 0 auto;
  background: @orange;
  box-shadow: 5px 5px 5px @black;
  text-align: center;
  display: none;
  padding: 50px;
  #close-modal {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 1.25em;
    border: 2px solid white;
    border-radius: 100%;
    height: 25px;
    width: 25px;
    transition: all .25s ease-in-out;
    &:hover{
      transform: scale(1.2);
    }
  }
}
              
            
!

JS

              
                // VARIABLES
// =========================
var playerTurn = 0;
var luke = '<img src="http://icons.iconarchive.com/icons/creativeflip/starwars-longshadow-flat/128/Luke-Skywalker-icon.png">';
var vader = '<img src="http://icons.iconarchive.com/icons/creativeflip/starwars-longshadow-flat/128/Darth-Vader-icon.png">';
var boxes = document.getElementsByClassName('box');
var resetBtn = document.getElementById('reset');
var musicBtn = document.getElementById('music-control');
var musicIcon = document.getElementById('music-icon');
var closeModalBtn = document.getElementById('close-modal');
var overlay = document.getElementById("overlay");
var modal = document.getElementById("modal");
var themeSong = document.getElementById('theme-song');
var darkSideClip = document.getElementById('darkside-audio');
var lightSideClip = document.getElementById('lightside-audio');
var soundOn = true;
// modal messages
var modalMessage = {
  box_occupied: "Sorry, Pick another Square",
  vader_wins: "Now you know the power of the dark side<br> - Vader wins",
  luke_wins: "The force is strong with this one<br> - Luke wins"
}

// EVENT LISTENERS
// =========================
//Attach an Event Listener to each box
for(var i = 0; i < boxes.length; i++ ){
  boxes[i].addEventListener('click', setPlayerTurn);
}
// Event Listener to reset the board
resetBtn.addEventListener('click', resetBoard);
//Event Listener to close the modal 
closeModalBtn.addEventListener('click', closeModal);

// Event Listener to play music
musicBtn.addEventListener('click', function (){  
  
  if(soundOn === true) {
    soundOn = false;
    musicIcon.className = "fa fa-volume-off"
  }else if (soundOn === false) {
    soundOn = true;
    musicIcon.className = "fa fa-volume-up"
  }
  
})


// GAME FUNCTIONS
// =========================
// Setting player turn on each click
function setPlayerTurn(){
  // first check if box is unoccupied
  if(this.innerHTML !== ''){
    openModal(modalMessage.box_occupied);
  // if turn is even number set the box as luke 
  }else if(playerTurn % 2 == 0){
    playerTurn += 1;
    this.innerHTML = luke;
    getWinner();
  // if it's odd, then set the box as vader
  }else {
    playerTurn += 1;
    this.innerHTML = vader;
    getWinner();
  }
}

function getWinner(){
  //Luke Wins Across
  if(boxes[0].innerHTML === luke && boxes[1].innerHTML === luke && boxes[2].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }else if(boxes[3].innerHTML === luke && boxes[4].innerHTML  === luke && boxes[5].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }else if(boxes[6].innerHTML  === luke && boxes[7].innerHTML  === luke && boxes[8].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }
   //Luke Wins Down
   else if(boxes[0].innerHTML  === luke && boxes[3].innerHTML  === luke && boxes[6].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }else if(boxes[1].innerHTML === luke && boxes[4].innerHTML === luke && boxes[7].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }else if(boxes[2].innerHTML === luke && boxes[5].innerHTML === luke && boxes[8].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }
  //Luke Wins Diagonal
   else if(boxes[0].innerHTML === luke && boxes[4].innerHTML === luke && boxes[8].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }else if(boxes[6].innerHTML === luke && boxes[4].innerHTML === luke && boxes[2].innerHTML === luke){
    openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
  }
  
  //Vader Wins Across
   else if(boxes[0].innerHTML === vader && boxes[1].innerHTML === vader && boxes[2].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }else if(boxes[3].innerHTML === vader && boxes[4].innerHTML  === vader && boxes[5].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }else if(boxes[6].innerHTML  === vader && boxes[7].innerHTML  === vader && boxes[8].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }
  
   //Vader Wins Down
   else if(boxes[0].innerHTML  === vader && boxes[3].innerHTML  === vader && boxes[6].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }else if(boxes[1].innerHTML === vader && boxes[4].innerHTML === vader && boxes[7].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }else if(boxes[2].innerHTML === vader && boxes[5].innerHTML === vader && boxes[8].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }
  
   //Vader Wins Diagonal
   else if(boxes[0].innerHTML === vader && boxes[4].innerHTML === vader && boxes[8].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }else if(boxes[6].innerHTML === vader && boxes[4].innerHTML === vader && boxes[2].innerHTML === vader){
    openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
  }
  
}


// function to reset board
function resetBoard(){
  for(var i = 0; i < boxes.length; i++){
    boxes[i].innerHTML = '';
  }
  playerTurn = 0;
}


// function when either luke or vader wins
function openMessageAndPlayMusic(player,song){
  // open messsage 
  openModal(player);
  
  // play song if sound is on
  function playSong(){
    if(soundOn === true){
      song.play();
    }
  }
  
  // invoke song function
  return playSong(song);
}


// Open modal and set message
function openModal(message){
  var h2 = document.createElement("h2");
  overlay.style.display = 'block';
  modal.style.display = 'block';
  
  h2.innerHTML = message;
  h2.id = "message";
  modal.appendChild(h2)
  
}

// Close the modal
function closeModal(){
  overlay.style.display = "none";
  modal.style.display = "none";
  // remove the previous message
  if(modal.children[1]){
    modal.removeChild(modal.children[1]);
  }
}





              
            
!
999px

Console