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

Auto Save

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

              
                <body>
  <div class = "middle">
    <div class = "mainContainer">
      
      <div class = "lTopContainer">
        <div class = "playerBar" id = "playerCross">
          <div class = "playerToken">&#10006;</div> 
          <div class = "scoreBoard" id = "cScore">-</div>
        </div>  
      </div>
      
      <div class = "rTopContainer">
        <div class = "playerBar" id = "playerNaught">
          <div class = "playerToken">&#9898;</div>
          <div class = "scoreBoard" id = "nScore">-</div>
        </div> 
      </div>
      
      <div class = "underTopContainer" id = "step">
      
      </div> 
      
      <div class = "middleContainer" id = "midContainer">
        <div class = "playingBoard" id = "playingBoard">
          <div class = "vLine" id = "line1"></div>
          <div class = "hLine" id = "line3"></div>
          <div class = "vLine vLine2" id = "line2"></div>
          <div class = "hLine hLine2" id = "line4"></div>
          <div class = "square" id = "sqr1"></div>  
          <div class = "square" id = "sqr2"></div> 
          <div class = "square" id = "sqr3"></div> 
          <div class = "square" id = "sqr4"></div>  
          <div class = "square" id = "sqr5"></div> 
          <div class = "square" id = "sqr6"></div>                   
          <div class = "square" id = "sqr7"></div>  
          <div class = "square" id = "sqr8"></div> 
          <div class = "square" id = "sqr9"></div>
          
          <div class = "bigCross"></div>
          <div class = "bigCross cross2"></div>
          <div class = "bigNaught"></div> 
          <div class = "medCross"></div>
          <div class = "medCross cross2"></div>
          <div class = "medNaught"></div>
          <div class = "endGame"></div>
          
          <signature>built by Hieu Lai</signature>
        </div>  
      </div>
      
      <div class = "bottomContainer" id = "restartBtn">
        <div class = "restartGameFont">RESTART GAME</div>
      </div>
    </div>
    
  </div>
<body>  
              
            
!

CSS

              
                body {
  display: table;
  position: absolute;
  height: 100%;
  width: 100%;
}

.middle {
  display: table-cell;
  vertical-align: middle;
}

.mainContainer {
  border: 0px solid #ededed;
  margin-left: auto;
  margin-right: auto;
  padding-top: 1px;
  width: 550px;
  height: 400px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.lTopContainer {
  border: 0px solid black;
  height: 80px;
  width: 275px;
  float: left;
  padding: 40px 0px 0 98px;
}

.rTopContainer {
  border: 0px solid black;
  height: 80px;
  width: 275px;
  float: right;
  padding: 40px 0px 0 4px;
}

.underTopContainer {
  border: 0px solid black;
  height: 40px;
  margin-top: 80px;
  text-align: center;
  line-height: 40px;
  font-size: 12px;
}

.middleContainer {
  height: 240px;
  background: #885ead;
  padding: 10px;
}

.bottomContainer {
  border: 0px solid blue;
  height: 40px;
  cursor: pointer;
  text-align: center;
  line-height: 40px;
}

.playerBar { 
  height: 40px;
  width: 170px;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19);
  padding-left: 17px;
  display: flex;
  align-items: center;
}

.playerToken {
  font-size: 20px;
}

.scoreBoard {
  margin-left: 100px;
  float: right;
  font-size: 20px;
}

.restartGameFont {
  font-weight: bold;
  color: #885ead;
}

.playingBoard {
  border: 0px solid black;
  width: 220px;
  height: 220px;
  margin: auto;
}

.square {
  border: 0px solid black;
  height: 73px;
  width: 73px;
  float: left;
  padding: 14px;
}

.vLine {
  border-left: 7px solid #551a8b;
  height: 0;
  position: absolute;
  margin-left: 70px;
  border-radius: 0 0 50% 50%;
  opacity: 0.5;  
}

.hLine {
  border-top: 7px solid #551a8b;
  width: 0px;
  position: absolute;
  margin-top: 70px;
  border-radius: 0 50% 50% 0%;
  opacity: 0.5;
}

.hLine2 {
  margin-top: 143px;  
}

.vLine2 {
  margin-left: 143px;
}

