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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <body>
<div class="box">
  <h1>You vs Computer</h1>
  
<div class="options">
  <h3>Choose</h3>
  <button>X</button>
  <button>O</button>
</div>
  
<div class="board">
  <div class="grid top left diagL" id="1">
  </div>
    <div class="grid top vert" id="2">
    </div>
    <div class="grid top right diagR" id="3">
    </div>
    <div class="grid mid left" id="4">
    </div>
    <div class="grid mid vert diagL diagR" id="5">
    </div>
    <div class="grid mid right" id="6">
    </div>
    <div class="grid bot left diagR" id="7">
    </div>
    <div class="grid bot vert" id="8">
    </div>
    <div class="grid bot right diagL" id="9">
    </div>
 </div>
  
<h2 id="victor"></h2>
</div>
</body>
            
          
!
            
              body {
  background: #cea6dd;
}

h1 {
  font-family: airal, tahoma;
}

h3 {
  margin: 0 0 10px 0;
}

.box {
  text-align: center;
  position: relative;
}

button {
  margin: 0 10px 0 10px;
  background: #e1b8ef;
  font-weight: bold;
  font-size: 100%;
}

.options {
  z-index: 1;
  width: 10%; 
  background: #a15cbc;
  color: #FFF;
  display: none;
  margin: 70px auto; 
  padding: 10px;
  border-radius: 25px;
  position: absolute;
  left: 0;
  right: 0;
}

.board {
  background:#e1b8ef;
  width: 306px;
  height: 306px;
  border: 1px solid #000;
   margin: 0 auto;
  z-index: 1;
  font-family: arial, tahoma;
}

.grid {
  background: #e1b8ef;
  border: 1px solid #000;
  width: 100px;
  height: 100px;
  float: left;
  text-align: center;
  line-height: 100px;
  font-size: 200%;
  font-weight: bold;
}

#victor {  
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #a15cbc;
  padding: 5px 35px;
  color: #fff;
}


            
          
