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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title text-center">Tic Tac Toe Game</h4>
        </div>
        <div class="modal-body">
          <h3>Welcome to the Tic Tac Toe Game!</h3>
          <p>It’s time to choose your figure.</p>
          <p id="test"></p>
        </div>
        <div class="modal-footer">
          <button type="button" id="x" class="btn" data-dismiss="modal" onClick="setFig(this.id)"><span class="fa fa-times"><span></button>
          <button type="button" id="o" class="btn" data-dismiss="modal" onClick="setFig(this.id)"><span class="fa fa-circle-o"><span></button>
        </div>
      </div>
    </div>
  </div>

  <div class="game">
    <div id="first" onClick="icon(this.id)" class="game-field"></div>
    <div id="second" onClick="icon(this.id)" class="game-field"></div>
    <div id="third" onClick="icon(this.id)" class="game-field"></div>
    <div id="fourth" onClick="icon(this.id)" class="game-field"></div>
    <div id="fifth" onClick="icon(this.id)" class="game-field"></div>
    <div id="sixth" onClick="icon(this.id)" class="game-field"></div>
    <div id="seventh" onClick="icon(this.id)" class="game-field"></div>
    <div id="eight" onClick="icon(this.id)" class="game-field"></div>
    <div id="nineth" onClick="icon(this.id)" class="game-field"></div>
  </div>
  <div class="row signature">
    <div class="col-xs-12">
    <h5 class="text-center animated fadeInUp" style="color:white">Made with <span style="color:#14CC60">&hearts;</span> by <a href="http://mrkaluzny.com" target="_blank" style="color:#14CC60">Wojciech Kałużny</a></h5>
    </div>
  </div>
</div>
            
          
!
            
              $border-style: 3px solid #525B76;
$mainColor: #29335C;
$secColor: #525B76;
$thiColor: #EAEAEA;
$fouColor: #E4572E;

@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600);

body {
  font-family: 'Source Sans Pro';
  background: $mainColor;
  background-size: cover;
}

.game {
  background-color: transparent;
  width: 402px;
  height: 402px;
  margin: 50px auto;
}

.game-field {
  width: 134px;
  height: 134px;
  display: block;
  float: left;
  color: $thiColor;
  font-size: 6.3em;
  padding: 1px 27px;
}

.win {
  color: $fouColor;
  transition: 200ms;
}

#first, #second, #third {
  border-bottom: $border-style;
}

#second {
  border-left: $border-style;
  border-right: $border-style;
}

#fourth, #fifth, #sixth {
  border-bottom: $border-style;
}

#fifth {
  border-left: $border-style;
  border-right: $border-style;
}
#eight {
  border-left: $border-style;
  border-right: $border-style;
}

.modal-content {
  background-color: $thiColor;
}

.modal-header {
  background-color: $mainColor;
  width: 100%;
  height: 100%;
  color: $thiColor;
  
  button {
    color: $thiColor;
    
    &:hover {
      color: $thiColor;
    }
  }
  
  h4 {
    font-size: 2.3em;
  }
}

.modal-body {
  padding: 30px;
  margin: 10px 0;
  text-align: center;
}

.modal-footer {
  padding: 10px 80px 10px 0px;
  
  button {
    background-color: white;
    color: black;
    width: 40%;
    font-size: 2em;
    transition: 500ms;
    
    &:hover { 
      background-color: $fouColor;
      color: white;
    }
  }
}

.signature {
  padding-top: 10px;
}
            
          
!
            
              /* Game object with all variables. */
var game = {
  first: true,
  user: '',
  computer: '',
  currentPlayer: '',
  moves: 1,
};


/* FIRST GAME FUNCTIONS */

// Show modal at the begining!
function start() {
  $('#myModal').modal('show');
}

function setFig(id) {
  if (id === 'x') {
    game.user = '<span class="fa fa-times"></span>';
    game.computer = '<span class="fa fa-circle-o"></span>';
  } else if (id === 'o') {
    game.user = '<span class="fa fa-circle-o"></span>';
    game.computer = '<span class="fa fa-times"></span>';
  }
  firstMove();
  setCurrPl('user');
}

/* GAME MECHANICS */
//First Move by Computer
function firstMove() {
  $('#fifth').html(game.computer);
  $('#fifth').removeAttr('onClick');
}

