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="row">
    <div class="selectSign">
      <div class="col-md-6 col-xs-6 col-md-offset-3 col-xs-offset-3">
        <div class="selectBox" style="margin-top:15%">
          <h2 class="text-center" style="padding-top:25%;color:#FAFAFA;font-family:font-family: 'Josefin Sans', sans-serif;font-size:32px;">Please Select An Option</h2>
          <div class="row" style="padding-top:15%;padding-left:28%">
            <div class="col-xs-6 col-md-6">
              <h3 class="signX">X</h3>
            </div>
            <div class="col-xs-6 col-md-6">
              <h3 class="signO">O</h3>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!--Playing Board-->
  <div class="playing-board">
    <div class="row">
      <div class="col-md-12 col-xs-12">
        <h1 class="text-center title">Tic Tac Toe</h1>
      </div>
    </div>

    <div class="row calculator" style="margin-left:25%">
      <div class="col-md-6 col-xs-6">
        <h3 class="player_win" style="font-family: 'Acme', sans-serif;">Wins</h3>
        <h4 class="score_win" style="font-family: 'Acme', sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</h4>
      </div>

      <div class="col-md-6 col-xs-6">
        <h3 class="player_loose" style="font-family: 'Acme', sans-serif;">Loose</h3>
        <h4 class="score_loose" style="font-family: 'Acme', sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</h4>
      </div>
    </div>

    <div class="row">
      <div class="col-md-9 col-xs-9">
        <button class="btn btn-default" id="reset" style="margin-left:62%" style="font-family: 'Acme', sans-serif;">Reset All</button>
      </div>
      <div class="col-md-4 col-xs-4"></div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12 col-xs-12 playing-board">
      <table border="1" class="border" style="margin:auto;margin-top:8%;">
        <tr>
          <td class="square" id="b1"></td>
          <td class="square" id="b2"></td>
          <td class="square" id="b3"></td>
        </tr>
        <tr>
          <td class="square" id="b4"></td>
          <td class="square" id="b5"></td>
          <td class="square" id="b6"></td>
        </tr>
        <tr>
          <td class="square" id="b7"></td>
          <td class="square" id="b8"></td>
          <td class="square" id="b9"></td>
        </tr>
      </table>
    </div>
  </div>
</div>
<div class="footer">
  <h4 class="text-center" style="margin-top:5%;">Coded with ❤ By Mohammad Hasham</h4>
</div>
</div>
            
          
!
            
              body {
  background-color: #9CCC65;
}

.title {
  font-size: 54px;
  font-family: 'Acme', sans-serif;
}

.square {
  width: 90px;
  height: 90px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  background-color: #C6FF00;
  color: #795548;
  font-size: 44px;
  font-family: 'Architects Daughter', cursive;
}

.selectBox {
  width: 500px;
  height: 370px;
  background-color: #A1887F;
  box-shadow: 10px 10px 10px 10px;
  border-radius: 10%;
}

.signX {
  cursor: pointer;
  color: #FAFAFA;
  font-size: 48px;
  font-family: font-family: 'Josefin Sans'
}

#reset {
  background-color: #FFEE58;
}

.signO {
  cursor: pointer;
  color: #FAFAFA;
  font-size: 48px;
  font-family: font-family: 'Josefin Sans'
}

#b1 {
  border-left: solid white;
  border-top: solid white;
  cursor: pointer;
}

#b2,
#b3 {
  border-top: solid white;
  cursor: pointer;
}

#b7,
#b8,
#b9 {
  border-bottom: solid white;
  cursor: pointer;
}

#b4,
#b7 {
  border-left: solid white;
  cursor: pointer;
}

#b3,
#b6,
#b9 {
  border-right: solid white;
  cursor: pointer;
}

