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

              
                <div class="container" ng-app="StarWarsToe" ng-controller="swtController">
  <div class="start-screen" ng-show="!started">
    <a href class="start" ng-click="start('start-screen')">Start the battle</a>
    <p>Made by <a href="https://saintgeo23.github.io/">saintgeo23</a><br>for <a href="https://www.freecodecamp.com/">FreeCodeCamp</a> project</p>
    <p>Inspired by <a href="https://www.behance.net/gallery/17998561/Star-Wars-Long-Shadow-Flat-Design-Icons">Filipe de Carvalho</a></p>
  </div>
  
  <div class="choose-block" ng-show="started && !sideChosen">
    <h1>Choose your side!</h1>
    <h3>But choose wisely!</h3>
    <div class="choose-block__links">
      <a href class="choose-block__links__link obi"><img src="https://useful-info.github.io/sites/mamarada/images/obi.png"></a>
      <a href class="choose-block__links__link darth"><img src="https://useful-info.github.io/sites/mamarada/images/darth.png"></a>
    </div>
  </div>
  
  <div class="board" ng-show="sideChosen">
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(0, 0, 0)" id="0"><img ng-src="{{sides[board[0][0]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(0, 1, 1)" id="1"><img ng-src="{{sides[board[0][1]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(0, 2, 2)" id="2"><img ng-src="{{sides[board[0][2]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(1, 0, 3)" id="3"><img ng-src="{{sides[board[1][0]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(1, 1, 4)" id="4"><img ng-src="{{sides[board[1][1]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(1, 2, 5)" id="5"><img ng-src="{{sides[board[1][2]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(2, 0, 6)" id="6"><img ng-src="{{sides[board[2][0]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(2, 1, 7)" id="7"><img ng-src="{{sides[board[2][1]]}}" alt="cell"></a></div>
    <div class="board__cell"><a href class="board__cell__btn" ng-click="playTurn(2, 2, 8)" id="8"><img ng-src="{{sides[board[2][2]]}}" alt="cell"></a></div>
  </div>
  
  <div class="result" ng-if="isWin || isDraw || isLose">
    <h1 ng-if="isWin">You won!</h1>
    <h1 ng-if="isDraw">Draw!</h1>
    <h1 ng-if="isLose">You lost!</h1>
    <a href class="replay" ng-click="replay('result')">Play again</a>
    <a href class="change" ng-click="start('result')">Change side</a>
  </div>
  
  
</div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Orbitron:400,900);

* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
  font-family: 'Orbitron', sans-serif;
}

body {
  background-image: url(https://useful-info.github.io/sites/mamarada/images/swbg.jpg);
  background-repeat: repeat;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.container {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center; 
  overflow: hidden;
}

.start-screen {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center; 
  overflow: hidden;
  text-align: center;
}

.start-screen p {
  color: #ffeb3b;
  font-weight: 400;
  font-size: 0.8em;
  margin-top: 15px;
}
.start-screen p+p {
  color: #ffeb3b;
  margin-top: 10px;
}

.start-screen p a {
  color: #ffffff;
}

.start,
.replay,
.change {
  display: block;
  width: 300px;
  height: 90px;
  line-height: 85px;
  vertical-align: middle;
  box-sizing: border-box;
  border: 3px solid #ffeb3b;
  border-radius: 5px;
  color: #ffeb3b;
  font-size: 2em;
  font-weight: 900;
  text-align: center;
  opacity: 1;
  transition: all 500ms ease-in;
}

.choose-block {
  position: relative;
  display: block;
  top: 0;
  width: 100%;
  max-width: 400px;
  text-align: center;
}

h1,
h3 {
  color: #ffeb3b;
}

h1 {
  font-size: 2em;
}

h3 {
  font-size: 0.8em;
  font-weight: 400;
  margin: 7px 0 10px;
}

.choose-block__links {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.choose-block__links__link {
  display: inline-block;
  width: 50%;
}

.choose-block__links__link img {
  width: 100%;
  height: auto;
}

.board {
  position: relative;
  display: block;
  top: 0;
  width: 100%;
  max-width: 610px;
  text-align: center;
  -moz-column-count: 3;
  -webkit-column-count: 3;
  column-count: 3;
  column-gap: 5px;
  -moz-column-gap: 5px;
  -webkit-column-gap: 5px;
}

.board__cell {
  display: inline-block;
  width: 100%;
}

.board img {
  width: 100%;
  height: auto;
  transition: background-image 500ms ease-in;
}

.result {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: rgba(0, 0, 0, 0.75);
}

.result h1 {
  font-size: 3em;
  margin: 0 0 10px;
}

.result a+a {
  margin: 10px 0 0;
}

@media screen and (orientation: landscape) and (max-height: 610px) {
  .choose-block h1 {
    font-size: 1.5em;
  }
  
  h3 {
    font-size: 0.8em;
    margin: 3px 0 5px;
  }
  
  .board {
    width: 50%;
  }
}

              
            
!

JS

              
                var app = angular.module('StarWarsToe', []);


app.controller('swtController', function($scope) {
  
  
  
  $scope.sides = {
    '0': 'https://useful-info.github.io/sites/mamarada/images/cell-bg.jpg',
    '1': 'https://useful-info.github.io/sites/mamarada/images/obi.png',
    '2': 'https://useful-info.github.io/sites/mamarada/images/darth.png'
  };
   
  
  $scope.cells = ["0,0", "0,1", "0,2", "1,0", "1,1", "1,2", "2,0", "2,1", "2,2"];
  
  
  $scope.started = false;
  $scope.sideChosen = false;
  
  
  $scope.reset = function() {
    $scope.isWin = false;
    $scope.isDraw = false;
    $scope.isLose = false;
    $scope.myTurn = true;
    $scope.board = [['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0']];
    for (var id = 0; id < 9; id++) {
      $('#' + id).removeClass('pressed');
    };
    
  };
  
  
  $scope.replay = function(classname) {
     $('.' + classname).fadeOut({
       duration: 200,
       done: function() {
         $scope.reset();
         $scope.$apply();
         $('.board').fadeIn({
           duration: 200,
           done: function() {
             $scope.sideChosen = true;
             $scope.$apply();
           }
         });
         
       }
     });
  };
  
  
  function ifWin() {
    if (($scope.board[0][0] === $scope.board[0][1] && $scope.board[0][1] === $scope.board[0][2] && $scope.board[0][0] !== '0') ||
       ($scope.board[1][0] === $scope.board[1][1] && $scope.board[1][1] === $scope.board[1][2] && $scope.board[1][0] !== '0') ||
       ($scope.board[2][0] === $scope.board[2][1] && $scope.board[2][1] === $scope.board[2][2] && $scope.board[2][0] !== '0') ||
       ($scope.board[0][0] === $scope.board[1][0] && $scope.board[1][0] === $scope.board[2][0] && $scope.board[0][0] !== '0') ||
       ($scope.board[0][1] === $scope.board[1][1] && $scope.board[1][1] === $scope.board[2][1] && $scope.board[0][1] !== '0') ||
       ($scope.board[0][2] === $scope.board[1][2] && $scope.board[1][2] === $scope.board[2][2] && $scope.board[0][2] !== '0') ||
       ($scope.board[0][0] === $scope.board[1][1] && $scope.board[1][1] === $scope.board[2][2] && $scope.board[0][0] !== '0') ||
       ($scope.board[0][2] === $scope.board[1][1] && $scope.board[1][1] === $scope.board[2][0] && $scope.board[0][2] !== '0')) {
      console.log('You win!');
      $scope.isWin = true;
    }
  };
  
  
  function ifDraw() {
    var empty1 = [];
    for (var i = 0; i < 3; i++) {
      for (var j = 0; j < 3; j++) {
        if ($scope.board[i][j] === '0') {
          empty1.push([i, j]);
        }
      }
    }
    if (empty1.length === 0) {
      $scope.draw();
    } else {
      $scope.myTurn = true;
    }
  };
  
  
  $scope.start = function(classname) {
    $('.' + classname).fadeOut({
      duration: 200,
      done: function() {
        $scope.reset();
        $scope.started = false;
        $scope.sideChosen = false;
        $scope.$apply();
        $('.choose-block').fadeIn({
          duration: 200,
          done: function() {
            $scope.started = true;
            $scope.$apply();
          }
        });
        console.log('Let the battle begins!');
      }
    });
  };
  
  
  $('.obi').click(function() {
    $scope.side = '1';
    $scope.aiSide = '2';
    $scope.reset();
    $('.choose-block').fadeOut({
      duration: 200,
      done: function() {
        $scope.sideChosen = true;
        $scope.$apply();
      }
    })
    console.log('May the force be with you!');
  });
  
  
  $('.darth').click(function() {
    $scope.side = '2';
    $scope.aiSide = '1';
    $scope.reset();
    $scope.sideChosen = true;
    $scope.$apply();
    console.log('Do not fail me!');
  });
 
  
  $scope.lose = function() {
    $scope.myTurn = false;
    $scope.isLose = true;
    console.log('You lose!');
  };
  
  
  $scope.draw = function() {
    $scope.isDraw = true;
    $scope.myTurn = false;
    console.log('Draw!')
  };
  
  
  // Here is AI tactics
  $scope.aiTurn = function() {
    // First AI tries to find if it can win with this turn
    if ((($scope.board[0][1] === $scope.aiSide && $scope.board[0][2] === $scope.aiSide) || ($scope.board[1][0] === $scope.aiSide && $scope.board[2][0] === $scope.aiSide) || ($scope.board[1][1] === $scope.aiSide && $scope.board[2][2] === $scope.aiSide)) && !$('#0').hasClass('pressed')) {
      $scope.aiTakeTurn(0, 0, '#0');
      $scope.lose();
      return;
    } else if ((($scope.board[0][0] === $scope.aiSide && $scope.board[2][0] === $scope.aiSide) || ($scope.board[1][1] === $scope.aiSide && $scope.board[1][2] === $scope.aiSide)) && !$('#3').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 0, '#3');
      $scope.lose();
      return;
    } else if ((($scope.board[0][0] === $scope.aiSide && $scope.board[1][0] === $scope.aiSide) || ($scope.board[2][1] === $scope.aiSide && $scope.board[2][2] === $scope.aiSide) || ($scope.board[1][1] === $scope.aiSide && $scope.board[0][2] === $scope.aiSide)) && !$('#6').hasClass('pressed')) {
      $scope.aiTakeTurn(2, 0, '#6');
      $scope.lose();
      return;
    } else if ((($scope.board[0][0] === $scope.aiSide && $scope.board[0][2] === $scope.aiSide) || ($scope.board[1][1] === $scope.aiSide && $scope.board[2][1] === $scope.aiSide)) && !$('#1').hasClass('pressed')) {
      $scope.aiTakeTurn(0, 1, '#1');
      $scope.lose();
      return;
    } else if ((($scope.board[0][0] === $scope.aiSide && $scope.board[2][2] === $scope.aiSide) || ($scope.board[2][0] === $scope.aiSide && $scope.board[0][2] === $scope.aiSide) || ($scope.board[0][1] === $scope.aiSide && $scope.board[2][1] === $scope.aiSide) || ($scope.board[1][0] === $scope.aiSide && $scope.board[1][2] === $scope.aiSide)) && !$('#4').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 1, '#4');
      $scope.lose();
      return;
    } else if ((($scope.board[0][1] === $scope.aiSide && $scope.board[1][1] === $scope.aiSide) || ($scope.board[2][0] === $scope.aiSide && $scope.board[2][2] === $scope.aiSide)) && !$('#7').hasClass('pressed')) {
      $scope.aiTakeTurn(2, 1, '#7');
      $scope.lose();
      return;
    } else if ((($scope.board[0][0] === $scope.aiSide && $scope.board[0][1] === $scope.aiSide) || ($scope.board[1][2] === $scope.aiSide && $scope.board[2][2] === $scope.aiSide) || ($scope.board[1][1] === $scope.aiSide && $scope.board[2][0] === $scope.aiSide)) && !$('#2').hasClass('pressed')) {
      $scope.aiTakeTurn(0, 2, '#2');
      $scope.lose();
      return;
    } else if ((($scope.board[0][2] === $scope.aiSide && $scope.board[2][2] === $scope.aiSide) || ($scope.board[1][0] === $scope.aiSide && $scope.board[1][1] === $scope.aiSide)) && !$('#5').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 2, '#5');
      $scope.lose();
      return;
    } else if ((($scope.board[0][2] === $scope.aiSide && $scope.board[1][2] === $scope.aiSide) || ($scope.board[2][0] === $scope.aiSide && $scope.board[2][1] === $scope.aiSide) || ($scope.board[1][1] === $scope.aiSide && $scope.board[0][0] === $scope.aiSide)) && !$('#8').hasClass('pressed')) {
      $scope.aiTakeTurn(2, 2, '#8');
      $scope.lose();
      return;
    } 
    // Then AI tries to prevent us from winning
    else if ((($scope.board[0][1] === $scope.side && $scope.board[0][2] === $scope.side) || ($scope.board[1][0] === $scope.side && $scope.board[2][0] === $scope.side) || ($scope.board[1][1] === $scope.side && $scope.board[2][2] === $scope.side)) && !$('#0').hasClass('pressed')) {
      $scope.aiTakeTurn(0, 0, '#0');
      ifDraw();
      return;
    } else if ((($scope.board[0][0] === $scope.side && $scope.board[2][0] === $scope.side) || ($scope.board[1][1] === $scope.side && $scope.board[1][2] === $scope.side)) && !$('#3').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 0, '#3');
      ifDraw();
      return;
    } else if ((($scope.board[0][0] === $scope.side && $scope.board[1][0] === $scope.side) || ($scope.board[2][1] === $scope.side && $scope.board[2][2] === $scope.side) || ($scope.board[1][1] === $scope.side && $scope.board[0][2] === $scope.side)) && !$('#6').hasClass('pressed')) {
      $scope.aiTakeTurn(2, 0, '#6');
      ifDraw();
      return;
    } else if ((($scope.board[0][0] === $scope.side && $scope.board[0][2] === $scope.side) || ($scope.board[1][1] === $scope.side && $scope.board[2][1] === $scope.side)) && !$('#1').hasClass('pressed')) {
      $scope.aiTakeTurn(0, 1, '#1');
      ifDraw();
      return;
    } else if ((($scope.board[0][0] === $scope.side && $scope.board[2][2] === $scope.side) || ($scope.board[2][0] === $scope.side && $scope.board[0][2] === $scope.side) || ($scope.board[0][1] === $scope.side && $scope.board[2][1] === $scope.side) || ($scope.board[1][0] === $scope.side && $scope.board[1][2] === $scope.side)) && !$('#4').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 1, '#4');
      ifDraw();
      return;
    } else if ((($scope.board[0][1] === $scope.side && $scope.board[1][1] === $scope.side) || ($scope.board[2][0] === $scope.side && $scope.board[2][2] === $scope.side)) && !$('#7').hasClass('pressed')) {
      $scope.aiTakeTurn(2, 1, '#7');
      ifDraw();
      return;
    } else if ((($scope.board[0][0] === $scope.side && $scope.board[0][1] === $scope.side) || ($scope.board[1][2] === $scope.side && $scope.board[2][2] === $scope.side) || ($scope.board[1][1] === $scope.side && $scope.board[2][0] === $scope.side)) && !$('#2').hasClass('pressed')) {
      $scope.aiTakeTurn(0, 2, '#2');
      ifDraw();
      return;
    } else if ((($scope.board[0][2] === $scope.side && $scope.board[2][2] === $scope.side) || ($scope.board[1][0] === $scope.side && $scope.board[1][1] === $scope.side)) && !$('#5').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 2, '#5');
      ifDraw();
      return;
    } else if ((($scope.board[0][2] === $scope.side && $scope.board[1][2] === $scope.side) || ($scope.board[2][0] === $scope.side && $scope.board[2][1] === $scope.side) || ($scope.board[1][1] === $scope.side && $scope.board[0][0] === $scope.side)) && !$('#8').hasClass('pressed')) {
      $scope.aiTakeTurn(2, 2, '#8');
      ifDraw();
      return;
    } 
    // Then AI tries to get center
    else if (!$('#4').hasClass('pressed')) {
      $scope.aiTakeTurn(1, 1, '#4');
      return;
    }
    // Then AI checks free places
    var empty = [];
    for (var i = 0; i < 3; i++) {
      for (var j = 0; j < 3; j++) {
        if ($scope.board[i][j] === '0') {
          empty.push([i, j]);
        }
      }
    }
    // If none - it is a draw
    if (empty.length === 0) {
      $scope.draw();
    } 
    // If just one - AI takes it (not sure if it is possible while player always plays first but just let it be here =) )
    else if (empty.length === 1) {
      $scope.board[empty[0][0]][empty[0][1]] = $scope.aiSide;
      $scope.draw();
    } 
    // Finally AI plays randomly
    else {
      var rnd = Math.floor(Math.random() * empty.length);
      var tempID = '#' + $scope.cells.indexOf(empty[rnd].toString());
      $scope.aiTakeTurn(empty[rnd][0], empty[rnd][1], tempID);
      $scope.myTurn = true;
    }
  };
    
  
  
  $scope.playTurn = function(a, b, id) {
    if ($scope.myTurn && !$('#' + id).hasClass('pressed')) {
      $('#' + id).fadeOut({
        duration: 100,
        done: function() {
          $scope.board[a][b] = $scope.side;
          $('#' + id).addClass('pressed');
          $('#' + id).fadeIn(100);
          $scope.myTurn = false;
          ifWin();
          if (!$scope.isWin) {
            $scope.aiTurn();
          }
          $scope.$apply();
        }
      });
    }
  };
  
  
  $scope.aiTakeTurn = function(a, b, id) {
    $(id).fadeOut({
      duration: 100,
      done: function() {
        $scope.board[a][b] = $scope.aiSide;
        $(id).addClass('pressed');
        $(id).fadeIn(100);
        $scope.myTurn = true;
        $scope.$apply();
      }
    });
  };
  
});
              
            
!
999px

Console