css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Permanent+Marker" rel="stylesheet">
</head>
  
<body>
  Remaining spaces:<div id="array"></div>
  Human spaces:<div id="myArray"></div>
  Comp spaces:<div id="compArray"></div>
  <button onclick="reset()">reset</button>
  <div id="paper">
    <div class="lines"></div>
    <div class="vertLine"></div>
    <div id="text">You want to go first?
      <div id="yes">
        <button onmouseover="showYes()" onmouseout="hideYes()" onclick="yes()">_ Yes (X)</button>
      <div id="checkYes">&#10004;</div>
      </div> <!-- end yes -->
      <div id="no"><button onmouseover="showNo()" onmouseout="hideNo()" onclick="no()">_ No (O)</button>
      <div id="checkNo">&#10004;</div>
      </div> <!-- end no -->
    </div> <!-- end text -->
  </div> <!-- end paper -->
  <div id="grid"><div class="vline1"></div><div class="vline2"></div><div class="hline1"></div><div class="hline2"></div></div>
  
  <div id="squares">
        <div id="box1" onclick="humanMove(id)"></div>      
        <div id="box2" onclick="humanMove(id)"></div>
        <div id="box3" onclick="humanMove(id)"></div>
        <div id="box4" onclick="humanMove(id)"></div>      
        <div id="box5" onclick="humanMove(id)"></div>
        <div id="box6" onclick="humanMove(id)"></div>
        <div id="box7" onclick="humanMove(id)"></div>      
        <div id="box8" onclick="humanMove(id)"></div>
        <div id="box9" onclick="humanMove(id)"></div> 
    <div id="winner"></div>
  </div> <!-- end squares -->
  <div id="myTurn">My turn...</div>
  <div id="yourTurn">Your turn...</div>
</body>
</html>
            
          
!
            
              body {
  background-color: gray;
  font-family: 'Permanent Marker', cursive;
   }

#paper {
  width: 400px;
  height: 418px;
  position: absolute;
  left: 50%;
  top: 50%;
   transform: translate(-50%, -50%);
  box-shadow: 2px 2px 3px black;
  background: rgba(255,255,204,1);
}
.vertLine{
  position: absolute;
  height: 418px;
  background-color: red;
  opacity: 0.3;
  width: 2px;
  left: 70px;
  top: 0px;
}

.lines {
  position: absolute;
  margin-top: 40px;
  width: 400px;
  height: 340px;
  top: 000px;
  background-image: repeating-linear-gradient(#ffffcc 0px, #ffffcc 35px, steelblue 36px);
}

#text {
  position: absolute;
  width: 400px;
  font-size: 34px;
top: 120px;
  left: 50%;
transform: translate(-50%, 0) rotate(-8deg);
  text-align: center;

}


#checkNo, #checkYes {
  font-size: 54px;
  font-family: 'Permanent Marker', cursive;
  position: absolute;
  left: 100px;
  top: 90px;
  z-index: 1;
  color: black;
  }

#checkYes{
  top: 25px;
  left: 100px;
}

.shown {
  opacity: 1.0;
}

.hidden {
  opacity: 0.0;
}

button {
  background: none;
  border: 0px;
    font-size: 44px;
  font-family: 'Permanent Marker', cursive;
}

#grid {
  position: absolute;
  left: 50%;
  top: 50%;  
  z-index: -1;
  transform: translate(-150px, -110px) rotate(-6deg);
  opacity: 1.0;
}
.vline1, .vline2, .hline1, .hline2 {
  background-color: black;
  width: 10px;
  height: 300px;
  margin: 1px;
  position: absolute;
  border-radius: 5px;
 }

.vline1 {
  left: 100px;
}
.vline2 {
  left: 200px;
}
.hline1, .hline2{
  -webkit-transform: rotate(90deg);
  top: -50px;
  left: 150px;
}
.hline2{
  top:50px;
}

#squares{
  font-family: 'Permanent Marker', cursive;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-130px, -145px) rotate(-6deg);
  width: 300px; 
  height: 300px;
  z-index: -1;
}
#box1, #box2, #box3,
#box4, #box5, #box6,
#box7, #box8, #box9{
  height: 90px;
  width: 90px;
  position: absolute;
  font-size: 90px;
  text-align: center;
  z-index: 1;
}
#box2{
  position: absolute;
  left: 104px;
}
#box3{
  position: absolute;
  left: 204px;
}
#box4{
  position: absolute;
  top: 103px;
}
#box5{
  position: absolute;
  top: 103px;
  left: 104px;
}
#box6{
  position: absolute;
  top: 103px;
  left: 204px;
}
#box7{
  position: absolute;
  top: 203px;
  left: 2px;
}
#box8{
  position: absolute;
  top: 203px;
  left: 104px;
}
#box9{
  position: absolute;
  top: 203px;
  left: 204px;
}

