cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div id="main">
    <div class="buttons">
      <h1>Choose Your Symbol</h1>
      <span id="symbol-X" class="symbol"> X </span>
      <span id="symbol-O" class="symbol"> O </span>
    </div>
  	<div id="gameboard" class="container">
    <div id="row1" class="row">
  		    <div class="left-square square col-xs-4" id="sq0"></div>
      <div class="middle-square square col-xs-4" id="sq1"></div>
      <div class="right-square square col-xs-4" id="sq2"></div>
    </div>
    <div id="row2" class="row">
      <div class="left-square square col-xs-4" id="sq3"></div>
      <div class="middle-square square col-xs-4" id="sq4"></div>
      <div class="right-square square col-xs-4" id="sq5"></div>
    </div>
    <div id = "row3" class="row">
      <div class="left-square square col-xs-4" id="sq6"></div>
      <div class="middle-square square col-xs-4" id="sq7"></div>
      <div class="right-square square col-xs-4" id="sq8"></div>
      </div>
  	</div> 
 </div>
            
          
!
            
              $background-color: #265F72; 
$board-color: #D9C661;
$glow-color: transparentize(darken($board-color, 25%), .5);
$boardsize: 500px;

@mixin centerer {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

body {
  background-color: $background-color;
}

#gameboard{
  @include centerer;
  height: $boardsize;
  width: $boardsize;
  color: $board-color;
  margin-left: 40px;
  }

.square{
  height: $boardsize/3;
  box-sizing: border-box;
  text-align: center;
  padding:50px;
  font-size:40px;
  font-weight: bold;
  }

.symbol {
  font-size:40px;
  font-weight: bold;
  padding: 8px;

}

.left-square {
  border-right: 2px solid black;
  border-color: $board-color;
}

.right-square{
  border-left: 2px solid black;
  border-color: $board-color;
}

#row1, #row2 {
  border-bottom: 2px solid black;
  border-color: $board-color;
}

.glow {
  text-shadow: 4px 2px 4px $glow-color,
               4px -2px 4px $glow-color,
               -4px 2px 4px $glow-color,
               -4px -2px 4px $glow-color,
               2px 4px 4px $glow-color,
               2px -4px 4px $glow-color,
               -2px 4px 4px $glow-color,
               -2px -4px 4px $glow-color,
}

.buttons {
  width: 170px;
  margin: 100px 0 0 40px;
  text-align: center;
  h1 {
    color: $board-color;
  }
}
.buttons span{
  cursor: pointer;
  display: inline-block;
  color: $board-color;
  border: 2px solid $board-color;
  width: 150px;
  height: 75px;
  margin: 8px;
  border-radius: 10%;
  text-align: center;
}



//add media queries 

//at 910
            
          
!
            
              var player = "X"; 
var ai = "O";
var winner, gameboard, playing;

//toggle player symbol based on button clicked
$("#symbol-X").click(function() {
  player = "X";
  ai = "O";
  $("#symbol-O").removeClass("glow");
  console.log("player: " + player);
  reset();
});

$("#symbol-O").click(function() {
  player = "O";
  ai = "X";
  $("#symbol-X").removeClass("glow");
  console.log("player: " + player);
  reset();
});

function reset() {
  setTimeout(function() {
    start();
    $('.square').removeClass("glow");
    console.log("game reset");
    $('.square').html("");
  }, 1500);
};

function start() {
  $('#symbol-' + player).addClass('glow');
  gameboard = ["", "", "", "", "", "", "", "", ""];
  playing = true;
  winner = "none";

}

function getEmptySpaces(board) {
  return board.reduce(function(results, square,index) {
    if(square !== "") {
      return results;
    } else {
      return results.concat(index);
    }
  }, []);
}

function aiTurn() {
  console.log("computer's turn");
  var available = getEmptySpaces(gameboard);
  console.log(available);
  //temporary stop if tie game
  if(available.length === 0) {
    return;
  }
  var aiIndex = available[Math.floor(Math.random() * available.length)];
  console.log(aiIndex);
  gameboard[aiIndex] = ai;
  $("#sq" + aiIndex).html(ai);
};


function checkForWinner(board) {
  // Check for win conditions 
  var winCon = [
    // Horizontal
    [0, 1, 2], 
    [3, 4, 5],
    [6, 7, 8],
    // Vertical
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    // Diagonal
    [0, 4, 8],
    [2, 4, 6],
  ];
  for(var i=0; i<winCon.length; i++) {
    var cond = winCon[i];
    // If all three are equal and not blank
    if(board[cond[0]] !== ""  && board[cond[0]] === board[cond[1]] && board[cond[1]] === board[cond[2]]) {
      winner = board[cond[0]];
      playing = false;
      $('#sq'+ cond[0]).addClass('glow');
      $('#sq'+ cond[1]).addClass('glow');
      $('#sq'+ cond[2]).addClass('glow');
      console.log(winner + " Wins!");
      reset();
      break;
    }
  }
  if(getEmptySpaces(gameboard).length === 0 && winner === "none") {
    console.log("Game tie!");
    reset();
  }
};


$("#gameboard").click(function(e) {
  //end game when winner delcared 
  if(!playing) return;
  console.log("players's turn");
  var playerPick = (e.target.id).slice(2);
  console.log(playerPick);
  var playerSelector = "#sq" + playerPick;
  if(gameboard[playerPick] != "") {
     //player doesn't lose turn if invalid entry 
    return;
  };
  if(gameboard[playerPick] == "") {
    gameboard[playerPick] = player;
    console.log(gameboard);
    $(playerSelector).html(player);
  }; 

  checkForWinner(gameboard); 
  if(playing) {
    aiTurn(); 
    checkForWinner(gameboard);
  }
});



start();
      

            
          
!
999px
Loading ..................

Console