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.

            
              <body>
  <h1> <span style=" color: red;">Tic</span>Tac<span style="color: blue; ">Toe</span></h1>

  <div id="alertBox">


  </div>
  <div id="chooseScreen"> Choose Symbol!
  </div>

  <div class="container">

    <div id="Choice">
      <button id="X" onclick="chooseX()">X</button>
      <p> <span style=" color: red;">Choose</span><span style="color: blue; ">Symbol</span> </p>
      <button id="O" onclick="chooseO()">O</button>
    </div>
    <div id="turn"></div>
    <div class="board">

      <div class="box" id="a"></div>
      <div class="box" id="b"></div>
      <div class="box" id="c"></div>
      <div class="box" id="d"></div>
      <div class="box" id="e"></div>
      <div class="box" id="f"></div>
      <div class="box" id="g"></div>
      <div class="box" id="h"></div>
      <div class="box" id="i"></div>


    </div>
    <input type="button" id="button" value="Restart!" onClick="window.location.href=window.location.href">


  </div>
</body>
            
          
!
            
              body {
  background-color: #000;
}

#alertBox {
  display: none;
  background: linear-gradient(120deg, red, blue 90%);
  text-align: center;
  font-size: 32px;
  text-decoration: underlined;
  font-weight: bold;
  opacity: 0.95;
  position: absolute;
  top: 30%;
  left: 50%;
  width: 400px;
  /* adjust as per your needs */
  height: 200px;
  /* adjust as per your needs */
  margin-left: -200px;
  /* negative half of width above */
  margin-top: -100px;
  background-color: grey;
  color: white;
  text-shadow: 2px 1px black;
  text-decoration: underline;
  border: 6px solid grey;
  border-radius: 12px;
}

.alertBoxShow {
  display: block !important;
}

#chooseScreen {
  opacity: .9;
  border-radius: 70px;
  text-shadow: 2px 1px black;
  text-decoration: underline;
  font-weight: bold;
  position: absolute;
  top: 30%;
  left: 50%;
  margin-left: -250px;
  /* negative half of width above */
  margin-top: -225px;
  height: 253px;
  width: 500px;
  font-size: 70px;
  text-align: center;
  color: white;
  background-color: black;
  display: block;
}

.hide {
  display: none !important;
}

#Ok {
  display: block;
  width: 60px;
  height: 50px;
  margin-left: 43%;
  margin-top: 20%;
  border: 1px solid black;
  border-radius: 2px;
  font-size: 20px;
  font-weight: bold;
  color: black !important;
}

.container {
  margin: auto;
  width: 650px;
  height: 600px;
  background-color: black;
  border: 1px solid grey;
  border-radius: 4px;
}

#choice {
  display: block;
  margin: auto;
  width: 500px;
  height: 100px;
  background-color: black;
}

#X {
  margin-top: 10px;
  margin-left: 23%;
  width: 90px;
  height: 60px;
  font-weight: bold;
  font-size: 23px;
  color: white;
  background-color: red;
  border: 2px solid white;
  border-radius: 4px;
}

#O {
  background-color: blue;
  border: 2px solid white;
  border-radius: 4px;
  margin-top: 10px;
  width: 90px;
  height: 60px;
  font-weight: bold;
  font-size: 23px;
  color: white;
}

h1 {
  border-radius: 3px;
  margin: auto;
  width: 195px;
  font-size: 40px;
  font-weight: bold;
  text-shadow: 3px 3px black;
  background-color: black;
  color: white;
  text-align: center;
  text-decoration: underline;
}

.board {
  height: 450px;
  width: 450px;
  margin: auto;
  background-color: white;
  border-radius: 3px;
}

.box {
  display: inline-block;
  height: 150px;
  width: 147px;
  border: 2px dashed grey;
  color: red;
  text-align: center;
  font-weight: bold;
  font-size: 100px;
  vertical-align: top;
  /*stops divs jumping on input */
}

#button {
  margin-left: 39%;
  margin-top: 10px;
  align: center;
  color: white;
  background-color: black;
  height: 60px;
  width: 140px;
  font-weight: bold;
  font-size: 24px;
  border: 4px solid white;
  border-radius: 4px;
}

.score {
  font-weight: bold;
  font-size: 19px;
  padding: 8px;
  color: black;
  text-align: center;
  width: 180px;
  height: 70px;
  margin: auto;
  background-color: grey;
  border: 2px solid black;
  border-radius: 4px;
}

