cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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 class="ttt-notification">
  
  <div class="ttt-text">
    <h1 class="ttt-h1">Tic-Tac-Toe</h1>
    <p class="ttt-question">Choose Wisely</p>
    <button class="btn btn-primary x ttt-btn" data-img="tick"><img src="https://dl.dropboxusercontent.com/s/kug5z67ars067gh/tick.jpg?dl=0" class="ttt-img"></button>
    <button class="btn btn-primary o ttt-btn" data-img="toe"><img src="https://dl.dropboxusercontent.com/s/y5b5yf9poupxcjo/toes%20-%20Edited.png?dl=0" class="ttt-img"></button>
    <p class="ttt-attribute">by <a href="http://brandonlehr.com">Brandon Lehr</a></p>
  </div> 
</div>

<div class="ttt-notify-winner">
  <div class="ttt-text">
    <p class="ttt-question"></p>
    <button class="btn btn-primary ttt-reset ttt-btn">Play Again</button>
  </div>
</div>

<div class="ttt-board container">
  <div class="row">

    <div id="sq1" class="square col-xs-4"></div>
    <div id="sq2" class="square col-xs-4"></div>
    <div id="sq3" class="square col-xs-4"></div>

    <div id="sq4" class="square col-xs-4"></div>
    <div id="sq5" class="square col-xs-4"></div>
    <div id="sq6" class="square col-xs-4"></div>

    <div id="sq7" class="square col-xs-4"></div>
    <div id="sq8" class="square col-xs-4"></div>
    <div id="sq9" class="square col-xs-4"></div>

  </div>
</div>
            
          
!
            
              body {
  padding-top: 50px;
}

.ttt-img {
  height: 75px;
  max-width: 90px;
  display: block;
  margin: auto;
  border-radius: 4px;
}
.ttt-h1 {
  font-weight: 700;
  letter-spacing: 1px;
  margin-bottom: 25px;
  font-size: 5em;
  color: #1D1F20;
}
.ttt-attribute {
  position: relative;
  bottom: -10px;
}

.ttt-notification,
.ttt-notify-winner {
  width: 100%;
  height: 100%;
  position: absolute;
  top: -100%;
  left: 0;
  z-index: 5;
  text-align: center;
  background: repeat url(https://dl.dropboxusercontent.com/s/35hy314d64teou6/seamless_paper_texture.png?dl=0);

}

.ttt-notify-winner .text .question {
  font-size: 3em;
}

.ttt-btn {
  font-size: 1.5em;
}

.ttt-text {
  position: relative;
  top: 10%;
  margin: auto;
  text-align: center;
  padding: 20px;
  width: 100%;
  color: #1D1F20;
  border-radius: 0 0 5px 5px;
  font-family: 'Roboto Slab', cursive;
}

.ttt-text p {
  font-size: 1.7em;
  font-weight: 700;
}

.ttt-board {
  width: 300px;
  height: 300px;
  margin: auto;
  text-align: center;
}

.square {
  width: 95px;
  height: 95px;
  padding-top: 15px;
  padding-left: 8px;
  font-size: 3em;
}

#sq4,
#sq5,
#sq6 {
  border-top: 2px solid #878383;
  border-bottom: 2px solid #878383;
}

#sq2,
#sq5,
#sq8 {
  border-left: 2px solid #878383;
  border-right: 2px solid #878383;
}

@media (max-width: 400px) {
  .ttt-h1 {
    font-size: 3em;
  }
}

            
          
