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

              
                <div class='fluid-container'>
  <div class='border'>
    
    <div class='row'>
      <div class='col-md-4' style='text-align: right'>
        <p class='p1-score'><span class='name'>Player 1</span>: <span class='score'>0</span> </p>        
      </div>
            
      <div class='col-md-4' style='text-align: left'>
        <p class='p2-score'><span class='name'>Computer</span>: <span class='score'>0</span></p>
      </div>
      
      <div class='col-md-4'>
        <button class='full-reset'>Reset-All</button>
      </div>
     </div>
     <div class='p1-turn'>
       <p></p>
     </div>
     <div class='p2-turn'>
       <p></p>
      
    </div>
    <div class='main'>
      <div class='text-box'>
        <h2>Would You Like To Be X or O?</h2>
        <button class='chose-x'>X</button>
        <button class='chose-o'>O</button>
      </div>
      <div class='game-field'>
        <canvas id='myCanvas'></canvas>
        <ul class='boxes'></ul>
      </div>
      <div class='win'>
        <h1>You Win!</h1>
      </div>
      <div class='lose'>
        <h1>You Lose!</h1>
      </div>
      <div class='draw'>
        <h1>It's a Draw!</h1>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                html {
  width: 100%;
  height: 100%;
}

.fluid-container {
  font-family: 'Architects Daughter', 'Helvetica', sans-serif;
  text-align: center;
}

.border {
  background-color: #E1BB8C;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  position: relative;
  border-radius: 10px;
  width: 400px;
  
  margin: 10% auto;
  padding: 20px 0 20px;
}