p {
  display: inline;
  margin: auto;
  font-size: 20px;
  font-weight: bold;
  text-decoration: underline;
  color: white;
}

#playerScore {
  background-color: white;
  display: inline-block;
  border: 1px solid black;
  margin: auto;
}

#cpuScore {
  background-color: white;
  margin: auto;
  border: 1px solid black;
  display: inline-block;
}

.unclickable {
  pointer-events: none;
}
            
          
!
            
              var cpuAnswer = [];
var answer = [];
var userX = false;
var userO = false;
var boxesTaken = [];
var userText = "";
var user="a";
var choices = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
var win = 0;
var choice;



function alertDraw(){
  $("#alertBox").toggleClass("alertBoxShow").text("Draw Game!").append( '<button id="Ok" onclick="end()">OK!</button>');
};


function alertUWin(){
  $("#alertBox").toggleClass("alertBoxShow").text("You Win!").append('<button id="Ok" onclick="end()">OK!</button>');
};

function alertCpuWin(){
  $("#alertBox").toggleClass("alertBoxShow").text("CPU Win!").append( '<button id="Ok" onclick="end()">OK!</button>');
};


$(".box").click(function() { //box click text based on user val
  if(user=="a"){
    $(".box").toggleClass("unclickable")
  }
 else if (user=="X") {
    $(this).text(userText).css("color", "red");
    $(this).addClass("unclickable");
console.log(userX+user);
  }  else  {
    $(this).text(userText).css("color", "blue");
    $(this).addClass("unclickable");
console.log(userO +user);
  }  
});

function chooseX() {
  user = "X";
 userText = "X";
$("#chooseScreen").toggleClass("hide");
  userX = true;
  console.log(user, userX, userO)
};

function chooseO() {
  user = "O";
      userText = "O";
  $("#chooseScreen").toggleClass("hide");

  userO = true;
  console.log(user, userX, userO)
};

function newChoice() {
  choice = choices[Math.floor(Math.random() * choices.length)];

  return choice;
  console.log(choice + " cpu next choice");
}

$(document).ready(function() {

  if (userX == true) {
    userX = true;
  } else if (userO == true) {
    userO = true;
  }

  $(".box").on('click', function act() {
    var id = $(this).attr('id'); //id equals the id of the box clicked

    for (var i = 0; i <= choices.length; i++) {
      if (choices[i] === id) { //If the id of box is in available choice array
        $("#" + id).text(userText);

        removeChoice(id);
        console.log(id + " user move removed");
        boxesTaken.push(id);
        answer.push(id);


        setTimeout(youWin(), 200);
        if (choices.length == 0 && win < 1) {
alertDraw();        
        }
        setTimeout(cpu, 420);
      }
    };
  })
});



function removeChoice(v) {
  choices.splice(choices.indexOf(v), 1);
  newChoice();
};

function cpu() { //CPU move chosen from random choice var
  if(user!="a"){
    
  
  if (win < 1) {
    var cpuChoice = newChoice(); //Choose random position for cpu
    if (userX) {
      cpuTacticsX(); // Pre move check that changes choice if it can make a better move
      $("#" + cpuChoice + "").text("O").css("color", "blue");
      $("#" + cpuChoice + "").addClass("unclickable");

      boxesTaken.push($("#" + cpuChoice + "").attr("id"));
      console.log(boxesTaken + " boxesTakenarray");
      console.log(choice + " chosen by CPU");

      cpuAnswer.push($("#" + cpuChoice + "").attr("id"));
      setTimeout(cpuWin(), 200);

    } else {
      // cpuTacticsO(); // Pre move check that changes choice if it can make a better move
      $("#" + cpuChoice + "").text("X").css("color", "red");
      $("#" + cpuChoice + "").addClass("unclickable");

      boxesTaken.push($("#" + cpuChoice + "").attr("id"));
      console.log(boxesTaken + " boxesTakenarray");
      console.log(choice + " chosen by CPU");
      cpuAnswer.push($("#" + cpuChoice + "").attr("id"));
      setTimeout(cpuWin(), 200);

    }
    removeChoice(cpuChoice);
  }
}}

