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

              
                <html>
 <head>
<title>Tic-Tac-Toe Game
 </title>
</head>
<body>
 <div class="spacer"><div class="container">
<div class="row text-center" id="intro-section">
  <h1>Player vs Computer Tic-Tac-Toe</h1>
  <h2 class="message" id="message">Choose your player:</h2>
  <form action="#">
  <fieldset>
    <input type="radio" name="player-choice" id="player-choice-1" value="X" />
    <label for="player-choice-1">X</label>
    <input type="radio" name="player-choice" id="player-choice-2" value="O" />
    <label for="player-choice-2">O</label>
  </fieldset>
  <button id="reset" class="btn btn-primary">Reset</button>
  </form>  
</div>
  
 <div class="gameboard" id="gameboard">
 
 <div class="row">
  <div class="square sq1"></div>
  <div class="square sq2"></div>
  <div class="square sq3"></div>
 </div>
  <div class="row">
  <div class="square sq4"></div>
  <div class="square sq5"></div>
  <div class="square sq6"></div>
 </div>
  <div class="row">
  <div class="square sq7"></div>
  <div class="square sq8"></div>
  <div class="square sq9"></div>
 </div>
 </div>
 
 <div class="spacer>"></div>
 <div class="col-md-12 text-center" id="reload"><a href="#" class="btn btn-primary btn-lg">Reload Game?</a></div>
 <div class="col-md-12 text-center spacer" id="win"> Player 1 has won the Game!!! </div>
 <div class="col-md-12 text-center spacer" id="hal"> Player 2 has won the Game!!! </div>

</body>
</html>
              
            
!

CSS

              
                .gameboard{
 width: 372px; 
 height: 372px; 
 margin: auto;
}

.square{
 float: left;
 background: rgba(255,255,255, .1);
 width: 122px;
 height: 122px; 
 border: 1px solid black;
 -moz-box-shadow: 8px 8px 15px #000; 
 -webkit-box-shadow: 8px 8px 15px #000;  
 box-shadow: 8px 8px 15px #000;
 cursor: pointer;
 text-align: center;
 font-size: 100px;

}

.ex{
 background-color: rgba(255,255,255, .1);
 color: #a80f9f;
}
.oh{
 background-color: rgba(255,255,255, .1);
 color: teal;
}

body{
 background: no repeat center center fixed; 
 background-image: url("http://res.cloudinary.com/mers/image/upload/v1506996356/659468_fcqz5b.jpg");
 -webkit-background-size: cover;
 -moz-background-size: cover;
  -o-background-size: cover; 
 background-size: cover; 
}

.spacer {
 padding: 5%;
 text-align: center;
 text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
  color: white;
  font: 2em Georgia, serif;
}

.btn {
 -moz-box-shadow: 8px 8px 15px
  #000; 
 -webkit-box-shadow: 8px 8px 15px #000;  
 box-shadow: 8px 8px 15px #000;
}

.btn-primary, 
.btn-primary:focus {
  background-color: #33B7A5;
  opacity: 2;
  outline: 0;  
  transition: all ease .3s;
}

.btn-primary:hover {
  background-color: #4C2F39;
}

              
            
!

JS

              
                /*code for selecting "x" or "o" */
 {
 var msg = document.querySelector('.message');
var chooser = document.querySelector('form');

// add click listener to radio buttons
function setPlayer() {
  mark = this.value;
  msg.textContent = mark + ', click on a square to make your move!';
  chooser.classList.add('game-on');
  this.checked = false; 
}

// let the computer make the next move
function computerMove() {
  var emptyCells = [];
  var random;
  
  cells.forEach(function(cell){
    if (cell.textContent == '') {
      emptyCells.push(cell);
    }
  });  
}
var players = Array.prototype.slice.call(document.querySelectorAll('input[name=player-choice]'));
players.forEach(function(choice){
  choice.addEventListener('click', setPlayer, false);
});


/*original code*/
    
   var player = 1;
   var random = Math.floor(Math.random() * 9) + 1;
   var rand = Math.floor(Math.random() * 9) + 1;
 
 /*var msg = document.querySelector('.message');*/
/* var chooser = document.querySelector('form');*/
 /* function selects random square  */
 
 
   var tag = ".sq" + rand.toString();
  
    $("#reload").show();
    $("#win").hide();
    $("#hal").hide();
    
 
    $('.square').on('click', function(event){
        
        //alert('add symbol here');
       
        
        var squareSelected = $(this);
        
        if(squareSelected.hasClass('ex')||squareSelected.hasClass('oh')){
            alert('This square has been selected!');
        }else{//all of the logic
           
            if(player===1){
                squareSelected.addClass('ex').append('<i class="fa fa-times" aria-hidden="true"></i>');
                if(didIwin('ex')){
                    $("#gameBoard").hide();
  alert('Congrats! Player ' + player + ' has won! Press relod button below to load a new game');
                     $("#reload").show();
                      $("#reload").click(function() {
                        location.reload();
                        });
                        $("#win").show();
                }else{
                   
                    console.log (tag);
                    player = 2;
                }
            
            }else{
                $(randomSquare(random)).addClass('oh').append('<i class="fa fa-circle-o" aria-hidden="true"></i>');
                if(didIwin('oh')){
                    $("#gameBoard").hide();
  /* alert('Rats! HAL has won! Press ok to load a new game');*/
                     $("#reload").show();
                      $("#reload").click(function() {
                        location.reload();
                        });
                        $("#hal").show();
                }else{
                    console.log (tag);
                    player = 1;
                }
            }
            
           
            
        }
        
    });
    
    /*helper function checks to see who won */
   function didIwin(symbol) {
    
     /* checking to see if it's an x or o */
            if($('.sq1').hasClass(symbol) && $('.sq2').hasClass(symbol) && $('.sq3').hasClass(symbol)){
                return true;
            }else if($('.sq4').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq6').hasClass(symbol)){
               return true;
            }else if($('.sq7').hasClass(symbol) && $('.sq8').hasClass(symbol) && $('.sq9').hasClass(symbol)){ 
               return true;
            }else if($('.sq1').hasClass(symbol) && $('.sq4').hasClass(symbol) && $('.sq7').hasClass(symbol)){ 
               return true;
            }else if($('.sq2').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq8').hasClass(symbol)){ 
               return true;
            }else if($('.sq3').hasClass(symbol) && $('.sq6').hasClass(symbol) && $('.sq9').hasClass(symbol)){ 
               return true;
            }else if($('.sq1').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq9').hasClass(symbol)){ 
               return true;
            }else if($('.sq7').hasClass(symbol) && $('.sq5').hasClass(symbol) && $('.sq3').hasClass(symbol)){ 
               return true;
            }else{
                return false;
            }

   }
   /*function selects random square*/
   
   function randomSquare(rand){
     
     
       while ($(tag).hasClass('ex') || $(tag).hasClass('oh')){
            rand = Math.floor(Math.random() * 9) + 1;
            tag = ".sq" + rand.toString();
        }
           
            return tag;
        
   };
   
   
    
}
              
            
!
999px

Console