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 class = 'container-fluid'>
  
<h2>Greta's tic tac toe game</h2>
  
  <!-- DISPLAY BUTTON -->
  
  <button class = 'hidden restart-btn'>Restart game</button>
  
  <!-- GAME RESULT DISPLAY -->
  
  <div class = 'hidden gameResult'>
  
  <!-- Displays who won-->
  
  <h3 class = "result "></h3> 
  
  <!-- Play again button -->
  
    <button class ="play-again-btn">Play again</button>
    
  </div>
  
<!-- -------------------------------------------------------------------------------------------- -->
      <!-- Player selects 1 or 2 player game -->
  
  <div class = 'playerSelection'> 
    
  <h6>Single Player or Two Player Game?</h6>
  
 <div class ='select'>
   
   <button class = 'player-btn' id = 'oneplayer'>Single Player</button>
   <button class = 'player-btn' id = 'twoplayer'>Two Player</button>
   
   </div> 
    
  </div> 
  
 <!-- -------------------------------------------------------------------------------------------- -->
   <!-- Player 1 selects icon -->
  
  <div class = 'iconSelection hidden'> 
    
  <h6>Player 1 : Would you like X or O ?</h6>
  
 <center><div class ='select'>
   
   <button class = 'icon-btn' id = 'x'>x</button>
   <button class = 'icon-btn' id = 'o'>o</button>
   
   </div></center>
    
  </div>
  
  <!-- -------------------------------------------------------------------------------------------- -->
  
  <!-- The game board -->
  
  <div class = "gamePlay hidden">
    
  <div class = "board">
    
    <table>
      
      <tr> <!-- FRIST ROW -->
        <td class = "square t l"><button id = '1' class = 'game-btn'></button></td>
        <td class = "square v t"><button id = '2' class = 'game-btn'></button></td>
        <td class = "square t r"><button id = '3' class = 'game-btn'></button></td>
      </tr>
      
      <tr> <!-- SECOND ROW -->
        <td class = "square h l"><button id = '4' class = 'game-btn'></button></td>
        <td class = "square v h"><button id = '5' class = 'game-btn'></button></td>
        <td class = "square h r"><button id = '6' class = 'game-btn'></button></td>
      </tr>
      
      <tr> <!-- THIRD ROW -->
        <td class = "square l b"><button id = '7' class = 'game-btn'></button></td>
        <td class = "square v b"><button id = '8' class = 'game-btn'></button></td>
        <td class = "square r b"><button id = '9' class = 'game-btn'></button></td>
      </tr>
      
    </table>
    
    </div>
    
  </div>
  <!-- -------------------------------------------------------------------------------------------- -->
  
</div>
              
            
!

CSS

              
                body{
  background-color : black; 
  margin-top :  1.1em;
  margin-bottom : 15px;
  color : white;
  font-family : Boogaloo;
  text-align : center;
}

h2{
  margin-bottom : 40px;
  font-size : 6em;
}

h3{
  font-size : 3.5em;
  display : inline;
}

h6{
  font-size : 3.5em;
  margin-top : 100px;
  margin-bottom : 50px;
}

.square{
    width:100px; 
    height:100px;
}

.t{
  border-top: 10px solid white;
}

.l{
  border-left: 10px solid white;
}

.r{
  border-right:10px solid white;
}

.b{
  border-bottom: 10px solid white;
}

.v{
    border-left:3px solid white;
    border-right:3px solid white;
}

.h{
    border-top:3px solid white;
    border-bottom:3px solid white;
}

.icon-btn{
  font-size : 70px;
  line-height : 30px;
}

.game-btn{
  width : 100px;
  height : 100px;
  border : none;
  margin : 0px;
  background-color : black;
  font-family : Architects Daughter;
  font-size : 90px;
  line-height : 100px;
  display:block;
}

.player-btn, .icon-btn{
  width : 130px;
  height : 60px;
}

.player-btn, .play-again-btn{
  font-size : 23px;
}

.play-again-btn{
  width : 100px;
  height : 60px;
  line-height : 20px;
  margin-left : 17px;
}

.restart-btn{
  width : 200px;
  height : 35px;
  line-height : 12px;
  font-size : 30px;
}

.player-btn, .icon-btn, .play-again-btn, .restart-btn{
  background : none;
  border-radius : 10%;
  border-style : dashed;
  border-color : white;
}

#twoplayer, #o{
  margin-left: 50px; 
}