.main {
  color: white;
  height: 340px;
  width: 350px;
  background: #006624;
  /* For browsers that do not support gradients */
  background: -webkit-radial-gradient(#009933, #001a09);
  /* Safari 5.1 to 6.0 */
  background: -o-radial-gradient(#009933, #001a09);
  /* For Opera 11.6 to 12.0 */
  background: -moz-radial-gradient(#009933, #001a09);
  /* For Firefox 3.6 to 15 */
  background: radial-gradient(#009933, #001a09);
  /* Standard syntax */
  position: relative;
  margin: 0 auto;
  overflow: hidden;
}

.text-box {
  margin: auto;
  margin-top: 70px;
 
  overflow: hidden;
}
.game-field {
   display: none;
   width: 100%;
  height: 100%;
  margin: 0 auto;
  position: relative;
}
.win, .lose, .draw {
  position: absolute;
  width: 400px;
  height: 400px;
  background: rgba(0,0,0,.8);
  top: -15px;
  left: 0;
  box-sizing: border-box;
  display: none;
  z-index: 2000;
}
.win h1, .lose h1, .draw h1{
  color: white;
  position: absolute;
  margin: 0;
  top: 150px;
  left: 100px;
  text-align: center;
}
button {
  background-color: Transparent;
  font-size: 40px;
  margin: 30px;
  border: none;
  color: LightGrey;
  border-radius: 10px;
  border-style: dashed;
  border-color: Transparent;
}

button:hover {
  color: white;
  border-color: white;
}

.p1-turn {
  background: #6495ED;
  left: 15px;
}
.p2-turn {
  background: rgba(200,100,100,1);
  right: 15px;
}
.p1-turn, .p2-turn {
  position: absolute;
  top: 0;
  width: 170px;
  height: 50px;
  z-index: -10;
  color: white;
  text-align: center;
}
.full-reset {
  
  color: black;
  margin: auto;
  font-size: 16px;
}

.full-reset:hover {
  color: black;
  border-color: black;
}
.p1-score, .p2-score {
 font-size: 18px;
  
}
.p1-score, .p2-score, .full-reset {
  display: none;
}

.p1-turn p,
.p2-turn p {
  font-size: 1.3rem;
  
  
}


.boxes {
  padding: 0;
  width: 100%;
  height: 100%;
  position: relative;
  left: 12px;
  top: 0;
}

.boxes li {
  width: 30%;
  height: 30%;
  display: inline-block;
  position: relative;
  z-index: 1000;
  margin-left: 2px;
  margin-right: 2px;
  overflow: hidden;
 
}
.boxes li:hover {
  cursor: pointer;
}
li i {
  font-size: 7.5rem;
  text-align: center;
  display: block;
  width: 100%;
  height: 100%;
  font-style: normal;
  font-family: "Architects Daughter", "Helvetica", "sans-serif";
  color: rgba(220,220,220,.7);
  z-index: 500;
}

li span {
  position: relative;
  bottom: 15px;
}
#myCanvas {
  width: 330px;
  height: 330px;
  position: absolute;
  
  left: 10px;
  top: 0;
  opacity: 1;
}
              
            
!

JS

              
                var GAME = GAME || {
  gameInPlay: false,
  winningCombos: [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [1, 4, 7],
    [2, 5, 8],
    [3, 6, 9],
    [1, 5, 9],
    [3, 5, 7],
  ],

  p1Score: 0,
  p2Score: 0,

  initVars: function() {
    this.currentBoard = {
      1: '',
      2: '',
      3: '',
      4: '',
      5: '',
      6: '',
      7: '',
      8: '',
      9: '',
    };
    this.availSpace = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
    this.numFilled = 0;
    this.p1Arr = [];
    this.p2Arr = [];
  },

  beginGame: function() {
    GAME.initVars();
    GAME.display.drawBoard();
    GAME.resetSquares();
    $('.chose-x').click(function() {
      GAME.player1 = 'X';
      GAME.player2 = 'O';
      GAME.display.showBoard();
      GAME.gameplay.play();
    });
    $('.chose-o').click(function() {
      GAME.player1 = 'O';
      GAME.player2 = 'X';
      GAME.display.showBoard();
      GAME.gameplay.play();
    });
    $('.full-reset').click(function() {
      GAME.gameplay.fullReset();

    });

  },
  display: {
    showChoice: function() {
      $('.game-field,.win,.lose,.draw,.p1-score,.p2-score,.full-reset').fadeOut();
      GAME.display.hideP1Turn();
      GAME.display.hideP2Turn();
      $('.text-box').fadeIn();
    },
    showBoard: function() {
      $('.text-box').fadeOut();
      $('.game-field,.p1-score,.p2-score,.full-reset').fadeIn();
    },
    showP1Turn: function() {
      $('.p1-turn p').text('Your turn!');
      $('.p1-turn').animate({
        'top': '-45px'
      }, 500);
    },
    hideP1Turn: function() {
      $('.p1-turn').animate({
        'top': '0'
      }, 500);
    },
    showP2Turn: function() {
      $('.p2-turn p').text("Computer's turn!");
      $('.p2-turn').animate({
        'top': '-45px'
      }, 500);
    },
    hideP2Turn: function() {
      $('.p2-turn').animate({
        'top': '0'
      }, 500);
    },
    showDraw: function() {
      $('.draw').fadeIn(500);
    },
    hideDraw: function() {
      $('.draw').fadeOut(1000);
    },
    showWin: function() {
      $('.win').fadeIn(500);
    },
    hideWin: function() {
      $('.win').fadeOut(1000);
    },
    showLose: function() {
      $('.lose').fadeIn(500);
    },
    hideLose: function() {
      $('.lose').fadeOut(1000);
    },
    drawBoard: function() {

      var c = document.getElementById("myCanvas"),
        canvas = c.getContext("2d");
      canvas.strokeStyle = '#fff';
      canvas.beginPath();
      canvas.moveTo(100, 4);
      canvas.lineTo(100, 146.5);
      canvas.closePath();
      canvas.stroke();
      canvas.beginPath();
      canvas.moveTo(200, 4);
      canvas.lineTo(200, 146.5);
      canvas.closePath();
      canvas.stroke();

      canvas.lineWidth = 0.5;
      canvas.beginPath();
      canvas.moveTo(4, 48.5);
      canvas.lineTo(296, 48.5);
      canvas.closePath();
      canvas.stroke();
      canvas.beginPath();
      canvas.moveTo(4, 98.5);
      canvas.lineTo(296, 98.5);
      canvas.closePath();
      canvas.stroke();
    }
  },
  resetSquares: function() {
    $('.boxes').html('');
    for (var i = 1; i <= 9; i++) {
      var box = '<li class="' + i + '"><i class="letter"><span></span></i></li>';
      $(box).appendTo($('.boxes'));

    }

  },
  gameplay: {
    goesFirst: function() {
      var first = Math.floor(Math.random() * 2) + 1;

      return first;
    },
    play: function() {
      GAME.gameInPlay = true;
      GAME.turn = GAME.gameplay.goesFirst();
      GAME.turnStart = true;
      $('.boxes li').click(function() {
        GAME.gameplay.playerTurn(this);
      });
      if (GAME.turn === 1) {

        GAME.display.showP1Turn();

      } else {

        GAME.display.showP2Turn();
        GAME.gameplay.computerTurn();
      }

    },
    playerTurn: function(box) {

      var symb = GAME.player1;

      var txt = $(box).children('i').children('span');
      var num = $(box).attr('class');
      if (txt.text() === '' && GAME.turn === 1 && GAME.gameInPlay) {

        GAME.gameplay.updateBoard(symb, num);
        GAME.gameplay.turnOver(1);

      }
    },
    computerTurn: function() {
      var symb = GAME.player2;
      if (GAME.gameInPlay) {
        setTimeout(function() {

          var num = GAME.computer.chooseSpot();
          console.log(num);
          GAME.gameplay.updateBoard(symb, num);

          GAME.gameplay.turnOver(2);
        }, 2000);
      }
    },
    turnOver: function(player) {
      GAME.numFilled = GAME.numFilled + 1;

      if (GAME.gameplay.checkWin() === 0 && GAME.numFilled < 9) {
        if (player === 1) {
          GAME.display.hideP1Turn();
          GAME.display.showP2Turn();
          GAME.turn = 2;
          GAME.gameplay.computerTurn();
        } else {
          GAME.display.hideP2Turn();
          GAME.display.showP1Turn();
          GAME.turn = 1;
          //incomplete
        }
      } else {
        GAME.gameInPlay = false;

        if (GAME.turn === 1) {
          GAME.display.hideP1Turn();
        } else {
          GAME.display.hideP2Turn();
        }
        if (GAME.gameplay.checkWin() === 1) {

          GAME.display.showWin();
          GAME.p1Score++;
          $('.p1-score .score').text(GAME.p1Score);
        } else if (GAME.gameplay.checkWin() === 2) {

          GAME.display.showLose();
          GAME.p2Score++;
          $('.p2-score .score').text(GAME.p2Score);
        } else {

          GAME.display.showDraw();
        }
        GAME.gameplay.resetGame();
      }
    },
    matchElem: function(arr, checkArr) {
      var newArr = arr.filter(function(elem) {
        return checkArr.indexOf(elem) > -1;
      });
      return newArr.length === checkArr.length;
    },
    checkWin: function(arr) {
      for (var i = 0; i < GAME.winningCombos.length; i++) {
        if (GAME.gameplay.matchElem(GAME.p1Arr, GAME.winningCombos[i])) {
          return 1;
        }
        if (GAME.gameplay.matchElem(GAME.p2Arr, GAME.winningCombos[i])) {
          return 2;
        }

      }
      return 0;
    },
    updateBoard: function(symb, num) {
      if (GAME.turn === 1) {
        GAME.p1Arr.push(parseInt(num));
      } else {
        GAME.p2Arr.push(parseInt(num));
      }
      $('.' + num + ' span').text(symb);
      GAME.currentBoard[num] = symb;
      var index = GAME.availSpace.indexOf(num);
      GAME.availSpace.splice(index, 1);
    },
    resetGame: function() {
      GAME.initVars();
      setTimeout(function() {
        GAME.display.hideDraw();
        GAME.display.hideLose();
        GAME.display.hideWin();
        $('.boxes li').fadeOut();
      }, 4000);
      setTimeout(function() {
        GAME.resetSquares();
        $('.boxes li').fadeIn();
        GAME.numFilledIn = 0;
      }, 5000);
      setTimeout(function() {
        GAME.gameplay.play();
      }, 5000);
    },
    fullReset: function() {
      GAME.gameInPlay = false;
      GAME.display.showChoice();
      GAME.p1Score = 0;
      GAME.p2Score = 0;
      $('.p1-score .score').text(GAME.p1Score);
      $('.p2-score .score').text(GAME.p2Score);
      GAME.resetSquares();
      GAME.initVars();
      GAME.player1 = null;
      GAME.player2 = null;
    }

  },
  computer: {

    chooseSpot: function() {

      var num = GAME.computer.checkWinOrBlock('win');
      console.log(num);
      if (!num) {

        num = GAME.computer.checkWinOrBlock('block');

      }
      if (!num) {
        console.log('not block');
        num = GAME.computer.center();
      }
      if (!num) {

        num = GAME.computer.corner();
      }
      if (!num) {

        num = GAME.computer.side();
        console.log(num);
      }
      num = (num && GAME.computer.checkAvail(num)) ? num : false;
      return num;
    },
    checkWinOrBlock: function(choice) {

      var newArr,
        tempArr,
        finalArr = [];
      var compArr;
      if (choice === 'win') {

        compArr = GAME.p2Arr;
      } else if (choice === 'block') {
        console.log('check3')
        compArr = GAME.p1Arr;
      } else {
        return;
      }
      GAME.winningCombos.forEach(function(combo) {

        newArr = compArr.filter(function(elem) {
          return combo.indexOf(elem) > -1;
        });
        console.log(newArr);
        console.log(GAME.currentBoard);
        if (newArr.length === 2) {
          tempArr = combo.filter(function(elem) {
            return compArr.indexOf(elem) === -1;
          });

          if (GAME.computer.checkAvail(tempArr[0])) {
            finalArr.push(tempArr[0]);

          }

        }

      });

      console.log(finalArr[0]);

      if (finalArr[0]) {
        return finalArr[0].toString();
      }
      return;

    },

    center: function() {
      if (GAME.currentBoard[5] === '') {
        return 5;
      }
      return;
    },
    corner: function() {
      var corners = [1, 3, 7, 9];
      var availCorn = [];
      corners.forEach(function(elem) {
        if (GAME.computer.checkAvail(elem)) {
          availCorn.push(elem);
        }
      });
      console.log(availCorn);
      if (availCorn.length > 0) {
        console.log(availCorn);
        var index = Math.floor(Math.random() * availCorn.length);
        return availCorn[index];
      }
      return;
    },
    side: function() {
      var sides = [2, 4, 6, 8];
      var availSides = [];
      sides.forEach(function(elem) {
        if (GAME.computer.checkAvail(elem)) {
          availSides.push(elem);
        }
      });
      if (availSides.length > 0) {
        return Math.floor(Math.random() * availSides.length) + 1;

      }
      return;
    },
    checkAvail: function(num) {
      if (GAME.currentBoard[num] === '') {
        return true;
      }
      return false;
    }

  }
};
$(document).ready(function() {
  GAME.beginGame();

});
              
            
!
999px

Console