.naught {
  border: 7px solid #e5e3bb;
  border-radius: 50%;
  height: 45px;
  width: 45px;
  position: absolute;
  display: none;
}

.cross {
  border-left: 7px solid #273746;
  height: 45px;
  transform: rotate(45deg);
  margin: 0px 20px;
  position: absolute;
  display: none;
}

.endGame {
  border: 0px solid #273746;
  height: 90px;
  width: 220px;
  position: absolute;
  margin-top: 130px;
  font-size: 30px;
  text-align: center;
  font-weight: bold;
  color: #273746;
  display: none;
}

.bigCross {
  border-left: 11px solid #273746;
  height: 130px;
  transform: rotate(45deg);
  margin: 0px 104px;
  position: absolute;
  display: none;
}

.bigNaught {
  border: 14px solid #e5e3bb;
  border-radius: 50%;
  height: 110px;
  width: 110px;
  margin: 10px 55px;
  position: absolute;
  display: none;
}

.medCross {
  border-left: 9px solid #273746;
  height: 70px;
  transform: rotate(45deg);
  margin: 35px 69px;
  position: absolute; 
  display: none;
}

.cross2 {
  transform: rotate(135deg);
}

.medNaught {
  border: 9px solid #e5e3bb;
  border-radius: 50%;
  height: 70px;
  width: 70px;
  margin: 35px 112px;
  position: absolute;
  display: none;
}

signature {
  color: white;
  font-size: 10px;
  position: absolute;
  margin-top: 210px;
  margin-left: 90px;
}


              
            
!