button:focus {
  outline : 0;
}

.gameResult, .restart-btn{
  margin-bottom : 15px;
}

 .board > table{
  margin: 0 auto;
}





              
            
!

JS

              
                var winningPos = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]], xturn = true, player1, player2, singlePlayer, x, turns = 0, onHold = false, gameStatus = true, successProbability;
/*----------------------------------------------------------------------------------------------------------*/

function checkForWin(){  //Checks every possible winning combination from winning pos array
  
  for(var i in winningPos){
    
     // if somebody won
    
    if($('#'+winningPos[i][0]).html() == $('#'+winningPos[i][1]).html() && $('#'+winningPos[i][2]).html() == $('#'+winningPos[i][0]).html() && 
       $('#'+winningPos[i][1]).html()== $('#'+winningPos[i][2]).html() && $('#'+winningPos[i][2]).html()!= '') {
      
      //turn the winning combo another color
      
      $('#' + winningPos[i][0] + ', #' + winningPos[i][1] + ', #' + winningPos[i][2]).css('background-color' , '#4d4d33'); 
      
      //display the winner
      
      if(singlePlayer && $('#' + winningPos[i][0]).html() == player1) $('.result').html('You won!');
      else if(singlePlayer) $('.result').html('Computer won!');
      else if($('#' + winningPos[i][0]).html() == player1) $('.result').html('Player1 won!');
      else $('.result').html('Player2 won!');
      
      
      $('.restart-btn').addClass('hidden');
      $('.gameResult').removeClass('hidden');
      
      //return winning icon (x or o)
      return $('#' + winningPos[i][1]).html();
    }
        
  }
  //if no one won, return false
  return false;
}

/*----------------*/

//Function that determines whether a move will be made by AI

function isMoveAllowed(chanceOfMove){
  return (Math.floor(Math.random()*100 + 1) <= chanceOfMove || turns == 8) ? true : false;
}

//AI ----------------------------------------------------

function computer(){
  
  // if possible, win
  var el1, el2, el3, sign = player1 == 'x' ? 'o' : 'x';
  
  for(var i in winningPos){
    
    el1 = $('#'+winningPos[i][0]);
    el2 = $('#'+winningPos[i][1]);
    el3 = $('#'+winningPos[i][2]);
    
    //successProbability = Math.random()*10;
    
  if(el1.html() == sign && el2.html() == sign && el3.html()=='' && isMoveAllowed(80)) return el3;
  if(el3.html() == sign && el2.html() == sign  && el1.html()=='' && isMoveAllowed(80)) return el1;
  if(el1.html() == sign && el3.html() == sign  && el2.html()=='' && isMoveAllowed(80)) return el2;
    
  }
  
  //block
  
  for(var i in winningPos){
    
    el1 = $('#'+winningPos[i][0]);
    el2 = $('#'+winningPos[i][1]);
    el3 = $('#'+winningPos[i][2]);
    
  if(el1.html() == player1 && el2.html() == player1 && el3.html()=='' && isMoveAllowed(50)) return el3;
  if(el3.html() == player1 && el2.html() == player1  && el1.html()=='' && isMoveAllowed(50)) return el1;
  if(el1.html() == player1 && el3.html() == player1  && el2.html()=='' && isMoveAllowed(50)) return el2;    
    
}
  
  //fork
  
  if($('#1').html()==sign && $('#9').html()=='' && isMoveAllowed(65)) return $('#9');
  if($('#3').html()==sign && $('#7').html()=='' && isMoveAllowed(65)) return $('#7');
  if($('#9').html()==sign && $('#1').html()=='' && isMoveAllowed(65)) return $('#1');
  if($('#7').html()==sign && $('#3').html()=='' && isMoveAllowed(65)) return $('#3');
  
  //block opponents fork
  
  if((($('#1').html()==player1 && $('#9').html()==player1) ||  
     ($('#3').html()==player1 && $('#7').html()==player1) || 
     ($('#1').html()==player1 && $('#9').html()==player1) || 
     ($('#1').html()==player1 && $('#9').html()==player1)) && isMoveAllowed(65)) {
    
    for(var i = 2; i <= 8; i+=2)  if($('#'+i).html()=='') return $('#'+i);
    
  }
 
  //opposite corner
  
  if($('#1').html()==player1 && $('#9').html()=='' && isMoveAllowed(65)) return $('#9');
  if($('#3').html()==player1 && $('#7').html()=='' && isMoveAllowed(65)) return $('#7');
  if($('#7').html()==player1 && $('#3').html()=='' && isMoveAllowed(65)) return $('#3');
  if($('#9').html()==player1 && $('#1').html()=='' && isMoveAllowed(65)) return $('#1');

//empty corner
  
  
  if($('#9').html()=='' && isMoveAllowed(30)) return $('#9');
  if($('#7').html()=='' && isMoveAllowed(40)) return $('#7');
  if($('#3').html()=='' && isMoveAllowed(50)) return $('#3');
  if($('#1').html()=='' && isMoveAllowed(70)) return $('#1');
  
   //center
  
  if($('#5').html()=='' && isMoveAllowed(80)) return $('#5');
  
  //empty side
  
  if($('#2').html()=='') return $('#2');
  if($('#4').html()=='') return $('#4');
  if($('#6').html()=='') return $('#6');
  if($('#8').html()=='') return $('#1');
  
}


