Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container  text-center">
  <div class="header">
    <h1>TIC-TAC-TOE</h1>
    <br><div class="footer text-center"><em>Written and coded by <a href=https://www.freecodecamp.com/ericsolomon21><strong>Eric Solomon II</strong></em></a></div>
  </div>
  <div>
    <a href="#" class="btn player btn-primary" id="1p">1 Player</a>
    <a href="#" class="btn player btn-secondary" id="2p">2 Player</a>
  </div>
  <div>
    <a href="#" class="btn menu btn-primary" id="easy">Easy</a>
    <a href="#" class="btn menu btn-secondary" id="hard">Hard</a>
    <a href="#" class="btn menu btn-secondary" id="futile">FUTILE</a>
  </div>
  <div>
    <a href="#" class="btn menu btn-primary" id="X">Play as <strong>X</strong></a>
    <a href="#" class="btn menu btn-secondary" id="O">Play as <strong>O</strong></a>
    <a href="#" class="btn menu btn-danger" id="reset">RESET</a>
  </div>
  <div class="board text-center">
    <a href="#" class="btn space" id="0"></a>
    <a href="#" class="btn space" id="1"></a>
    <a href="#" class="btn space" id="2"></a><br>
    <a href="#" class="btn space" id="3"></a>
    <a href="#" class="btn space" id="4"></a>
    <a href="#" class="btn space" id="5"></a><br>
    <a href="#" class="btn space" id="6"></a>
    <a href="#" class="btn space" id="7"></a>
    <a href="#" class="btn space" id="8"></a><br>
  </div>
  <!--<div>
    <h5 id = "total">Total Score: 0</h5>
    <h6 id = "sub1">Easy: Win-0 Tie-0 Loss-0</h6>
    <h6 id = "sub2">Hard: Win-0 Tie-0 Loss-0</h6>
    <h6 id = "sub3">Futile: Win-<em>Don't Kid Yourself</em> Tie-0 Loss-0</h6>
  </div>-->
</div>
              
            
!

CSS

              
                body{
  background:  #140354;
  color: white;
}
.header{
  font-weight: bold;
  margin: auto;
  padding: 10px;
  border-radius:15px;
  color: #0B0133;
  width: 400px;
  background: #EDC200;
  margin-top: 10px;  
}
h1{
  margin-bottom: -10px;
}
.footer{
  font-weight: normal;
}
.space{
  background: #EDC200;
  font-size: 5em;
  width: 125px;
  height: 125px;
  margin-left: -2px;
  margin-right: -3px;
  border-width: 10px;
  border-color: #140354;
  border-radius: 0px;
  padding-top: 0px;
}
.btn{
  color: navy; 
  text-align: center;  
}
.menu{
  margin-top:8px;
  margin-bottom:0px;
  font-color:white;
  border-type: none;  
  padding-top: 3px;
  padding-bottom: 4px;
  padding-right: 8px;  
  padding-left: 8px;
}
.player{
  margin-top:8px;
  margin-bottom:0px;
  font-color:white;
  border-type: none;   
  padding-top: 3px;
  padding-bottom: 4px;
  padding-right: 8px;  
  padding-left: 8px;
}
              
            
!

