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 id="wrap">   
<div id="image"></div>

  <div id="top"> 
   
    <div id="dropdown"> <label for="unittype">&blacktriangledown;</label>
      <select>
        <option value="0">Easy</option>
        <option value="1">Medium</option>
        <option value="2" selected>Impossible</option>
        <option value="3">Play against a friend</option>
      </select>
    </div>
    
      <div id="cross"><span class="cn">x</span><span class="count" id="scoreX">-</span></div>
      <div id="nought"><span class="cn">o</span><span class="count" id="scoreO">-</span></div>    
    <div id="message">Start game</div>
  </div>
  
 <div id="center">
    <ul id="board">
      <li id="one"   value="0"></li>
      <li id="two"   value="1"></li>
      <li id="three" value="2"></li>
      <li id="four"  value="3"></li>
      <li id="five"  value="4"></li>
      <li id="six"   value="5"></li>
      <li id="seven" value="6"></li>
      <li id="eigth" value="7"></li>
      <li id="ninen" value="8"></li>
    </ul>
   
    <div class="result" id="crossWin"><span class="x">&#x02A2F;</span><br>WINNER!</div>

    <div class="result" id="noughtWin"><span class="o">&bigcirc;</span><br>WINNER!</div>

    <div class="result" id="tie"><span class="x">&#x02A2F;</span> <span class="o">&bigcirc;</span> <br>Draw!</div>
   
</div>
  
 <button id="bottom">
   RESTART GAME
 </button>
</div>  

<footer id="footer" > 
<a href="https://www.linkedin.com/in/vlad-moroshan-266229129?trk=nav_responsive_tab_profile" target="_blank"><em>Feedback</em></a>
</footer>


              
            
!

CSS

              
                #wrap {  
  margin: 10px auto 10px;  
  width: 800px;
  text-align: center;
  min-width: 400px;
  border: 1px solid #d3d3d3;
  font: 1em 'Varela Round', sans-serif; 
  box-shadow: 0 4px 4px rgba(0, 0, 0, 0.4);
}

#image {  
  height: 170px;
  background: url("https://41dmav17y2a239wj1k1kd0yt-wpengine.netdna-ssl.com/monitor/wp-content/uploads/sites/3/2016/11/tic-tac-toe-screenshot.png");
  background-repeat: no-repeat;
}

#top {
  height: 160px; 
  color: #747474;
  border-top: 1px solid #d3d3d3;
  background-color: #f8f8f8;
}

#dropdown {  
  text-align: left;
  margin: 20px;
}

select {  
  padding: 10px;  
  background: none;
  width: auto;
  font: 1.2em 'Raleway', sans-serif; 
  color: #747474; 
  font-size: 16px;
  border: none;
  height: 40px;
  appearance: none;  
  -webkit-appearance: none;
  white-space: nowrap;
  outline: none;
}

select option { 
  background: white; 
  font: 1em 'Varela Round', sans-serif; 
  color: black; 
  box-shadow: 20px 20px black;
  outline: none;
} 

option:checked, option:hover {
    background: #f8f8f8;
}

#cross, #nought {  
  display: inline-block;
  margin: -10px 4px 20px;
  width: 200px;
  height: 40px;
  background-color: white;
  border: 1px solid #f6f6f6;
  border-radius: 2px;
  box-shadow: 1px 3px 10px #e0e0e0;
  text-align: left;
  font: 35px 'Varela Round', sans-serif;
}

.cn {
  margin: 0px 130px 0px 20px;
  color: black;
}

.count {
  vertical-align: middle;
  color: #939393;
  font-size: 25px;
}

#message {
  color: #939393;
}

#center { 
  height: 410px; 
  background-color: #14bdac;
  vertical-align: middle;
  text-align: center; 
}

#board {
  display:block;
  width: 374px;
  margin: auto;
  padding: 20px;  
}

#board li {  
  list-style-type:none;
  margin: 0px;
  font: 105px 'Varela Round', sans-serif;
  color: white;
  width: 120px;
  height: 120px;
  float: left;  
}

#five {
  border: 7px solid #0da192;  
}

#four, #six{
  border-top: 7px solid #0da192;
  border-bottom: 7px solid #0da192;
}

#two, #eigth{
  border-left: 7px solid #0da192;
  border-right: 7px solid #0da192;
}

#crossWin, #noughtWin, #tie {
  display:none;
  width: 374px;
  margin: auto;
  padding-top: 0px; 
  font: 5em 'Varela Round', sans-serif;
  font-weight: bold;
  color: #545454; 
}

.x{
   font: 3em 'Varela Round', sans-serif; 
}

.o {
  font: 2.5em 'Varela Round', sans-serif;
  color: white;
}

#bottom {
  height: 85px;
  width: 100%;
  cursor:pointer;
  border: none;
  outline:none;
  font: 1.2em 'Raleway', sans-serif; 
  color: #14bdbc;  
  background-color:#f8f8f8; 
  box-shadow: 0 4px 2px -2px gray;
}

#footer {
  font-family: sans-serif;
  font-size: 12px;  
  height: 30px;   
  text-align: right;
}

a{
  color: #747474;
  text-decoration:none
}
              
            
!

JS

              
                var message = document.getElementById('message'),
    square = document.querySelectorAll("li"), //for set function
    scoreX = document.getElementById('scoreX'), //for keeping track of wins
    scoreO = document.getElementById('scoreO'),
    winX = 0,
    winO = 0;

