Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

              
                <link href="https://fonts.googleapis.com/css?family=Droid+Serif" rel="stylesheet">
<body>
<div class="container text-center">
  <h1 class="font">Tic Tac Toe 
  <h2 class="font2">Please Select Your Piece<h2>
  <a class="btn btn-primary font2" id="turnX">I want ' O '</a>
  <a class="btn btn-secondary font2" id="turnO">I want ' X '</a>
    <h1 class="font2">Let's Play!</h1>
    <div class="row">
        <a class="btn jeet" id="0">#</a>
        <a class="btn jeet" id="1">#</a>
        <a class="btn jeet" id="2">#</a>
      
      </div>
        <div class="row">
        
        <a class="btn jeet" id="3">#</a>
        <a class="btn jeet" id="4">#</a>
        <a class="btn jeet" id="5">#</a>
          </div>
          
          <div class="row">
          
        <a class="btn jeet" id="6">#</a>
        <a class="btn jeet" id="7">#</a>
        <a class="btn jeet" id="8">#</a>
          </div>
        <h6>Computer is pretty dumb for now</h6>
        
              
            
!

CSS

              
                body{
background: #FFAFBD; /* fallback for old browsers */
background: -webkit-linear-gradient(to left, #FFAFBD , #ffc3a0); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to left, #FFAFBD , #ffc3a0); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
                
        color:white;
}
.container{
  background-color: darkslategray;
  margin: 20px auto;
	padding: 20px 20px 9px;
  width: 400px;
	height: auto;
  border-radius: 7px;
	box-shadow: 0px 4px gray, 0px 10px 15px rgba(0, 0, 0, 0.6);
}
.jeet{
  margin:4px;
  width:75px;
  height:75px;
  text-align:center;
  font-size:40px;
}
.font{
  font-family:pacifico;
  font-size:50px;
}
.font2{
  font-family: 'Droid Serif', serif;
}
.btn.jeet{
  background-color:#e74c3c;
  font-family:Raleway;
  color:white;
  cursor: pointer;
  border-radius: 10px;
	box-shadow: 0px 4px rgba(225, 45, 39, 0.4);
  transition: all 0.2s ease;

}
.btn-primary, .btn-secondary{
  color:white;
}


              
            
!

JS

              
                $(document).ready(function() {

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

        computerTurn();
        winCondition(turns, computersTurn);
      }
    }
  }

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

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

  $(".jeet").click(function() {
    var slot = $(this).attr('id');
    playerTurn(turn, slot);
  });

  $("#turnX").click(function() {
    reset();
    turn = 'O';
    computersTurn = 'X';
    $("#turnX").removeClass("btn-primary");
    $("#turnO").addClass("btn-primary");

  });
  $("#turnO").click(function() {
    reset();
    turn = "X";
    computersTurn = 'O'
    $("#turnO").removeClass("btn-primary");
    $("#turnX").addClass("btn-primary");
    $(".jeet").text("#");
  });

  function reset() {
    turns = ["#", "#", "#", "#", "#", "#", "#", "#", "#"];
    count = 0;
    $(".jeet").text("#");
    gameOn= false;
  }
});
              
            
!
999px

Console