JS

              
                $(document).ready(function() {
  var ticker;
  var scoreNaught = 0;
  var scoreCross = 0;
  var initialState = true;
  var initialBoard = [0,1,2,3,4,5,6,7,8];
  var huPlayer = "O";
  var aiPlayer = "X";
  var arrFunctions = [sq1Function, sq2Function, sq3Function,
                      sq4Function, sq5Function, sq6Function,
                      sq7Function, sq8Function, sq9Function];
  var nextPlayer;
  var count = 1;
  var count2 = 0;
  
  crossTurn(); 
  drawGameBoard();
  
  function drawGameBoard() {
    $("#step").html("Start game or select player"); 
    $(".vLine").css("height", "0");
    $(".hLine").css("width", "0");
    $("#midContainer").css("cursor", "");
    $("#sqr1").html("");
    $("#sqr2").html("");
    $("#sqr3").html("");
    $("#sqr4").html("");
    $("#sqr5").html("");
    $("#sqr6").html("");
    $("#sqr7").html("");
    $("#sqr8").html("");
    $("#sqr9").html("");
    ticker = 1;
    $("#line1").animate({height: '220px'}, "fast", function() {
      $("#line2").animate({height: '220px'}, "fast", function() {
        $("#line3").animate({width: '220px'}, "fast", function() {
          $("#line4").animate({width: '220px'}, "fast");
        });
      }); 
    });    
  } 
   
  function drawCross(position) {
    var id1 = position + "1";
    var id2 = position + "2";
    $("#"+position).html('<div class = "cross" id ='+id1+'></div><div class = "cross cross2" id = '+id2+'></div>');
    $("#"+id1).fadeIn("fast", function() {
      $("#"+id2).fadeIn("fast");
    });
    initialState = false;
    $("#playerNaught").off("click");
  }
  
  function drawNaught(position) {
    var id1 = position + "1";    
    $("#"+position).html('<div class = "naught" id = '+id1+'></div>');
    $("#"+id1).slideDown("normal");
  }
  
  function crossTurn() {
    ticker = 1;
    setTimeout(function() { 
    if(!initialState) {
      $("#step").html("&#10006; Turn");
    }  
    $("#playerCross").css('border-bottom', '3px solid #885ead');
    $("#playerCross").css('box-shadow', '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)');
    $("#playerNaught").css('border-bottom', '');
    $("#playerNaught").css('box-shadow', '0 1px 2px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19)');
    }, 400); 
    
  }
  
  function naughtTurn() {
    ticker = 2;
    setTimeout(function() {
    $("#step").html("&#9898; Turn");
    $("#playerNaught").css('border-bottom', '3px solid #885ead');
    $("#playerNaught").css('box-shadow', '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)');
    $("#playerCross").css('border-bottom', '');
    $("#playerCross").css('box-shadow', '0 1px 2px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19)');
    }, 400);  
  }
  
  function displayEndGame(player) {
    console.log("Winner");
    setTimeout(function() {
      $("#midContainer").css('cursor', 'pointer');
      $("#midContainer").on("click", restartGame);
      $("#step").html("Game Over");
      $("#sqr1").html("");
      $("#sqr2").html("");
      $("#sqr3").html("");
      $("#sqr4").html("");
      $("#sqr5").html("");
      $("#sqr6").html("");
      $("#sqr7").html("");
      $("#sqr8").html("");
      $("#sqr9").html("");
      $(".vLine").css("height", "0");
      $(".hLine").css("width", "0");
    }, 1000);
    setTimeout(function() {
      if(player === huPlayer){
        scoreNaught++;
        $(".bigNaught").slideDown("normal");
        $("#nScore").html(scoreNaught);
        $(".endGame").html("Winner winner, chicken dinner!");
      }
      else if(player === aiPlayer) {
        scoreCross++;
        $(".bigCross").slideDown("normal");
        $(".bigCross cross2").slideDown("normal");
        $("#cScore").html(scoreCross);
        $(".endGame").html("Winner winner, chicken dinner!");
      }
      else if(player === "draw") {
        $(".medCross").slideDown("normal");
        $(".medCross cross2").slideDown("normal");
        $(".medNaught").slideDown("normal");
        $(".endGame").html("Draw!");
      }
      $(".endGame").slideDown("normal");
    }, 1500) 
  }
 
  $("#playerNaught").on("click", naughtFunction);
  
  function naughtFunction() {
    $("#playerNaught").off("click");
    count++;
    initialState = false;
    if(count2 == 0) {
      var aiMove = minimax(initialBoard, aiPlayer).index;
      count2 = 1;
      return arrFunctions[aiMove]();
    }
  } 
  
//----------------------------------------------------------------------------  
  $("#sqr1").on("click", sq1Function); 
                             
  function sq1Function() {
    if(initialBoard[0] != 0) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr1");
      naughtTurn();
      initialBoard[0] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      }  
    } 
    else {
      drawNaught("sqr1");
      crossTurn();
      initialBoard[0] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      }
    }
    console.log(count);
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    }
    console.log("look");
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  
  } 
   
  $("#sqr2").on("click", sq2Function);
  
  function sq2Function() {    
    if(initialBoard[1] != 1) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr2");
      naughtTurn();
      initialBoard[1] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr2");
      crossTurn();
      initialBoard[1] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
    
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    }
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }  
  
  $("#sqr3").on("click", sq3Function);    
    
    function sq3Function() {
    if(initialBoard[2] != 2) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr3");
      naughtTurn();
      initialBoard[2] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr3");
      crossTurn();
      initialBoard[2] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    } 
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }  
  }
  
  $("#sqr4").on("click", sq4Function);    
    
    function sq4Function() {
    if(initialBoard[3] != 3) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr4");
      naughtTurn();
      initialBoard[3] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr4");
      crossTurn();
      initialBoard[3] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    } 
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }
  
  $("#sqr5").on("click", sq5Function);    
    
    function sq5Function() {
    if(initialBoard[4] != 4) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr5");
      naughtTurn();
      initialBoard[4] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr5");
      crossTurn();
      initialBoard[4] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    }
      
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }
  
  $("#sqr6").on("click", sq6Function);    
    
    function sq6Function() {
    if(initialBoard[5] != 5) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr6");
      naughtTurn();
      initialBoard[5] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr6");
      crossTurn();
      initialBoard[5] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    } 
      
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }
  
  $("#sqr7").on("click", sq7Function);    
    
    function sq7Function() {
    if(initialBoard[6] != 6) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr7");
      naughtTurn();
      initialBoard[6] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr7");
      crossTurn();
      initialBoard[6] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    } 
      
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }
  
  $("#sqr8").on("click", sq8Function);    
    
    function sq8Function() {
    if(initialBoard[7] != 7) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr8");
      naughtTurn();
      initialBoard[7] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr8");
      crossTurn();
      initialBoard[7] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    }
      
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }
  
  $("#sqr9").on("click", sq9Function);    
    
    function sq9Function() {
    if(initialBoard[8] != 8) {
      return;
    }
    if(ticker == 1) {
      drawCross("sqr9");
      naughtTurn();
      initialBoard[8] = aiPlayer;
      nextPlayer = huPlayer;
      if(checkWin(initialBoard, aiPlayer)) {
        displayEndGame(aiPlayer);
      } 
    } 
    else {
      drawNaught("sqr9");
      crossTurn();
      initialBoard[8] = huPlayer;
      nextPlayer = aiPlayer;
      if(checkWin(initialBoard, huPlayer)) {
        displayEndGame(huPlayer);
      } 
    }
     
    count++;
    if(count%2 === 0) {
      setTimeout(function() {
      var aiMove = minimax(initialBoard, nextPlayer).index;
      return arrFunctions[aiMove]();
      }, 500);
    } 
    if(emptySquares(initialBoard).length === 0) {
      displayEndGame("draw");
    }
  }
   
 //---------------------------------------------------------------------------- 
  $("#restartBtn").on("click", restartGame);
    
  function restartGame() {
    initialState = true;
    crossTurn();
    drawGameBoard(); 
    initialBoard = [0,1,2,3,4,5,6,7,8];
    count = 1;
    count2 = 0;
    $("#midContainer").off("click", restartGame);
    $(".bigNaught").fadeOut("normal");
    $(".bigCross").fadeOut("normal");
    $(".bigCross cross2").fadeOut("normal");
    $(".medCross").fadeOut("normal");
    $(".medCross cross2").fadeOut("normal");
    $(".medNaught").fadeOut("normal");
    $(".endGame").fadeOut("normal");
    $("#playerNaught").on("click", naughtFunction);
  }
  