var difficult = '2', //default on AI 
    state = [0,0,0,0,0,0,0,0,0],
    game = true,
    last = "x"; //for two players

var HUMAN = false,
    COMPUTER = true,
    HUMVAL = -10, //values for minimax algoritm
    COMVAL = 10;

var winMatrix= [[0,1,2],
                [3,4,5],
                [6,7,8],
                [0,3,6],
                [1,4,7],
                [2,5,8],
                [0,4,8],
                [2,4,6]];

function reset() { 
  message.innerHTML = "Start game"; 
  document.getElementById("crossWin").style.display = "none"; 
  document.getElementById("noughtWin").style.display = "none"; 
  document.getElementById("tie").style.display = "none"; 
  document.getElementById("board").style.display = "block"; 

    for(var i = 0; i <= 9; i++) {       
      square[i].innerHTML = "";
      state[i] = 0;
    }   

  game = true;
}

function set(index, player, id) { //make all changes on UI
    if(player !== true) {
        document.getElementById(id).style.color = '#545454'; //color X brown
        square[index].innerHTML = "&#x02A2F;"; //array of li tags, where set x
        document.getElementById("cross").style.borderBottom = "none"; // delete previous border
        document.getElementById("nought").style.borderBottom = "solid #14bdac"; //set O border
        message.innerHTML = "&#x02A2F; Turn"; //update text above board
        state[index] = HUMVAL; //this needs for AI, checkWin, checkFull
    }
    else {
      if(id) { //if two players
        document.getElementById(id).style.color = 'white';
      }
        square[index].innerHTML = "&bigcirc;"; // all the same, read above
        document.getElementById("nought").style.borderBottom = "none";
        document.getElementById("cross").style.borderBottom = "solid #14bdac";
        message.innerHTML = "&bigcirc; Turn";
        state[index] = COMVAL;
    }
     
   if(checkWin(state, player)) {    
      message.innerHTML = "Game Over";
      document.getElementById("board").style.display = "none"; //remove game grid
     
      if(player) { //pick right X or O winning div
        document.getElementById("noughtWin").style.display = "block";
        scoreO.innerHTML = winO + 1; 
      }
      else {
        document.getElementById("crossWin").style.display = "block";
        scoreX.innerHTML = winX + 1;    
      }      
      game = false;
    }

    if(checkFull(state)) { // check on draw
      message.innerHTML = "Game Over";
      document.getElementById("board").style.display = "none";
      document.getElementById("tie").style.display = "block";
    }  
}

function checkWin(board, player) {
  var value = player === HUMAN ? HUMVAL : COMVAL;

  for(var i = 0; i < 8; i++) {
    var win = true;
    
    for(var j = 0; j < 3; j++) {
      if(board[winMatrix[i][j]] !== value) {
        win = false; 
        break;
      }
    }

    if(win) {           
      return true;    
    }
  }
  return false;
}

function checkFull(board) {
  for(var i = 0; i < 9; i++) {
      if(board[i] === 0)
       return false;
      }    
  return true;
}

function blindMove(board) { // easy level
  var newMove = []; //array of empty boxes
    for(var i = 0; i < 9; i++) {
      if(board[i] === 0) 
      newMove.push(i)
    }  //just random move
  set(newMove[Math.floor(Math.random() * newMove.length)], COMPUTER);
}

function random() {
   return Math.floor(Math.random() * 11); //for novice move(medium);
}

function aiTurn(board, depth, player) { //for master move, unbeatable
  
  if(checkWin(board, !player))  
    return -10 + depth;
  
  if(checkFull(board)) 
    return 0;
  
  var value = player == HUMAN ? HUMVAL : COMVAL;
  
  var max = -Infinity;  
  var index = 0;
  
  for(var i = 0; i < 9; i++) {    
    
    if(board[i] === 0) {
        var newboard=board.slice();
        newboard[i]=value;
      
      var moveval= -aiTurn(newboard,depth + 1, !player);
      
      if(moveval > max) {
        max = moveval;
        index = i;
      }
    }
  }
  if(depth === 0)
   set(index, COMPUTER);

return max;
}

function turn() {  //for two players
   if(last === "x") last = "o";
   else last = "x";
 return last;
}

document.body.addEventListener("click", function(e) {  
   if (e.target.tagName === 'SELECT') {     
     difficult = e.target.value; 
   } 
  
  if (e.target.tagName === 'LI') {    
      if(e.target.innerHTML === "") { //if empty position
        if(game) { 
          setTimeout(function() {          
            set(e.target.value, HUMAN, e.target.id); //after 0.6sec set "X" or "O" on the board
            
            if(difficult == '0') {           
              blindMove(state);           
            }

            else if(difficult == '1') {
              if(random() < 5) aiTurn(state, 0, COMPUTER); // half of the time random move
              else blindMove(state); 
            }          

            else if(difficult == '2') {
              aiTurn(state, 0, COMPUTER);   
            }

            else if(difficult == '3') { 
              set(e.target.value, turn() === "o" ? HUMAN : COMPUTER, e.target.id);  //for two players 
            } 
          }, 600);                     
       }
    }
  } 
  
  if(e.target.id === "bottom") {
    reset();    
  }
  
}, false);
              
            
!
999px

Console