123

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.

            
              section.container
  h1 Title
  h3 Play As
    button.btn.btn-primary.icon#X(type='button') X
    button.btn.btn-primary.icon#O(type='button') O
  .board.center-block
    .row
      .col-xs-1.field#r1c1
      .col-xs-1.field#r1c2
      .col-xs-1.field#r1c3
    .row
      .col-xs-1.field#r2c1
      .col-xs-1.field#r2c2
      .col-xs-1.field#r2c3
    .row
      .col-xs-1.field#r3c1
      .col-xs-1.field#r3c2
      .col-xs-1.field#r3c3
            
          
!
            
              h1, h3
  text-align: center
  font-family: "Lucidia Console", monospace
  color: #663300
section
  padding-top: 10px
  padding-bottom: 10px
  color: #fff
  bottom: 10px
body
  background: #222
.field
  border-color: #fff
  border-radius: 15%
  width: 150px
  height: 150px
  border-width: 5px
.board
  margin: 40px auto
  display: block
  width: 600px
.ico
  margin: 40px auto
  display: block
  width: 295px
  font-size: 80px
.winningRow
  background-color: red
#r2c1
  border-style: solid solid solid none
#r2c2 
  border-style: solid none solid none
#r2c3 
  border-style: solid none solid solid
#r1c1 
  border-style: none solid none none
#r1c3 
  border-style: none none none solid
#r3c1 
  border-style: none solid none none
#r3c3 
  border-style: none none none solid 
            
          
!
            
              $(document).ready(function(){
// Declare Players
  var player = 'X';
  var machine = 'O';
  var curPlayer = player;

  // Other needed Variables
  var gameOver = false;
  var lockIcon = false;
  var moves = 1;

  $('.field').on('click', MarkPosition);

  $('.icon').on('click', SetIcon);

  // Set the icon for the players
  function SetIcon() {
    if ($(this).attr('id') === 'X' && !lockIcon) {
      player = 'X';
      machine = 'O';
      lockIcon = true;
    } else if ($(this).attr('id') === 'O' && !lockIcon) {
      player = 'O';
      machine = 'X';
      lockIcon = true;
    }

    curPlayer = player;

    // Adjust the right setting
    $('.player').removeClass('X');
    $('.player').removeClass('O');
    $('.machine').removeClass('O');
    $('.machine').removeClass('X');
    $('.player').addClass(player);
    $('.machine').addClass(machine);
  };

  // Player Toggler
  function PToggler(cplayer) {
    cplayer === player ? curPlayer = machine : curPlayer = player;
  };

  // Check for a draw
  function DrawChecker() {
    if (moves === 9) {
      setTimeout(Clear, 1000);
    }
  };

  // Check for a win
  function WinCheck() {
    switch (true) {
      case $('#r1c1').text() === curPlayer && $('#r1c2').text() === curPlayer &&
      $('#r1c3').text() === curPlayer:
        DrawLine('#r1c1', '#r1c2', '#r1c3');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r2c1').text() === curPlayer && $('#r2c2').text() === curPlayer &&
      $('#r2c3').text() === curPlayer:
        DrawLine('#r2c1', '#r2c2', '#r2c3');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r3c1').text() === curPlayer && $('#r3c2').text() === curPlayer &&
      $('#r3c3').text() === curPlayer:
        DrawLine('#r3c1', '#r3c2', '#r3c3');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r1c1').text() === curPlayer && $('#r2c1').text() === curPlayer &&
      $('#r3c1').text() === curPlayer:
        DrawLine('#r1c1', '#r2c1', '#r3c1');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r1c2').text() === curPlayer && $('#r2c2').text() === curPlayer &&
      $('#r3c2').text() === curPlayer:
        DrawLine('#r1c2', '#r2c2', '#r3c2');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r1c3').text() === curPlayer && $('#r2c3').text() === curPlayer &&
      $('#r3c3').text() === curPlayer:
        DrawLine('#r1c3', '#r2c3', '#r3c3');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r1c1').text() === curPlayer && $('#r2c2').text() === curPlayer &&
      $('#r3c3').text() === curPlayer:
        DrawLine('#r1c1', '#r2c2', '#r3c3');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      case $('#r1c3').text() === curPlayer && $('#r2c2').text() === curPlayer &&
      $('#r3c1').text() === curPlayer:
        DrawLine('#r1c3', '#r2c2', '#r3c1');
        gameOver = true;
        setTimeout(UpdateScore, 1000);
        break;
      default:

        DrawChecker();
    }
  };

  // Clarify the winning hand
  function DrawLine(pos1, pos2, pos3) {
    var $pos1 = $(pos1);
    var $pos2 = $(pos2);
    var $pos3 = $(pos3);
    $pos1.addClass('winningRow');
    $pos2.addClass('winningRow');
    $pos3.addClass('winningRow');

    // Fix for the race problem of JS vs jQuery toggling the current player befre the score is changed.
    PToggler(curPlayer);
  }

  // Updates the score and clears the board by calling the Clear function.
  function UpdateScore() {
    $('.' + curPlayer).text(+$('.' + curPlayer).text() + 1);
    Clear();
  }

  //Clear the board
  function Clear() {
    // Clear board
    $('.field').empty();

    // Allow board to be clickable again
    $('.field').removeClass('clicked');

    // Clear winner line
    $('div').removeClass('winningRow');

    // Reset Moves
    moves = 1;

    // Reset gameover
    gameOver = false;

    // Reset Player to chosen one.
    SetIcon();

  };

  // FUnctiont o draw icon on the board.
  function DrawIcon(id) {
    $('#' + id).html('<p class="ico">' + curPlayer + '</p>');
  }

  //Mark position clicked
  function MarkPosition() {
    lockIcon = true;
    var id = $(this).attr('id');

    // Prevent changing already selected grid
    if (!$('#' + id).hasClass('clicked')) {
      $('#' + id).addClass('clicked');

      // Add Icon to the board
      DrawIcon(id);

      WinCheck();

      // Mark number of moves
      moves += 1;

      PToggler(curPlayer);

      // Add Icon to the board for machine
      if (gameOver === false && moves % 2 === 0) {
        MachineAI();
        WinCheck();
        moves += 1;
        PToggler(curPlayer);
      }
    }
  };

  // just place moves on the board, bare minimum implementation
  function MachineAI() {
    switch (true) {
      case $('#r1c1').text() !== player && $('#r1c1').text() !== machine:
        DrawIcon('r1c1');
        break;
      case $('#r1c2').text() !== player && $('#r1c2').text() !== machine:
        DrawIcon('r1c2');
        break;
      case $('#r1c3').text() !== player && $('#r1c3').text() !== machine:
        DrawIcon('r1c3');
        break;
      case $('#r2c1').text() !== player && $('#r2c1').text() !== machine:
        DrawIcon('r2c1');
        break;
      case $('#r2c2').text() !== player && $('#r2c2').text() !== machine:
        DrawIcon('r2c2');
        break;
      case $('#r2c3').text() !== player && $('#r2c3').text() !== machine:
        DrawIcon('r2c3');
        break;
      case $('#r3c1').text() !== player && $('#r3c1').text() !== machine:
        DrawIcon('r3c1');
        break;
      case $('#r3c2').text() !== player && $('#r3c2').text() !== machine:
        DrawIcon('r3c2');
        break;
      case $('r3c3').text() !== player && $('#r2c3').text() !== machine:
        DrawIcon('r3c3');
        break;
    }
  };
});
            
          
!
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