cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="container">
  <h1>TicTacToe</h1>

<div class="menu">
  <div class="choose">Choose player</div>
  <div class="buttons">
    <button class="xButton chosen"onclick="xClicked()"><i class="fa fa-close"></i></button>
  <button class="oButton"onclick="oClicked()"><i class="fa fa-circle-o" aria-hidden="true"></i></button>
    </div>
  <div class="computer"><button onclick="showGrid('computer')">Play with computer</button> <br>
    <button onclick="showGrid('player')">Play with human</button>
  </div>
  
</div>

  
  <div class='new newDisabled'>
    <div class="buttonsGrid">
      <div>
      <button name="1"></button>
      <button name="2"></button>
      <button name="3"></button>
      </div>
    <div>
      <button name="4"></button>
      <button name="5"></button>
      <button name="6"></button>
      </div>
    <div>
      <button name="7"></button>
      <button name="8"></button>
      <button name="9"></button>
    </div>
      
    </div>
      <button class="newGameButton"onclick="newGame()">NEW GAME</button>
    </div>
  
</div>



            
          
!
            
              body {
  background-color: #c3e2db;
  font-family: 'Lato', sans-serif;
}
.container {
  text-align: center;
  width: 500px;
  margin: 0 auto;
  height: 350px;
  border-radius: 10px;
  display: inline;
}
h1 {
  
  padding-top: 15px;
}
.choose {
  letter-spacing: 1px;
  font-size: 20px;
}


.buttons button {
  position: relative;
  margin: 20px 5px;
  
  height: 200px;
  width: 200px;
  border-radius: 20px;
  border: none;
  font-size: 100px;
  background-color: #a8c6f7;
  outline: none;
  box-shadow: 2px 6px #85a2d1;
  cursor: pointer;
  
}

.buttons button:hover{
  box-shadow: 1px 4px #85a2d1;
  top: 2px;
  left: 1px;
}

.chosen{
  box-shadow: 0px 0px #85a2d1 !important;
  top: 6px !important;
  left: 3px !important;
}

.computer button{
  margin-bottom: 10px;
  background: none;
  border: 1px black solid;
  letter-spacing: 1px;
  outline: none;
  cursor: pointer;
  width: 100px;
  transition-duration: 0.1s;
  
}

.computer button:hover {
  opacity: 0.7;
}

.computer button:active {
  background-color: #2f2f2f;
  color: white;
}

.new button {
  width: 100px;
  height:100px;
  border-radius: 5px;
  vertical-align: top;
  outline: none;
}

.newDisabled {
  display: none;
}

.newGameButton{
  margin-top: 20px;
  width: 100px !important;
  height: 30px !important;
  
}
            
          
!
            
              var x = '<i class="fa fa-close fa-5x"></i>';
var o = '<i class="fa fa-circle-o fa-4x"></i>';
var menu = document.querySelector(".menu"),
    grid = document.querySelector(".new"),
    buttonsGrid = document.querySelector(".buttonsGrid"),
    header = document.querySelector("h1");
var xORo = "x",
    bufor = "x",
    state = false,
    flag = false,
    counter = 1;
const arrOfSquares = [1,2,3,4,5,6,7,8,9];
var obj = {},
    usedSquares = [];

buttonsGrid.addEventListener("click", function(val){
  click(val.srcElement.name);
})

// MENU TO CHOOSE FROM X OR Y
function xClicked(){
  // showGrid();
  xORo = "x";
  bufor = "x";
  state = false;
  document.querySelector(".xButton").classList.add("chosen");
  document.querySelector(".oButton").classList.remove("chosen");
}

function oClicked(){
  // showGrid();
  xORo = "o";
  bufor = "o";
  state = true;
  document.querySelector(".oButton").classList.add("chosen");
  document.querySelector(".xButton").classList.remove("chosen");
}
// END MENU






function showGrid(val){
  clearButtons();
  grid.classList.toggle('newDisabled');
  menu.classList.toggle('newDisabled');
  if(val === "computer"){
    
    flag = true;
    if(bufor === "x" && counter === 1){
    compMove();
    }
  } else {
    flag = false;
  }
  
}

// if counter is uneven -> x was chosen

function compMove(){
  state = false;
 switch(counter){
case 1: //first 
  
      reversexORo();
    
      click(5); 
   
    break;
    
  
case 3:
    
              if(!obj[3]){
                click(3);
              } else {
                click(6);
              }
            
    
    break;
case 5:
       if(checkMove("o")){
      click(checkMove("o"));
    } else if(checkMove("x")){
      click(checkMove("x"));
    } else {
      console.log("random");
      click(random());
    }
           
    break;
case 7:
    
    if(checkMove("o")){
      console.log("check move o");
      click(checkMove("o"));
    } else if(checkMove("x")){
      console.log("check move x");
      click(checkMove("x"));
    } else {
      console.log("random");
      click(random());
    }
            
    break;
case 9:

          console.log("random");    
      click(random());
    
            
    break;
    case 2:
    
      
              if(!obj[5]){
                click(5);
              } else{
                click(3);
              }
       
    break;
    
case 4:
     
    if(checkMove("o")){
      click(checkMove("o"));
    } else{
      click(random());
    }
    // checkMove("o");
    
            
    break;
    
case 6:
   
              if(checkMove("x")){
      click(checkMove("x"));
    } else if(checkMove("o")){
      click(checkMove("o"));
    } else {
      click(random());
    }
              
    break;
    
case 8:
   
        if(checkMove("x")){
      click(checkMove("x"));
    } else if(checkMove("o")){
      click(checkMove("o"));
    } else {
      click(random());
    }
            
    break;
    
    
}

  
}





