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 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="container">
  <div class="result">
    <p></p>
    <p><i></i></p>
  </div>
  <div class="tictactoe">
    <ul class="ttt">
      <li class="field" data-move='0'><i></i></li>
      <li class="field" data-move='1'><i></i></li>
      <li class="field" data-move='2'><i></i></li>
      <li class="field" data-move='3'><i></i></li>
      <li class="field" data-move='4'><i></i></li>
      <li class="field" data-move='5'><i></i></li>
      <li class="field" data-move='6'><i></i></li>
      <li class="field" data-move='7'><i></i></li>
      <li class="field" data-move='8'><i></i></li>
    </ul>
  </div>
  <div class="playerChoose">
    <p class="computer" data-player="0"><i class="fa fa-laptop"></i><span>Computer starts</span></p>
    <p class="player" data-player="1"><i class="fa fa-user"></i><span>Human starts</span></p>
  </div>
</div>
            
          
!
            
              $font: Roboto, sans-serif;
$background: #A7DBD8;
$background2: #69D2E7;
$front: #F38630;
$front2: #FA6900;

body {
  font: 100% $font;
  box-sizing: border-box;
  background: $background;
}

.center {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

.container {
  @extend .center;
  width: 25rem;
  height: 25rem;
}

.result {
  background: darken($background, 10%);
  background: rgba(darken($background, 10%), 0.7);
  @extend .center;
  opacity: 0;
  z-index: 1;
  visibility: hidden;
  
  p {
    padding-top: calc(50% - 2*3rem);
    font-size: 3rem;
    vertical-align: middle;
    text-align: center;
  }
}

.ttt {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  align-content: center;
  align-items: center;
  list-style: none;
  
  .field {
    color: $front;
    flex: 1 1 auto;
    text-align: center;
    vertical-align: middle;
    width: 8.2rem;
    height: 8.3rem;
    font-size: 7rem;
    line-height: 8.3rem;
  }
  
  li {
    &:nth-child(3n+1) {
    border-right: 1px solid $front;
    }
    
    &:nth-child(3n+3) {
      border-left: 1px solid $front;
    }
    
    &:nth-child(-n+6) {
      border-bottom: 1px solid $front;
    }
    
    &:hover {
      background: lighten($background, 2%);
    }
  }
}

.playerChoose {
  
  width: 100%;
  padding-top: 3rem;
  color: $front2;
  
  p {
    text-align: center;
    
    &:hover {
      color: lighten($front2, 20%);
      cursor: pointer;
    }
  }
  
  .computer {
    float: left;
  }
  .player {
    float: right;
  }
  
  i {
    display: block;
    font-size: 3rem;
  }
  
  p span {
    display: block;
    font-size: 1.2rem;
  }
}
            
          
!
            
              function Board() {
  var board = Array.apply(null, Array(9)).map(Number.prototype.valueOf, -1);
  var winningLines = [
    [0, 1, 2],
    [0, 4, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [2, 4, 6],
    [3, 4, 5],
    [6, 7, 8]
  ];

  this.isPositionFree = function(pos) {
    return board[pos] === -1;
  }

  this.getFreePositions = function() {
    var pos = [];
    for (var i = 0; i < board.length; i++) {
      if (this.isPositionFree(i)) pos.push(i);
    }
    return pos;
  }
  
  this.getPositionValue = function(pos) {
    return board[pos];
  }
  
  this.markPosition = function (player, pos) {
    if (this.isPositionFree(pos)) {
      board[pos] = player;
      return true;
    }
    return false;
  }
  
  this.getWinningLines = function() {
    return winningLines;
  }

}

function AI() {
  this.getComputerMove = function(board) {
    var moves = board.getFreePositions();
    var pos =  this.generateMove(moves.length);
    return moves[pos];
  }
  
  this.generateMove = function(max) {
    return Math.floor((Math.random() * max) + 1) - 1;
  }
}

function Game(startingPlayer, jquery) {
  this.startingPlayer = startingPlayer;
  this.currentPlayer = startingPlayer;
  var numberOfMoves = 0;
  var board = new Board();
  var ai = new AI();
  var playerWon = -1;
  var $ = jquery;

  this.changePlayer = function() {
    if (this.currentPlayer === 0) this.currentPlayer = 1;
    else this.currentPlayer = 0;
  }

  this.getCurrentPlayer = function() {
    return this.currentPlayer;
  }
  
  this.move = function(player, pos) {
    if (this.end()) return false;
    if (board.markPosition(player, pos)) {
      numberOfMoves += 1;
      if (this.hasWon(player)) this.playerWon = player;
      if (this.end()) this.showResult();
      return true;
    }
    return false;
  }
  
  this.possibleMove = function(pos) {
    return board.isPositionFree(pos);
  }
  
  this.computerMove = function() {
    if (this.end()) return false;
    var move = ai.getComputerMove(board);
    $('.field[data-move=' + move + ']').addClass('fa fa-circle-o');
    if (this.move(this.getCurrentPlayer(), move)) this.changePlayer();
  }
  
  this.hasWon = function(player) {
    var that = this;
    return board.getWinningLines().some(function(line) {
      for (var i = 0; i < line.length; i++) {
        var pos = line[i];
        if (board.getPositionValue(pos) !== player) return false;
      }
      playerWon = player;
      return true;
    });
  }
  
  this.end = function() {
    if (playerWon !== -1 || numberOfMoves === 9) return true;
    return false;
  }
  
  this.showResult = function() {
    $('.result').css({'visibility': 'visible'});
    if (numberOfMoves === 9 && playerWon === -1) {
      $('.result p:first-child').text('TIE! Good game!');
      $('.result').fadeTo(1000, 1.0);
    } else {
      if (playerWon === 0) {
        $('.result p:first-child').text('Computer Won!');
        $('.result i').addClass('fa fa-laptop');
      } else {
        $('.result p:first-child').text('Human Won!')
        $('.result i').addClass('fa fa-user');
      }
      $('.result').fadeTo(1000, 1.0);
    }
  }
  
  this.reset = function() {
    $('.field').removeClass('fa fa-circle-o fa-times');
    $('.result p:first-child').text('');
    $('.result i').removeClass('fa fa-laptop fa-user');
    this.currentPlayer = this.startingPlayer = playerWon = -1;
    numberOfMoves = 0;
    board = new Board();
    ai = new AI();
  }
}

$('document').ready(function() {
  var G;
  $('.playerChoose p').click(function(e) {
    if (G) return;
    if ($(this).hasClass('computer')) {
      G = new Game(0, jQuery);
      G.computerMove();
    } else G = new Game(1, jQuery); 
    e.preventDefault();
  });

  $('.field').click(function(e) {
    var pos = $(this).data('move');
    if (G && G.possibleMove(pos)) {
      if (G.getCurrentPlayer() === 1) {
        $(this).addClass('fa fa-times');
        G.move(G.getCurrentPlayer(), pos)
        G.changePlayer();
        G.computerMove();
      }
    } else {
      return false;
    }
    e.preventDefault();
  });
  
  $('.result').click(function(e) {
    G.reset();
    G = undefined;
    $(this).fadeOut(1000);
  });
});
            
          
!
999px
Loading ..................

Console