JS

              
                $(document).ready(function() {
  var playerTurn = 0;
  var symbol = ["X", "O"];
  var plays = ["0", "1", "2", "3", "4", "5", "6", "7", "8"];
  var gamePlay = "on";
  var computer = "on";
  var remain = [0, 1, 2, 3, 4, 5, 6, 7, 8];
  var turn = 0;
  var player = ["computer", "player"];
  var level = "easy";
  /*var totalScore = 0;
  var easyWin = 0;
  var easyTie = 0;
  var easyLoss = 0;
  var hardWin = 0;
  var hardTie = 0;
  var hardLoss = 0;
  var futileWin = "<em>Don't Kid Yourself</em>";
  var futileTie = 0;
  var futileLoss = 0;*/  
  $("#1p").click(function() {
    computer = "on";
    $("#easy, #hard, #futile").show();
    $("#1p").css("background-color", "#226ee8");
    $("#2p").css("background-color", "white");
  });
  $("#2p").click(function() {
    computer = "off";
    $("#easy, #hard, #futile").hide();
    $("#2p").css("background-color", "#226ee8");
    $("#1p").css("background-color", "white");
  });
  $("#easy").click(function() {
    level = "easy";
    $("#easy").css("background-color", "#226ee8");
    $("#hard").css("background-color", "white");
    $("#futile").css("background-color", "white");
  });
  $("#hard").click(function() {
    level = "hard";
    $("#easy").css("background-color", "white");
    $("#hard").css("background-color", "#226ee8");
    $("#futile").css("background-color", "white");
  });
  $("#futile").click(function() {
    level = "futile";
    $("#easy").css("background-color", "white");
    $("#hard").css("background-color", "white");
    $("#futile").css("background-color", "#226ee8");
  });
  $("#X").click(function() {
    playerTurn = 0;
    $("#X").css("background-color", "#226ee8");
    $("#O").css("background-color", "white");
  });
  $("#O").click(function() {
    playerTurn = 1;
    $("#O").css("background-color", "#226ee8");
    $("#X").css("background-color", "white");
  });
  $("#reset").click(function() {
    $(".space").html("");
    $("#1p, #2p, #X, #O, #easy, #hard, #futile").show();
    plays = ["0", "1", "2", "3", "4", "5", "6", "7", "8"];
    $(".space").css("background-color", "#EDC200");
    gamePlay = "on";
    remain = [0, 1, 2, 3, 4, 5, 6, 7, 8];
    var turn = 0;
  });
  var spaceClicked = function() {
    playerTurn++;
    if (playerTurn >= symbol.length) {
      playerTurn = 0;
    }
    $("#1p, #2p, #X, #O, #easy, #hard, #futile").hide();
    game();
    if (gamePlay === "off" || remain.length === 0) {
      setTimeout(function() {
        $("#reset").click()
      }, 2000);
    }
    if (computer !== "off" && player[turn] !== "player") {
      setTimeout(function() {
        computerPlay()
      }, 500);
      turn++;
      if (turn >= player.length) {
        turn = 0;
      }
    }

  }
  function reduce(arr) {
    var args = Array.prototype.slice.call(arguments);
    remain = arr.filter(function(element) {
      return args.indexOf(element) === -1;
    });
  }
  var computerPlay = function() {
    var pick = remain[Math.floor(Math.random() * remain.length)];  
    if (level === "futile"){
        if(remain.indexOf(4) !== -1){
          pick = 4;
        } else if(plays[0] === plays[8] && remain.indexOf(3) !== -1){
          pick = 3;
        } else if(plays[2] === plays[6] && remain.indexOf(3) !== -1){
          pick = 3;
        } else if(plays[3] === plays[4] && remain.indexOf(5) !== -1){
          pick = 5;
        } else if(plays[4] === plays[8] && remain.indexOf(6) !== -1){
          pick = 6;
        } else if(plays[5] === plays[6] && remain.indexOf(7) !== -1){
          pick = 7;
        } else if(plays[3] === plays[8] && remain.indexOf(7) !== -1){
          pick = 7;
        } else if(plays[5] === plays[0] && remain.indexOf(1) !== -1){
          pick = 1;
        } else if(plays[3] === plays[2] && remain.indexOf(1) !== -1){
          pick = 1;
        } else if(plays[7] === plays[2] && remain.indexOf(1) !== -8){
          pick = 8;
        } else if(plays[7] === plays[0] && remain.indexOf(1) !== -8){
          pick = 8;
        } else if(remain.indexOf(4) === -1 && remain.indexOf(0) !== -1){
          pick = 0;
        } 
      } 
    if (level === "hard" || level === "futile"){    
      if(plays[0] === plays[1] && remain.indexOf(2) !== -1){
        pick = 2;
      } else if(plays[3] === plays[4] && remain.indexOf(5) !== -1){
        pick = 5;
      } else if(plays[4] === plays[5] && remain.indexOf(3) !== -1){
        pick = 3;
      } else if(plays[1] === plays[2] && remain.indexOf(0) !== -1){
        pick = 0;
      } else if(plays[0] === plays[2] && remain.indexOf(1) !== -1){
        pick = 1;
      } else if(plays[3] === plays[5] && remain.indexOf(4) !== -1){
        pick = 4;
      } else if(plays[6] === plays[7] && remain.indexOf(8) !== -1){
        pick = 8;
      } else if(plays[7] === plays[8] && remain.indexOf(6) !== -1){
        pick = 6;
      } else if(plays[6] === plays[8] && remain.indexOf(7) !== -1){
        pick = 7;
      } else if(plays[0] === plays[3] && remain.indexOf(6) !== -1){
        pick = 6;
      } else if(plays[3] === plays[6] && remain.indexOf(0) !== -1){
        pick = 0;
      } else if(plays[4] === plays[7] && remain.indexOf(1) !== -1){
        pick = 1;
      } else if(plays[1] === plays[4] && remain.indexOf(7) !== -1){
        pick = 7;
      } else if(plays[0] === plays[6] && remain.indexOf(3) !== -1){
        pick = 3;
      } else if(plays[1] === plays[7] && remain.indexOf(4) !== -1){
        pick = 4;
      } else if(plays[2] === plays[5] && remain.indexOf(8) !== -1){
        pick = 8;
      } else if(plays[5] === plays[8] && remain.indexOf(2) !== -1){
        pick = 2;
      } else if(plays[2] === plays[8] && remain.indexOf(5) !== -1){
        pick = 5;
      } else if(plays[0] === plays[4] && remain.indexOf(8) !== -1){
        pick = 8;
      } else if(plays[4] === plays[8] && remain.indexOf(0) !== -1){
        pick = 0;
      } else if(plays[0] === plays[8] && remain.indexOf(4) !== -1){
        pick = 4;
      } else if(plays[2] === plays[4] && remain.indexOf(6) !== -1){
        pick = 6;
      } else if(plays[4] === plays[6] && remain.indexOf(2) !== -1){
        pick = 2;
      } else if(plays[2] === plays[6] && remain.indexOf(4) !== -1){
        pick = 4;
      } 
    }
    
    $("#" + pick).click();
    turn++;
    if (turn >= player.length) {
      turn = 0;
    }
  }
  var game = function() {
    if (plays[0] === plays[1] && plays[1] === plays[2]) {
      $("#0").css("background-color", "#226ee8");
      $("#1").css("background-color", "#226ee8");
      $("#2").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[3] === plays[4] && plays[4] === plays[5]) {
      $("#3").css("background-color", "#226ee8");
      $("#4").css("background-color", "#226ee8");
      $("#5").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[6] === plays[7] && plays[7] === plays[8]) {
      $("#6").css("background-color", "#226ee8");
      $("#7").css("background-color", "#226ee8");
      $("#8").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[0] === plays[3] && plays[3] === plays[6]) {
      $("#0").css("background-color", "#226ee8");
      $("#3").css("background-color", "#226ee8");
      $("#6").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[1] === plays[4] && plays[4] === plays[7]) {
      $("#1").css("background-color", "#226ee8");
      $("#4").css("background-color", "#226ee8");
      $("#7").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[2] === plays[5] && plays[5] === plays[8]) {
      $("#2").css("background-color", "#226ee8");
      $("#5").css("background-color", "#226ee8");
      $("#8").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[0] === plays[4] && plays[4] === plays[8]) {
      $("#0").css("background-color", "#226ee8");
      $("#4").css("background-color", "#226ee8");
      $("#8").css("background-color", "#226ee8");
      gamePlay = "off";
    }
    if (plays[2] === plays[4] && plays[4] === plays[6]) {
      $("#2").css("background-color", "#226ee8");
      $("#4").css("background-color", "#226ee8");
      $("#6").css("background-color", "#226ee8");
      gamePlay = "off";
    }
  }
  $("#0").click(function() {
    if ($("#0").html() === "" && gamePlay === "on") {
      plays[0] = symbol[playerTurn];
      $("#0").html(symbol[playerTurn]);
      reduce(remain, 0);
      spaceClicked();
    }
  });
  $("#1").click(function() {
    if ($("#1").html() === "" && gamePlay === "on") {
      plays[1] = symbol[playerTurn];
      $("#1").html(symbol[playerTurn]);
      reduce(remain, 1);
      spaceClicked();
    }
  });
  $("#2").click(function() {
    if ($("#2").html() === "" && gamePlay === "on") {
      plays[2] = symbol[playerTurn];
      $("#2").html(symbol[playerTurn]);
      reduce(remain, 2);
      spaceClicked();
    }
  });
  $("#3").click(function() {
    plays[3] = symbol[playerTurn];
    if ($("#3").html() === "" && gamePlay === "on") {
      $("#3").html(symbol[playerTurn]);
      reduce(remain, 3);
      spaceClicked();
    }
  });
  $("#4").click(function() {
    if ($("#4").html() === "" && gamePlay === "on") {
      plays[4] = symbol[playerTurn];
      $("#4").html(symbol[playerTurn]);
      reduce(remain, 4);
      spaceClicked();
    }
  });
  $("#5").click(function() {
    if ($("#5").html() === "" && gamePlay === "on") {
      plays[5] = symbol[playerTurn];
      $("#5").html(symbol[playerTurn]);
      reduce(remain, 5);
      spaceClicked();
    }
  });
  $("#6").click(function() {
    if ($("#6").html() === "" && gamePlay === "on") {
      plays[6] = symbol[playerTurn];
      $("#6").html(symbol[playerTurn]);
      reduce(remain, 6);
      spaceClicked();
    }
  });
  $("#7").click(function() {
    if ($("#7").html() === "" && gamePlay === "on") {
      plays[7] = symbol[playerTurn];
      $("#7").html(symbol[playerTurn]);
      reduce(remain, 7);
      spaceClicked();
    }
  });
  $("#8").click(function() {
    if ($("#8").html() === "" && gamePlay === "on") {
      plays[8] = symbol[playerTurn];
      $("#8").html(symbol[playerTurn]);
      reduce(remain, 8);
      spaceClicked();
    }
  });
});
              
            
!
999px

Console