#winner {
  position: relative;
  font-size: 60px;
  top: -60px;
  width: 350px;
}

#myTurn, #yourTurn{
  position: absolute;
  top: 50%;
  left: 50%;
  width: 200px;
  height: 60px;
  background-color: white;
  border: black solid 8px;
  border-radius: 20px;
  text-align: center;
  font-size: 36px;
  transform: translate(210px, -70px)  rotate(-10deg);
  transition: opacity 0.5s;
  z-index: 2;
  }

#yourTurn{
  transform: translate(-420px, -50px) rotate(-10deg);
}


            
          
!
            
              var compWon = 0, won = 0, human = 0, turn = 0;
var spaces = [1,2,3,4,5,6,7,8,9];
var mySpaces = [];
var compSpaces = [];

window.onload = function() {
    document.getElementById('checkYes').className = 'hidden';
    document.getElementById('checkNo').className = 'hidden';
    document.getElementById('myTurn').className = 'hidden';
    document.getElementById('yourTurn').className = 'hidden';
   };

function hideText(){
  document.getElementById('text').style.zIndex = -1;
}

function drawGrid(){      
  document.getElementById('grid').style.zIndex = 1;
  document.getElementById('squares').style.zIndex = 1;
  hideText();
}

function showYes(){
  document.getElementById('checkYes').className = 'shown';
}
function hideYes(){
  document.getElementById('checkYes').className = 'hidden';
}
function showNo(){
  document.getElementById('checkNo').className = 'shown';
}
function hideNo(){
  document.getElementById('checkNo').className = 'hidden';
}

function yes(){
  document.getElementById('yourTurn').className = 'shown';
  drawGrid();
  setTimeout(humanMove,500);
}

function no(){
  document.getElementById('myTurn').className = 'shown';
  drawGrid();
  setTimeout(compMove,1000);
}