// Add icon to certain field
function icon(id) {
  if (game.currentPlayer == 'user') {
    $('#' + id).html(game.user);
    $('#' + id).removeAttr('onClick');
    gameStatus();
    setCurrPl('computer');
  } else if (game.currentPlayer == 'computer') {
    $('#' + id).html(game.computer);
    $('#' + id).removeAttr('onClick');
    gameStatus();
    setCurrPl('user');
  }
  game.moves++;
  draw();

  if (game.currentPlayer == 'computer') {
    comp();
  }
}

// How computer thinks?
function comp() {
  switch (true) {
    case $('#first').html() != game.user && $('#first').html() != game.computer:
      icon('first');
      break;
    case $('#second').html() !== game.user && $('#second').html() !== game.computer:
      icon('second');
      break;
    case $('#third').html() !== game.user && $('#third').html() !== game.computer:
      icon('third');
      break;
    case $('#fourth').html() !== game.user && $('#fourth').html() !== game.computer:
      icon('fourth');
      break;
    case $('#fifth').html() !== game.user && $('#fifth').html() !== game.computer:
      icon('fifth');
      break;
    case $('#sixth').html() !== game.user && $('#sixth').html() !== game.computer:
      icon('sixth');
      break;
    case $('#seventh').html() !== game.user && $('#seventh').html() !== game.computer:
      icon('seventh');
      break;
    case $('#eight').html() !== game.user && $('#eight').html() !== game.computer:
      icon('eight');
      break;
    case $('nineth').html() !== game.user && $('#nineth').html() !== game.computer:
      icon('nineth');
      break;
  }
};

//Mini functions
function lockAll() {
  $('.game-field').removeAttr('onClick');
}

function setCurrPl(curr) {
  game.currentPlayer = curr;
}

/* GAME OVER */

// Check game status - win/draw or still going?
function gameStatus() {
  var curPlayer;

  if (game.currentPlayer == 'user') {
    curPlayer = game.user;
  } else if (game.currentPlayer == 'computer') {
    curPlayer = game.computer;
  }

  switch (true) {
    case $('#first').html() === curPlayer && $('#second').html() === curPlayer &&
    $('#third').html() === curPlayer:
      show('#first', '#second', '#third');
      break;
    case $('#fourth').html() === curPlayer && $('#fifth').html() === curPlayer &&
    $('#sixth').html() === curPlayer:
      show('#fourth', '#fifth', '#sixth');
      break;
    case $('#seventh').html() === curPlayer && $('#eight').html() === curPlayer &&
    $('#nineth').html() === curPlayer:
      show('#seventh', '#eight', '#nineth');
      break;
    case $('#first').html() === curPlayer && $('#fourth').html() === curPlayer &&
    $('#seventh').html() === curPlayer:
      show('#first', '#fourth', '#seventh');
      break;
    case $('#second').html() === curPlayer && $('#fifth').html() === curPlayer &&
    $('#eight').html() === curPlayer:
      show('#second', '#fifth', '#eight');
      break;
    case $('#third').html() === curPlayer && $('#sixth').html() === curPlayer &&
    $('#nineth').html() === curPlayer:
      show('#third', '#sixth', '#nineth');
      break;
    case $('#first').html() === curPlayer && $('#fifth').html() === curPlayer &&
    $('#nineth').html() === curPlayer:
      show('#first', '#fifth', '#nineth');
      break;
    case $('#third').html() === curPlayer && $('#fifth').html() === curPlayer &&
    $('#seventh').html() === curPlayer:
      show('#third', '#fifth', '#seventh');
      break;
    default:
      draw();
  }
};

// Is it a draw??
function draw() {
  if (game.moves === 9) {
    setTimeout(reset, 1000);
  }
}

// Show which row wins!
function show(x, y, z) {
  var x = $(x),
    y = $(y),
    z = $(z);
  x.addClass('win');
  y.addClass('win');
  z.addClass('win');
  lockAll();
  setTimeout(reset, 1500);
}

/* RESET THE GAME */
function reset() {
  $('.game-field').html('');
  game.moves = 1;
  $('.game-field').attr('onClick', 'icon(this.id)');
  $('.win').removeClass('win');
  setTimeout(firstMove, 200);
}

setTimeout(start,600);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console