.footer {
  font-family: 'Shadows Into Light', cursive;
  font-size: 12px;
}
            
          
!
            
              $(document).ready(function() {

  $('.playing-board').hide();
  var flag = 0,
    win = 0,
    loose = 0;
  var computer_sign, j = 0,
    sign_option;
  var arr = [];
  var arr1 = [];

  for (var i = 0; i <= 9; i++) {
    arr.push('');
    arr1.push('');
  }

  //getting user's input
  $('.signX').on('click', function() {
    sign_option = 'X';
    if (sign_option == 'X') {
      computer_sign = 'O';
    }
    transition();

  });
  $('.signO').on('click', function() {
    sign_option = 'O';
    if (sign_option == 'O') {
      computer_sign = 'X';
    }
    transition();

  });

  function transition() {
    $('.playing-board').fadeIn(1000);
    $('.selectSign').hide();
    $('.playing-board').show();
  }

  //prompt user to select an option

  //   an example one function 
  //   assumed player moves first
  $('td').on('click', function(e) {

    var id = $(this).attr('id'); //getting the id of the clicked td element
    var i = id.slice(-1); //getting the last char of the id

    if (arr[i] === '') // checking to see if the cell is empty
    {
      arr[i] = sign_option; //place the sign into the corresponding array index

      $('#' + id).html(sign_option); //show the user sign_option

      player_win(i);

      computerMove(i); //caling the AI(computer) moves function
    } else {
      alert('Woops! Cell aready taken');
    }
  });

  function computerMove(i) {

    //      ADD AI LOGIC
    var generatedNumber = getRandomInt(0, 10);
    var cmove = '#b' + generatedNumber;

    if (generatedNumber === 0) {
      zeroException(i);
    } else if (arr[generatedNumber] === '') {
      $(cmove).html(computer_sign);
      arr[generatedNumber] = computer_sign; //Handling casses other than zero.
      arr1[generatedNumber] = computer_sign;
      j++;
      if (flag != 1) {
        player_loose(j);
      }
      console.log(arr1);

    } else {

      computerMove(i);
    }
  }

  function zeroException() {
    var generatedNumber;
    generatedNumber = getRandomInt(0, 10);
    computerMove(generatedNumber);
  }

  function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
  }

  function player_win(i) {

    if (arr[1] == sign_option && arr[2] == sign_option && arr[3] == sign_option) {
      alert('You Won!');
      flag = 1;
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);

    } else if (arr[1] == sign_option && arr[4] == sign_option && arr[7] == sign_option) {
      alert('You Won!');
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);
      flag = 1;
    } else if (arr[3] == sign_option && arr[6] == sign_option && arr[9] == sign_option) {
      alert('You Won!');
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);
      flag = 1;
    } else if (arr[7] == sign_option && arr[8] == sign_option && arr[9] == sign_option) {
      alert('You Win!');
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);
      flag = 1;
    } else if (arr[1] == sign_option && arr[5] == sign_option && arr[9] == sign_option) {
      alert('You Win!');
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);
      flag = 1;
    } else if (arr[3] == sign_option && arr[5] == sign_option && arr[7] == sign_option) {
      alert('You Win!');
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);
      flag = 1;
    } else if (arr[4] == sign_option && arr[5] == sign_option && arr[6] == sign_option) {
      alert('You Win!');
      win++;
      $('.score_win').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + win);
      flag = 1;
    }
  }

  function player_loose(j) {

    if (arr1[1] == computer_sign && arr1[2] == computer_sign && arr1[3] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    } else if (arr1[1] == computer_sign && arr1[4] == computer_sign && arr1[7] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    } else if (arr1[3] == computer_sign && arr1[6] == computer_sign && arr1[9] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    } else if (arr1[7] == computer_sign && arr1[8] == computer_sign && arr1[9] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    } else if (arr1[1] == computer_sign && arr1[5] == computer_sign && arr1[9] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    } else if (arr1[3] == computer_sign && arr1[5] == computer_sign && arr1[7] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    } else if (arr1[4] == computer_sign && arr1[5] == computer_sign && arr1[6] == computer_sign) {
      alert('You Loose!');
      loose++;
      $('.score_loose').html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + loose);

    }

  }

  //reset function
  $('#reset').click(function() {

    for (var i = 0; i <= 9; i++) {
      $('#b' + i).text('');
      arr[i] = '';
      arr1[i] = '';

    }

  });

});
            
          
!
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