function humanMove(){
  var move=0;
   if($(event.target).is('#box1')) {
      move = 1;   
      if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
          humanMove(); // do the human move over again
      else
          document.getElementById('box1').textContent="X";         
        }
     if($(event.target).is('#box2')) {
        move = 2; 
        if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box2').textContent="X";          
        }
     if($(event.target).is('#box3')) {
       move = 3;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box3').textContent="X";
     }
     if($(event.target).is('#box4')) {
       move = 4;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box4').textContent="X";
     }
     if($(event.target).is('#box5')) {
       move = 5;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box5').textContent="X";}
     if($(event.target).is('#box6')) {
       move = 6;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box6').textContent="X";
     }
     if($(event.target).is('#box7')) {
       move = 7;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box7').textContent="X";
     }
     if($(event.target).is('#box8')) {
       move = 8;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box8').textContent="X";
     }
     if($(event.target).is('#box9')) {
       move = 9;
       if (!isInArray(move,spaces)) // if that space is NOT in the available spaces array
            humanMove(); // do the human move over again
        else
            document.getElementById('box9').textContent="X";
     }
    
  mySpaces.push(move); // add the move to the human array
 document.getElementById('myArray').textContent=mySpaces;
  spaces.splice(spaces.indexOf(move),1); // remove from array of available spaces
  document.getElementById('array').textContent=spaces;
  checkWin();
  if (won==0){
    document.getElementById('yourTurn').className = 'hidden';
    document.getElementById('myTurn').className = 'shown';
    setTimeout(compMove,1000);}
  else
    ;
}

function checkWin(){
  if (isInArray(1, mySpaces) && isInArray(2, mySpaces) && isInArray(3,mySpaces))
      won=1; 
  else if (isInArray(1,mySpaces)&&isInArray(4,mySpaces) && isInArray(7,mySpaces))
    won = 1;
  else if (isInArray(3,mySpaces)&&isInArray(5,mySpaces) && isInArray(7,mySpaces))
    won = 1;
  else if (isInArray(1,mySpaces)&&isInArray(5,mySpaces) && isInArray(9,mySpaces))
    won = 1;
  else if (isInArray(4,mySpaces)&&isInArray(5,mySpaces) && isInArray(6,mySpaces))
    won = 1;
  else if (isInArray(7,mySpaces)&&isInArray(8,mySpaces) && isInArray(9,mySpaces))
    won = 1;
  else if (isInArray(2,mySpaces)&&isInArray(5,mySpaces) && isInArray(8,mySpaces))
    won = 1;
  else if (isInArray(3,mySpaces)&&isInArray(6,mySpaces) && isInArray(9,mySpaces))
    won = 1;
  else
    won = 0;
  if (won == 1){
      document.getElementById('yourTurn').className = 'hidden';
      document.getElementById('myTurn').className = 'hidden';
      document.getElementById('winner').textContent="You won :(";
      human = 1; // human won
      setTimeout(reset,2000);
  }
}
      
function isInArray(value, array){
    return array.indexOf(value) > -1;
  }

function compMove(){
  var ran = Math.floor(Math.random() * spaces.length); // get a random number from 0 to length of spaces array
  var move = spaces[ran]; // assigns the value at the array element
  switch (move){
    case 1: document.getElementById('box1').textContent='O';
      break;
    case 2:document.getElementById('box2').textContent='O';
      break;
      case 3: document.getElementById('box3').textContent='O';
      break;
    case 4:document.getElementById('box4').textContent='O';
      break;
      case 5: document.getElementById('box5').textContent='O';
      break;
    case 6: document.getElementById('box6').textContent='O';
      break;
      case 7: document.getElementById('box7').textContent='O';
      break;
    case 8:document.getElementById('box8').textContent='O';
      break;
      case 9: document.getElementById('box9').textContent='O';
      break;    
             }
    compSpaces.push(move);
    document.getElementById('compArray').textContent=compSpaces;
    checkCompWin();
    spaces.splice(spaces.indexOf(move),1);
    document.getElementById('array').textContent=spaces;
    document.getElementById('yourTurn').className = 'shown';
    document.getElementById('myTurn').className = 'hidden';
}

function checkCompWin(){
  if (isInArray(1, compSpaces) && isInArray(2, compSpaces) && isInArray(3,compSpaces))
    compWon = 1;
  else if (isInArray(1,compSpaces)&&isInArray(4,compSpaces) && isInArray(7,compSpaces))
    compWon = 1;
  else if (isInArray(1,compSpaces)&&isInArray(5,compSpaces) && isInArray(9,compSpaces))
    compWon = 1;
  else if (isInArray(4,compSpaces)&&isInArray(5,compSpaces) && isInArray(6,compSpaces))
    compWon = 1;
  else if (isInArray(7,compSpaces)&&isInArray(8,compSpaces) && isInArray(9,compSpaces))
    compWon = 1;
  else if (isInArray(2,compSpaces)&&isInArray(5,compSpaces) && isInArray(8,compSpaces))
    compWon = 1;
  else if (isInArray(3,compSpaces)&&isInArray(6,compSpaces) && isInArray(9,compSpaces))
    compWon = 1;
  else
    compWon = 0;
  if (compWon == 1){
      human = 0; // human lost
      document.getElementById('yourTurn').className = 'hidden';
      document.getElementById('myTurn').className = 'hidden';
      document.getElementById('winner').textContent="I won!";
      setTimeout(reset,2000);
  }
}

function reset(){
    document.getElementById('myTurn').className = 'hidden';
    document.getElementById('yourTurn').className = 'hidden';
  compWon = 0, won = 0, turn = 0;
spaces = [1,2,3,4,5,6,7,8,9];
mySpaces = [];
compSpaces = [];
document.getElementById('grid').style.zIndex = -1;
  document.getElementById('squares').style.zIndex = -1;
  document.getElementById('winner').textContent="";
  document.getElementById('box1').textContent='';
  document.getElementById('box2').textContent='';
  document.getElementById('box3').textContent='';
  document.getElementById('box4').textContent='';
  document.getElementById('box5').textContent='';
  document.getElementById('box6').textContent='';
  document.getElementById('box7').textContent='';
  document.getElementById('box8').textContent='';
  document.getElementById('box9').textContent='';
  document.getElementById('text').style.zIndex = 1;
  document.getElementById('myArray').textContent=mySpaces;
  document.getElementById('array').textContent=spaces;
  document.getElementById('compArray').textContent="";
  /*
  if (human == 0) // if the human lost, he / she gets to go first in the next game
{
      document.getElementById('yourTurn').className = 'shown';
      setTimeout(humanMove,500);
}
  else
  {
      document.getElementById('myTurn').className = 'shown';
      setTimeout(compMove,1000);
  } */
}
            
          
!
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.
Loading ..................

Console