// function to write x or y
function click(val){
 if(!obj[val]){
    console.log(counter, "flag", flag, "state", state);
      if(xORo === 'x') document.getElementsByName(val)[0].innerHTML = x;
      else  if(xORo === 'o') document.getElementsByName(val)[0].innerHTML = o;
    document.getElementsByName(val)[0].disabled = true;
    obj[val] = xORo;
    usedSquares.push(val);
    if(evaluate(obj, "x")){
      return true;
    }
    if(evaluate(obj, "o")){
      return true;
    }
    reversexORo();
  counter++;
  if(counter > 9){
    arrOfSquares.forEach(function(val){
        if(!document.getElementsByName(val)[0].disabled){
          document.getElementsByName(val)[0].disabled = true;
        }
        header.innerHTML = "DRAW!";
      })
  }

  if(flag === true && state === true){
    state = false;
    return compMove();
          }
  state = true;
 } else {
   compMove();
 }

  
}

function clearButtons(){
    arrOfSquares.forEach(function(val){
    document.getElementsByName(val)[0].removeAttribute('disabled');
      while ( document.getElementsByName(val)[0].firstChild)  document.getElementsByName(val)[0].removeChild( document.getElementsByName(val)[0].firstChild);
   });
}

function newGame(){
  
  clearButtons();
  resetxORo();
    showGrid();
  header.innerHTML = "freeCodeCamp TicTacToe";
   
}

function resetxORo(){
  if(bufor === 'x'){
    xORo = "x";
    
  } else{
    xORo =  "o";
  }
  obj = {};
  usedSquares = [];
  counter = 1;
if(flag === true){
  state = true;
}
}

function reversexORo(){
  if(xORo === 'x'){
    xORo = "o";
  } else{
    xORo =  "x";
  }
}

function evaluate(obj, xORo){
  var arr = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], [2,5,8], [3,6,9],[1,5,9],[3,5,7]];
  arr.forEach(function(val, index){
    var result = arr[index].every(function(val){
      return obj[val] === xORo;
    });
    if(result === true){
      console.log("wygrana", xORo);
      arrOfSquares.forEach(function(val){
        if(!document.getElementsByName(val)[0].disabled){
          document.getElementsByName(val)[0].disabled = true;
        }
        header.innerHTML = "The winner is: " + xORo;
      })
      return true;
    }
  });
}


function checkMove(val){
  if(obj[2] === val && obj[3] === val && !obj[1]){ 
    console.log("auto move", 1);
     return 1;
     } else if(obj[1] === val && obj[2] === val && !obj[3]){
       console.log("auto move", 3);
               return 3;
               } else if(obj[4] === val && obj[5] === val && !obj[6]){
                 console.log("auto move", 6);
                 return 6;
               }  else if(obj[5] === val && obj[6] === val && !obj[4]){
                 console.log("auto move", 4);
                  return 4;
               }   else if(obj[7] === val && obj[8] === val && !obj[9]){
                 console.log("auto move", 9);
                   return 9;
               }     else if(obj[8] === val && obj[9] === val && !obj[7]){
                 console.log("auto move", 7);
                   return 7;
               } else if(obj[1] === val && obj[5] === val && !obj[9]){
                 console.log("auto move", 9);
                   return 9;
               } else if(obj[5] === val && obj[9] === val && !obj[1]){
                 console.log("auto move", 1);
                   return 1;
               } else if(obj[3] === val && obj[5] === val && !obj[7]){
                 console.log("auto move", 7);
                   return 7;
               } else if(obj[5] === val && obj[7] === val && !obj[3]){
                 console.log("auto move", 3);
                   return 3;
               } else if(obj[1] === val && obj[4] === val && !obj[7]){
                 console.log("auto move", 7);
                   return 7;
               } else if(obj[4] === val && obj[7] === val && !obj[1]){
                 console.log("auto move", 1);
                   return 1;
               } else if(obj[2] === val && obj[5] === val && !obj[8]){
                 console.log("auto move", 8);
                   return 8;
               } else if(obj[5] === val && obj[8] === val && !obj[2]){
                 console.log("auto move", 2);
                   return 2;
               } else if(obj[3] === val && obj[6] === val && !obj[9]){
                 console.log("auto move", 9);
                   return 9;
               } else if(obj[6] === val && obj[9] === val && !obj[3]){
                 console.log("auto move", 3);
                   return 3;
               } else if(obj[1] === val && obj[3] === val && !obj[2]){
                 console.log("auto move", 2);
                   return 2;
               } else if(obj[4] === val && obj[6] === val && !obj[5]){
                 console.log("auto move", 5);
                   return 5;
               } else if(obj[7] === val && obj[9] === val && !obj[8]){
                 console.log("auto move", 8);
                   return 8;
               } else if(obj[1] === val && obj[7] === val && !obj[4]){
                 console.log("auto move", 4);
                   return 4;
               } else if(obj[2] === val && obj[5] === val && !obj[5]){
                 console.log("auto move", 5);
                   return 5;
               } else if(obj[3] === val && obj[6] === val && !obj[9]){
                 console.log("auto move", 9);
                   return 9;
               } else if(obj[1] === val && obj[9] === val && !obj[5]){
                 console.log("auto move", 5);
                   return 5;
               } else if(obj[3] === val && obj[7] === val && !obj[5]){
                 console.log("auto move", 5);
                   return 5;
               } 
  
                         return false;
}


function random(){
  while(true){
        
  var num = Math.floor(Math.random() * 9) + 1;
  if(usedSquares.indexOf(num) === -1){
    return num;
  }

        }
}
            
          
!
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