!
            
              $(document).ready(function() {
  var user,
    computer,
    wait = false,
    one, two, three, four, five, six, seven, eight, nine;

  $(".options").fadeIn(2000);

  $("button").on("click", function() {

    $(".options").fadeOut(2000);

    user = $(this).html();

    if (user == "X") { //choose sides
      computer = "O";
    } else {
      $("#5").html("X");
      computer = "X";
    }

  });

  $(".grid").on("click", function() {

    var pick = $(this).html();

    getCurrent();

    if ($(".options").is(":hidden") &&
      pick !== "X" &&
      pick !== "O" &&
      wait == false) {

      $(this).html(user);
      wait = true;
      setTimeout(computerTurn, 600);
    }

  });

  function computerTurn() {
    getCurrent();
    checkForTie();
    var gameOver = cpuWinAttempt();

    if (!gameOver) {
      
      if (five !== "X" && five !== "O") {
        $("#5").html(computer);
      }

      // run loops to check through various blocks
      else if (two == user && three == user &&
        one !== "X" && one !== "O") {
        console.log(user);
        $("#1").html(computer);
      } else if (four == user && seven == user &&
        one !== "X" && one !== "O") {
        console.log("hi");
        $("#1").html(computer);
      } else if (five == user && nine == user &&
        one !== "X" && one !== "O") {
        $("#1").html(computer);
      }

      else if (one == user && three == user &&
        two !== "X" && two !== "O") {
        $("#2").html(computer);
      } else if (five == user && eight == user &&
        two !== "X" && two !== "O") {
        $("#2").html(computer);
      }

     else if (two == user && one == user &&
        three !== "X" && three !== "O") {
        $("#3").html(computer);
      } else if (six == user && nine == user &&
        three !== "X" && three !== "O") {
        $("#3").html(computer);
      } else if (five == user && seven == user &&
        three !== "X" && three !== "O") {
        $("#3").html(computer);
      }

     else if (five == user && six == user &&
        four !== "X" && four !== "O") {
        $("#4").html(computer);
      } else if (one == user && seven == user &&
        four !== "X" && four !== "O") {
        $("#4").html(computer);
      }

      else if (three == user && nine == user &&
        six !== "X" && six !== "O") {
        $("#6").html(computer);
      } else if (four == user && five == user &&
        six !== "X" && six !== "O") {
        $("#6").html(computer);
      }

      else if (eight == user && nine == user &&
        seven !== "X" && seven !== "O") {
        $("#7").html(computer);
      } else if (one == user && four == user &&
        seven !== "X" && seven !== "O") {
        $("#7").html(computer);
      } else if (five == user && three == user &&
        seven !== "X" && seven !== "O") {
        $("#7").html(computer);
      }

      else if (two == user && five == user &&
        eight !== "X" && eight !== "O") {
        $("#8").html(computer);
      } else if (nine == user && seven == user &&
        eight !== "X" && eight !== "O") {
        $("#8").html(computer);
      }
      
      else if (nine !== "X" && nine !== "O") {
        $("#9").html(computer);
      }

       else if (two == user && four == user && one !== user && one !== computer) {
        $("#1").html(computer);
      } else if (two == user && six == user && three !== user && three !== computer) {
        $("#3").html(computer);
      } else if (six == user && eight == user && nine !== user && nine !== computer) {
        $("#9").html(computer);
      } else if (eight == user && four == user && seven !== user && seven !== computer){
        $("#7").html(computer);
    }

      

     // Computer move on first open space
      else {
        var done = false,
            i = 1;
        do {

          if ($("#7").html() !== "X" &&
             $("#7").html() !== "O"){
             $("#7").html(computer);
             done = true;
          }
          
          if ($("#" + i).html() !== "X" &&
            $("#" + i).html() !== "O" && !done) {

            $("#" + i).html(computer);
            done = true;
          }

          i++;
        } while (done == false)
      }

      wait = false;

    } else {
      setTimeout(reset, 3000);
      gameOver = false;
      wait = false;
    }

    getCurrent();
    checkForTie();
  }

  function getCurrent() {
    one = $("#1").html(),
      two = $("#2").html(),
      three = $("#3").html(),
      four = $("#4").html(),
      five = $("#5").html(),
      six = $("#6").html(),
      seven = $("#7").html(),
      eight = $("#8").html(),
      nine = $("#9").html();
  }
//victor replaced by 

  function cpuWinAttempt() {

    // grid blocks
    if (three == computer && two == computer &&
      one !== user && one !== computer) {
      $("#1").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".top").css("background", "#efe681");
      return true;
    }
   
    else if (four == computer && seven == computer &&
      one !== user && one !== computer) {
      $("#1").html(computer);
     $("#victor").html("Computer Wins").show();
      $(".left").css("background", "#efe681");
      return true;
    }
   
    else if (five == computer && nine == computer &&
      one !== user && one !== computer) {
      $("#1").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".diagL").css("background", "#efe681");
      return true;
    }

    
    else if (three == computer && one == computer &&
      two !== user && two !== computer) {

      $("#2").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".top").css("background", "#efe681");
      return true;
    }
    
    else if (five == computer && eight == computer &&
      two !== user && two !== computer) {
      $("#2").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".vert").css("background", "#efe681");
      return true;
    }

    
    else if (one == computer && two == computer &&
      three !== user && three !== computer) {
      $("#3").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".top").css("background", "#efe681");
      return true;
    }
    
    else if (six == computer && nine == computer &&
      three !== user && three !== computer) {
      $("#3").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".right").css("background", "#efe681");
      return true;
    }
    
    else if (five == computer && seven == computer &&
      three !== user && three !== computer) {
      $("#3").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".diagR").css("background", "#efe681");
      return true;
    }

    
    else if (five == computer && six == computer &&
      four !== user && four !== computer) {
      $("#4").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".mid").css("background", "#efe681");
      return true;
    }
    
    else if (one == computer && seven == computer &&
      four !== user && four !== computer) {
      $("#4").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".left").css("background", "#efe681");
      return true;
    }

    
    else if (four == computer && five == computer &&
      six !== user && six !== computer) {
      $("#6").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".mid").css("background", "#efe681");
      return true;
    }
    
    else if (three == computer && nine == computer &&
      six !== user && six !== computer) {
      $("#6").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".right").css("background", "#efe681");
      return true;
    }

    
    else if (eight == computer && nine == computer &&
      seven !== user && seven !== computer) {
      $("#7").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".bot").css("background", "#efe681");
      return true;
    }
    
    else if (one == computer && four == computer &&
      seven !== user && seven !== computer) {
      $("#7").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".left").css("background", "#efe681");
      return true;
    }
    
    else if (three == computer && five == computer &&
      seven !== user && seven !== computer) {
      $("#7").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".diagR").css("background", "#efe681");
      return true;
    }

    
    else if (seven == computer && nine == computer &&
      eight !== user && eight !== computer) {
      $("#8").html(computer);
       $("#victor").html("Computer Wins").show();
      $(".bot").css("background", "#efe681");
      return true;
    }
    
    else if (two == computer && five == computer &&
      eight !== user && eight !== computer) {
      $("#8").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".vert").css("background", "#efe681");
      return true;
    }

    
    else if (one == computer && five == computer &&
            nine !== user && nine !== computer) {
      $("#9").html(computer);
      $("#victor").html("Computer Wins").show();
      $(".diagL").css("background", "#efe681");
      return true;
    }

  }
//reset game
  function reset() {
    $(".grid").html("");
    $(".grid").css("background", "#e4d5ea");
    $(".options").slideDown();
    $("#victor").hide();
  }
//check for tie
  function checkForTie() {
    if ((one == "X" || one == "O") &&
      (two == "X" || two == "O") &&
      (three == "X" || three == "O") &&
      (four == "X" || four == "O") &&
      (five == "X" || five == "O") &&
      (six == "X" || six == "O") &&
      (seven == "X" || seven == "O") &&
      (eight == "X" || eight == "O") &&
      (nine == "X" || nine == "O")
    ) {

      $("#victor").html("It's A Tie").show();

      setTimeout(reset, 3000);
      gameOver = false;
      wait = false;
    }
  }

});


            
          
!
999px
Loading ..................

Console