function cpuTacticsX() {

  if (answer.indexOf("a") >= 0 && answer.indexOf("c") >= 0 && answer.indexOf("b") <= 0 && cpuAnswer.indexOf("b") <= 0) {
    cpuChoice = "b";

  } else if (answer.indexOf("a") >= 0 && answer.indexOf("i") >= 0 && answer.indexOf("e") <= 0 && cpuAnswer.indexOf("e") <= 0) {
    cpuChoice = "e";
  } else if (answer.indexOf("g") >= 0 && answer.indexOf("i") >= 0 && answer.indexOf("e") <= 0 && cpuAnswer.indexOf("e") <= 0) {
    cpuChoice = "h";

  } else if (answer.indexOf("a") >= 0 && answer.indexOf("g") >= 0 && answer.indexOf("d") <= 0 && cpuAnswer.indexOf("d") <= 0) {
    cpuChoice = "d";

  } else if (answer.indexOf("b") >= 0 && answer.indexOf("h") >= 0 && answer.indexOf("e") <= 0 && cpuAnswer.indexOf("e") <= 0) {
    cpuChoice = "e";

  } else if (answer.indexOf("c") >= 0 && answer.indexOf("i") >= 0 && answer.indexOf("f") <= 0 && cpuAnswer.indexOf("f") <= 0) {
    cpuChoice = "f";

  } else if (answer.indexOf("c") >= 0 && answer.indexOf("g") >= 0 && answer.indexOf("e") <= 0 || cpuAnswer.indexOf("e") <= 0) {
    cpuChoice = "e";

  } else if (answer.indexOf("c") >= 0 && answer.indexOf("b") >= 0 && answer.indexOf("a") <= 0 && cpuAnswer.indexOf("a") <= 0) {
    cpuChoice = "a";

  } else if (answer.indexOf("e") >= 0 && answer.indexOf("f") >= 0 && answer.indexOf("d") <= 0 && cpuAnswer.indexOf("d") <= 0) {
    cpuChoice = "d";

  } else if (answer.indexOf("d") >= 0 && answer.indexOf("f") >= 0 && answer.indexOf("e") <= 0 && cpuAnswer.indexOf("e") <= 0) {
    cpuChoice = "e";

  } else if (answer.indexOf("h") >= 0 && answer.indexOf("i") >= 0 && answer.indexOf("g") <= 0 && cpuAnswer.indexOf("g") <= 0) {
    cpuChoice = "g";

  }
};

function cpuWin() {
  if (cpuAnswer.indexOf("a") >= 0 && cpuAnswer.indexOf("b") >= 0 && cpuAnswer.indexOf("c") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("d") >= 0 && cpuAnswer.indexOf("e") >= 0 && cpuAnswer.indexOf("f") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("g") >= 0 && cpuAnswer.indexOf("h") >= 0 && cpuAnswer.indexOf("i") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("a") >= 0 && cpuAnswer.indexOf("d") >= 0 && cpuAnswer.indexOf("g") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("b") >= 0 && cpuAnswer.indexOf("e") >= 0 && cpuAnswer.indexOf("h") >= 0) {
     alertCpuWin();

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("c") >= 0 && cpuAnswer.indexOf("f") >= 0 && cpuAnswer.indexOf("i") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("a") >= 0 && cpuAnswer.indexOf("e") >= 0 && cpuAnswer.indexOf("i") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (cpuAnswer.indexOf("c") >= 0 && cpuAnswer.indexOf("e") >= 0 && cpuAnswer.indexOf("g") >= 0) {
   alertCpuWin();
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else {
    return;
  }
};

function youWin() {
  if (answer.indexOf("a") >= 0 && answer.indexOf("b") >= 0 && answer.indexOf("c") >= 0) {
    alertUWin();
    win++;
    //  score++;
    //  $("#ps").text(score);
    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("d") >= 0 && answer.indexOf("e") >= 0 && answer.indexOf("f") >= 0) {
alertUWin();    win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("g") >= 0 && answer.indexOf("h") >= 0 && answer.indexOf("i") >= 0) {
alertUWin();    win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("a") >= 0 && answer.indexOf("d") >= 0 && answer.indexOf("g") >= 0) {
alertUWin();     win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("b") >= 0 && answer.indexOf("e") >= 0 && answer.indexOf("h") >= 0) {
    alertUWin(); 
    win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("c") >= 0 && answer.indexOf("f") >= 0 && answer.indexOf("i") >= 0) {
   alertUWin(); 
    win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("a") >= 0 && answer.indexOf("e") >= 0 && answer.indexOf("i") >= 0) {
   alertUWin(); 
    win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else if (answer.indexOf("c") >= 0 && answer.indexOf("e") >= 0 && answer.indexOf("g") >= 0) {
    alertUWin(); 
    win++;

    // setTimeout(function() {
    //   end()
    // }, 140);
  } else {
    return;
  }
};

function end() {

  $("#button").trigger("click")
};
            
          
!
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