!
            
               var ttt = (function() {

    var player = "",
      computer = "",
      winArr = [
        ['sq1', 'sq2', 'sq3'],
        ['sq4', 'sq5', 'sq6'],
        ['sq7', 'sq8', 'sq9'],
        ['sq1', 'sq4', 'sq7'],
        ['sq2', 'sq5', 'sq8'],
        ['sq3', 'sq6', 'sq9'],
        ['sq1', 'sq5', 'sq9'],
        ['sq3', 'sq5', 'sq7']
      ],
      spacesArr = ['sq1', 'sq2', 'sq3', 'sq4', 'sq5', 'sq6', 'sq7', 'sq8', 'sq9'],
      cornerSpacesArr = ['sq1', 'sq3', 'sq7', 'sq9'],
      computerMarkArr = [],
      playerMarkArr = [],
      tempArr = [],
      playerTurn,
      mark,
      blockPos = "",
      beatPos = "",
      toe = '<img src="https://dl.dropboxusercontent.com/s/y5b5yf9poupxcjo/toes%20-%20Edited.png?dl=0" class="ttt-img">',
      tick = '<img src="https://dl.dropboxusercontent.com/s/kug5z67ars067gh/tick.jpg?dl=0" class="ttt-img">';

    var showNotification = function showNotification() {
      var showDelay = setTimeout(function() {
        $('.ttt-notification').animate({
          top: "0"
        }, 500);
      }, 1000);
    };

    var hideNotification = function hideNotification() {
      $('.ttt-notification').animate({
        top: "-100%"
      }, 500);
    };

    var showVictory = function showVictory(winner) {
      playerTurn = 5;
      $('.ttt-notify-winner .ttt-text p').html(winner);
      $('.ttt-notify-winner').animate({
        top: "0"
      }, 500);
    };

    var hideResult = function hideResult() {
      $('.ttt-notify-winner .ttt-text p').html("");
      $('.ttt-notify-winner').animate({
        top: "-100%"
      }, 500);
    };

    var showTie = function showTie() {
      playerTurn = 5;
      $('.ttt-notify-winner .ttt-text p').html("Its a Tie!");
      $('.ttt-notify-winner').animate({
        top: "0"
      }, 500);
    };

    var usedSpaces = function usedSpaces(space) {
      spacesArr.splice(spacesArr.indexOf(space), 1);
      if (cornerSpacesArr.indexOf(space) !== -1) {
        cornerSpacesArr.splice(cornerSpacesArr.indexOf(space), 1);
      }
    };

    var blockPlayer = function blockPlayer() {
      winArr.forEach(function(win) {
        tempArr.length = 0;
        playerMarkArr.forEach(function(pos) {
          if (win.indexOf(pos) !== -1) {
            tempArr.push(pos);
            if (tempArr.length === 2) {
              win.forEach(function(winPos) {
                if (tempArr.indexOf(winPos) === -1) {
                  if (spacesArr.indexOf(winPos) !== -1) {
                    blockPos = winPos;
                    return true;
                  }
                }
              });
            }
          }
        });
      });
    };

    var beatPlayer = function beatPlayer() {
      winArr.forEach(function(win) {
        tempArr.length = 0;
        computerMarkArr.forEach(function(pos) {
          if (win.indexOf(pos) !== -1) {
            tempArr.push(pos);
            if (tempArr.length === 2) {
              win.forEach(function(winPos) {
                if (tempArr.indexOf(winPos) === -1) {
                  if (spacesArr.indexOf(winPos) !== -1) {
                    beatPos = winPos;
                    return true;
                  }
                }
              });
            }
          }
        });
      });
    };

    var randomChoice = function randomChoice(arr) {
      var markDelay = setTimeout(function() {
        beatPlayer();
        if (beatPos !== "") {
          mark = beatPos;
        } else {
          blockPlayer();
          if (blockPos !== "") {
            mark = blockPos;
          } else {
            var arrPos = Math.floor(Math.random() * (arr.length - 1 - 0 + 1)) + 0;
            mark = arr[arrPos];
          }
        }

        computerMarkArr.push(mark);
        usedSpaces(mark);
        checkForWin(computerMarkArr);
        playerTurn = 1;
        $("#" + mark).html(computer);

      }, 1200);
    };

    var playGame = function playGame() {
      if (playerTurn === 0) {
        if (spacesArr.length === 0) {
          showTie();
        } else {
          if (computerMarkArr.length < 3) {
            randomChoice(cornerSpacesArr);
          } else {
            randomChoice(spacesArr);
          }
        }
      }
    };

    var victory = function victory() {
      if (playerTurn === 1) {
        showVictory("You Win!");
      } else {
        showVictory("You Lose!");
      }
    };

    var checkForWin = function checkForWin(arr) {
      winArr.forEach(function(win) {
        if (arr.indexOf(win[0]) !== -1 && arr.indexOf(win[1]) !== -1 && arr.indexOf(win[2]) !== -1) {
          victory();
          return;
        }
      });
      if (playerTurn === 0 && spacesArr.length === 0) {
        showTie();
      } else {
        if (playerTurn === 1) {
          playerTurn = 0;
          playGame();
        }
      }
    };

    var playerTakesTurn = function() {
      if (playerTurn === 1) {
        var id = $(this).attr('id');
        if (spacesArr.indexOf(id) !== -1) {
          $(this).html(player);
          playerMarkArr.push(id);
          usedSpaces(id);
          checkForWin(playerMarkArr);
          blockPos = "";
          beatPos = "";
        } else {
          playerTurn = 1;
        }
      }
    };

    var playerChooseMark = function() {
      player = $(this).data('img');
      if (player === 'tick') {
        player = tick;
        computer = toe;
      } else {
        player = toe;
        computer = tick;
      }
      hideNotification();
      playerTurn = 0;
      computerMarkArr.length = 0;
      playerMarkArr.length = 0;
      playGame();
    };

    var reset = function() {
      $('.square').html("");
      spacesArr.length = 0;
      cornerSpacesArr.length = 0;
      spacesArr = ['sq1', 'sq2', 'sq3', 'sq4', 'sq5', 'sq6', 'sq7', 'sq8', 'sq9'];
      cornerSpacesArr = ['sq1', 'sq3', 'sq7', 'sq9'];
      blockPos = "";
      beatPos = "";
      computerMarkArr.length = 0;
      playerMarkArr.length = 0;
      playerTurn = 0;
      hideResult();
      playGame();
    };

    return {
      showNotification: showNotification,
      playerTakesTurn: playerTakesTurn,
      playerChooseMark: playerChooseMark,
      reset: reset
    };

  })();

  $(function() {

    ttt.showNotification();

    $('.square').on('click', ttt.playerTakesTurn);

    $('.x, .o').on('click', ttt.playerChooseMark);

    $('.ttt-reset').on('click', ttt.reset);

  });
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console