function displayComputerMove(){
  
  computer().html(player1=='x'?'o':'x');
  turns ++;
  onHold = false;
  
  //IF WIN OR DRAW
  
  if(!checkForWin() && turns == 9) {
          
          $('.result').html('Draw')
          $('.restart-btn').addClass('hidden');
          $('.gameResult').removeClass('hidden');
         
        }
}


/*----------------------------------------------------------------------------------------------------------*/
$(document).ready(function(){
  
 /*--------------------------------------------------------------------------------------------------------*/
  
  $('.player-btn').on('click', function(){
    
    $('.playerSelection').addClass('hidden');
    if ($(this).html()=='Single Player') singlePlayer = true;
    else singlePlayer = false;
   $('.iconSelection').removeClass('hidden');
    
  })
  /*------------------------------------------------------------------------------------------------------*/
  
  $('.icon-btn').on('click', function(){
    
    $('.iconSelection').addClass('hidden');
    
    if ($(this).html()=='x') x = true;
    else x = false; 
    player1 = $(this).html();
    player2 = x ? 'o' : 'x';
    
   $('.gamePlay, .restart-btn').removeClass('hidden');
   //$('.restart-btn').removeClass('hidden');
    
    if(singlePlayer && player1 == 'o'){
      
      onHold = true;
      setTimeout(displayComputerMove, 500);
      
    }
    
  })
  
  /*-------------------------------------------------------------------------------------------------------*/
   
  $('.game-btn').on('click', function(){ /* THE MAIN GAME LOGIC */
     
    if($(this).html()=='' && !onHold && !checkForWin()){ 
      
   /*--------- IF SINGLE PLAYER -----------*/   
     if(singlePlayer){
       
        $(this).html(player1);
        turns ++;
       
       //IF DRAW or WIN
       
         if(!checkForWin() && turns == 9) {
           
           console.log('Hi mom')
          
          $('.restart-btn').addClass('hidden');
          $('.result').html('Draw')
          $('.gameResult').removeClass('hidden'); 
          
        }
        
        if(!checkForWin() && turns != 9){ //if somebody didn't win on the previous move
         
        onHold = true;
        setTimeout(displayComputerMove, 600); 
          
       } 
       
    }
      //--------IF TWO PLAYER GAME ----------
      
      else{
        
        if(xturn) $(this).html('x');
        else  $(this).html('o');
        xturn = !xturn;
        turns ++;

     
        //----- CHECKS FOR DRAW OR WIN ---------*/
         if(!checkForWin() && turns == 9) {
          
          $('.restart-btn').addClass('hidden');
          $('.result').html('Draw')
          $('.gameResult').removeClass('hidden');
          
        }
        
      }
      
    }
     
  })
  
  //the play again button ------------------------------
  
  $('.play-again-btn').on('click', function(){
    
    $('.gamePlay').addClass('hidden');
    
    $('.game-btn').html('');
    
    $('.game-btn').css('background-color' , 'black');
    
    $('.gameResult').addClass('hidden');
    
    $('.playerSelection').removeClass('hidden');
    
    //onHold = false; 
    gameStatus = true;
    turns = 0;
    
  });
  
  //RESTART BUTTON ------------------
  
  $('.restart-btn').on('click', function(){
    
    $('.game-btn').html('');
     
    gameStatus = true;
    turns = 0;
    
  });
   
})
              
            
!
999px

Console