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 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.

Quick-add: + add another resource

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.

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 text-center">
  <div class="choose">Choose X or O</div>
  <a class="btn btn-primary" id="mutaX">Turn X</a>
  <a class="btn btn-primary" id="mutaO">Turn O</a>
  <div class="row">
    <div class="col-md-12">
      <a class="btn btn-success tic" id="0">#</a>
      <a class="btn btn-success tic" id="1">#</a>
      <a class="btn btn-success tic" id="2">#</a>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <a class="btn btn-success tic" id="3">#</a>
      <a class="btn btn-success tic" id="4">#</a>
      <a class="btn btn-success tic" id="5">#</a>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <a class="btn btn-success tic" id="6">#</a>
      <a class="btn btn-success tic" id="7">#</a>
      <a class="btn btn-success tic" id="8">#</a>
    </div>
  </div>
  
</div>
            
          
!
            
              body{
  background: #343436;
}
.col-md-12 .btn{
  width: 80px;
  height: 80px;
  font-size: 50px;
  border-radius: 50px;
}
.col-md-12:first-child {
  top: 20px;
}
.choose{
  color:white;
}
            
          
!
            
              $(document).ready(function() {

  function playermuta(muta, id) {
    //STORE THE VALUE OF THE ID  
    var punctLuat = $("#" + id).text();
    //IF SPOT IS NOT X OR O
    if (punctLuat === "#") {
      //KEEPS TRACK FOR WHILE LOOP FOR COMPUTER LOGIC
      count++;
      //CHANGES VALUE TO THE PLAYERS muta
      $("#" + id).text(muta);
      //ADDS VALUE TO THE ARRAY
      mutari[id] = muta;
      //WIN CODITION FUNCTION TAKES IN mutari ARRAY AND CURRENT X OR 0 muta
      winCondition(mutari, muta);
      //RUN THE COMPUTERS muta IF THE GAME DID NOT JUST END
      if (activate === false) {

        computermuta();
        winCondition(mutari, randComputer);
      }
    }
  }

  function winCondition(activeazaArray, randCurent) {
    if (activeazaArray[0] === randCurent && activeazaArray[1]===randCurent && activeazaArray[2] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (Top row across 0,1, and 2 spots)");
    } 
     else if (activeazaArray[2] === randCurent && activeazaArray[4]===randCurent && activeazaArray[6] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (Top row across 2,4, and 6 spots)");
    } 
    else if (activeazaArray[0] === randCurent && activeazaArray[3]===randCurent && activeazaArray[6] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (1st row down 0,3, and 6 spots)");
      }
    else if (activeazaArray[0] === randCurent && activeazaArray[4]===randCurent && activeazaArray[8] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (1st row diagonally across 0,4, and 8 spots)");
    } 
    else if (activeazaArray[1] === randCurent && activeazaArray[4]===randCurent && activeazaArray[7] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (2nd row down 1,4, and 7 spots)");
    } 
    else if (activeazaArray[2] === randCurent && activeazaArray[5]===randCurent && activeazaArray[8] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (3rd row down 2,5, and 8 spots)");
    } else if (activeazaArray[2] === randCurent && activeazaArray[5]===randCurent && activeazaArray[8] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (3rd row across 2,4, and 6 spots)");
    } else if (activeazaArray[3] === randCurent && activeazaArray[4]===randCurent && activeazaArray[5] === randCurent) {
      activate = true;
      reset();
      alert("Player " + randCurent + " wins! (Middle row across 3,4, and 5 spots)");
      
    } 
    else if(activeazaArray[6]===randCurent && activeazaArray[7]===randCurent && activeazaArray[8]===randCurent){
        activate = true;
      reset();
      alert("Player " + randCurent + " wins! (Bottom row across 6,7, and 8 spots)");
      
    }
    else {
      activate = false;
    }
  }

  //FUNCTION FOR COMPUTERS muta
  function computermuta() {
    //USED TO BREAK WHILE LOOP
    var taken = false;
    //COUNT USED TO BREAK INFINITE LOOP ON LAST muta
    while (taken === false && count !== 5) {
      //CHOOSE A RANDOM SPOT FOR THE COMPUTER TO MOVE TO
      var mutareComputer = (Math.random() * 10).toFixed();
      var move = $("#" + mutareComputer).text();
      if (move === '#') {
        $('#' + mutareComputer).text(randComputer);
        taken = true;
        mutari[mutareComputer] = randComputer;
      }
    }
  }

  $(".tic").click(function() {
    var slot = $(this).attr('id');
    playermuta(muta, slot);
  });

  $("#mutaX").click(function() {
    reset();
    muta = 'O';
    randComputer = 'X';
    $("#mutaX").removeClass("btn-primary");
    $("#mutaO").addClass("btn-primary");

  });
  $("#mutaO").click(function() {
    reset();
    muta = "X";
    randComputer = 'O'
    $("#mutaO").removeClass("btn-primary");
    $("#mutaX").addClass("btn-primary");
    $(".tic").text("#");
  });

  function reset() {
    mutari = ["#", "#", "#", "#", "#", "#", "#", "#", "#"];
    count = 0;
    $(".tic").text("#");
    activate= false;
  }
});
            
          
!
999px
Loading ..................

Console