//-----------------------------------------------------------------------------
//Credit to Ahmad Abdolsaheb in helping me figure the minimax algo . 
//Thanks buddy.
  function checkWin(board, player) {
    if(board[0] == player && board[1] == player && board[2] == player ||
       board[3] == player && board[4] == player && board[5] == player || 
       board[6] == player && board[7] == player && board[8] == player ||
       board[0] == player && board[3] == player && board[6] == player ||
       board[1] == player && board[4] == player && board[7] == player ||
       board[2] == player && board[5] == player && board[8] == player ||
       board[0] == player && board[4] == player && board[8] == player ||
       board[2] == player && board[4] == player && board[6] == player) {
      return true;
    }
    return false;         
  }
  
  function emptySquares(board) {
    return board.filter(s => s != huPlayer && s != aiPlayer);
  }
  
  function minimax(newBoard, player) {
    var openSquare = emptySquares(newBoard);
    
    if(checkWin(newBoard, aiPlayer)) {
      return {score:10};
    }
    else if(checkWin(newBoard, huPlayer)) {
      return {score:-10};
    }
    else if(openSquare.length === 0) {
      return {score:0};
    }
    
    var moves = [];
    for(var i = 0; i < openSquare.length; i++) {
      var move = {};
      move.index = newBoard[openSquare[i]]; 
      newBoard[openSquare[i]] = player;
      
      if(player === aiPlayer) {
        var result =  minimax(newBoard, huPlayer);
        move.score = result.score;
      }
      else {
        var result =  minimax(newBoard, aiPlayer);
        move.score = result.score;
      }
    
      newBoard[openSquare[i]] = move.index;
      moves.push(move);
    }
    
    var bestMove;
    if(player === aiPlayer) {
      var bestScore = -10000;
      for(var i = 0; i < moves.length; i++) {
        if(moves[i].score > bestScore) {
          bestScore = moves[i].score;
          bestMove = i;
        }
      }
    } else {
      var bestScore = 10000;
      for(var i = 0; i < moves.length; i++) {
        if(moves[i].score < bestScore) {
          bestScore = moves[i].score;
          bestMove = i;
        }
      }
    }
    
    return moves[bestMove];
  }   
  
});
              
            
!
999px

Console