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.

            
              <div class="container">
      <div class="pickSymbol">
        <p>Player One pick <button class="xSymbol">X</button> or <button class="oSymbol">O</button></p>
        <p>If not selected X will be default</p>
      </div>
      <div class="board">
        <div class="cell" id = 1></div>
        <div class="cell" id = 2></div>
        <div class="cell" id = 3></div>
        <div class="cell" id = 4></div>
        <div class="cell" id = 5></div>
        <div class="cell" id = 6></div>
        <div class="cell" id = 7></div>
        <div class="cell" id = 8></div>
        <div class="cell" id = 9></div>
      </div>
    </div>
            
          
!
            
              .board{
  display:flex;
  flex-flow: row wrap;
  width:300px;
  height:300px;
}

.cell{
  width:100px;
  height:100px;
  border:1px solid black;
  box-sizing: border-box;
  font-size:2em;
  text-align:center;
  padding-top:.8em;
}

            
          
!
            
              const board = document.querySelector('.board');

//select player symbol
let xSymbol = document.querySelector('.xSymbol');
let oSymbol = document.querySelector('.oSymbol');
let symbolDiv = document.querySelector('.pickSymbol');

var p1 = {
  mySymbol : "X",
  myCells: []
}
var computer = {
  mySymbol : "O",
  myCells: []
}

//uses a regex to check if the players myCell array has the proper combination to win the game
var winningCombo = function check(array){
  let arr = array.join("");
  switch(true){
    case(/^(?=.*1)(?=.*2)(?=.*3)/.test(arr)):
      alert("Winner, Top Row!");
      clearBoard();
      break;
    case(/^(?=.*4)(?=.*5)(?=.*6)/.test(arr)):
      alert("Winner, Middle Row!");
      clearBoard();
      break;
    case(/^(?=.*7)(?=.*8)(?=.*9)/.test(arr)):
      alert("Winner, Bottom Row!");
      clearBoard();
      break;
    case(/^(?=.*1)(?=.*4)(?=.*7)/.test(arr)):
      alert("Winner, Left Column!");
      clearBoard();
      break;
    case(/^(?=.*2)(?=.*5)(?=.*8)/.test(arr)):
      alert("Winner, Middle Column");
      clearBoard();
      break;
    case(/^(?=.*3)(?=.*6)(?=.*9)/.test(arr)):
      alert("Winner, Right Column");
      clearBoard();
      break;
    case(/^(?=.*1)(?=.*5)(?=.*9)/.test(arr)):
      alert("Winner, Left Diagonal!");
      clearBoard();
      break;
    case(/^(?=.*3)(?=.*5)(?=.*7)/.test(arr)):
      alert("Winner, Right Diagonal!");
      clearBoard();
      break;
  }
};

xSymbol.addEventListener('click', function(){
  p1.mySymbol = "X";
  computer.mySymbol = "O";
  //Make div with symbol picking invisible
  symbolDiv.style.opacity= 0;
  symbolDiv.style.visibility="none";
});
oSymbol.addEventListener('click', function(){
  p1.mySymbol = "O";
  computer.mySymbol = "X";

  symbolDiv.style.opacity= 0;
  symbolDiv.style.visibility="none";
});



//holds all the possible square values
var boardContents = [1,2,3,4,5,6,7,8,9];
//Set up click listener to change cells to X or O depending on player
board.addEventListener('click',function(e){
  if(e.target.className === 'cell' && e.target.innerHTML === ""){
    //On p1 turn push cell id into myCells so winningCombo function can check for winning combination
    //then change player turn
    //cell is clicked and thats id is set to square
    let square = parseInt(e.target.id);
  //change the cell that was clicked's innerHTML to X
    e.target.innerHTML = p1.mySymbol;
    //take that id and push it into p1myCells
    p1.myCells.push(e.target.id);
    //take board contents and remove the index of the id that was selected
    boardContents.splice(boardContents.indexOf(square), 1);
    //check if p1 cells contain a winning ocmbo
    winningCombo(p1.myCells);
    //after 1 second
    var computerMove = setTimeout(function(){
      //pick a random number between 0 and the length of boardcontents
      var box =  Math.floor(Math.random() * boardContents.length);
      // change the html of the cell with the id that match the random number picked
        document.getElementById(boardContents[box]).innerHTML = computer.mySymbol;
        computer.myCells.push(boardContents[box]);
        winningCombo(computer.myCells);
        //then take away that number from board contents
        boardContents.splice(boardContents.indexOf(boardContents[box]), 1);
    }, 1000);
  }
})


//clears all cells innerHTML and refills boardContents
var clearBoard = function(){
  let nodes = board.childNodes;
  for(let i=0; i<nodes.length; i ++){
    nodes[i].innerHTML = "";
  }
  p1.myCells = [];
  computer.myCells =[];
  boardContents = [1,2,3,4,5,6,7,8,9];
}
var reset = document.querySelector('.restart');
reset.addEventListener('click', function(){
  symbolDiv.style.opacity = 1;
  symbolDiv.style.visibility= "visible";
  clearBoard();

})





//holds player moves in myCells that is then later checked by the winningCombo function


            
          
!
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