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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <body> 
   
    <div id="board-outer" class="center">
        <table id="board">
            <tr class="row">
                <td id="a1"></td>
                <td id="a2"></td>
                <td id="a3"></td>
            </tr>
            <tr class="row">
                <td id="b1"></td>
                <td id="b2"></td>
                <td id="b3"></td>
            </tr>
            <tr class="row">
                <td id="c1"></td>
                <td id="c2"></td>
                <td id="c3"></td>
            </tr>
        </table>  
    </div>
   
    <div class="center button">
        <button id="playAgain">new game</button>
        
    </div>
   
    <!-- Win Alert Modal Box -->
   <div class = "winAlert">
     <div class="content">
					       <h3>YOU WIN!</h3>
       <p class="winFade">try again</p>
				    </div>
   </div> <!-- End Win Alert Modal Box -->
  
  <!-- Lose Alert Modal Box -->
   <div class = "loseAlert">
     <div class="content">
					       <h3>YOU LOSE!</h3>
       <p class="loseFade">try again</p>
				    </div>
   </div> <!-- Lose Alert Modal Box -->
  
  <!-- Tie Alert Modal Box -->
   <div class = "tieAlert">
     <div class="content">
					       <h3>IT'S A TIE!</h3>
       <p class="tieFade">try again</p>
				    </div>
   </div> <!-- Tie Alert Modal Box -->
  
  

</body>
            
          
!
            
              body {
   margin-top: 50px;
   background-color: #2F2F2F;
   font-family: 'Gochi Hand', cursive;
}

 .center {
    margin-right: auto;
    margin-left: auto;
}

 h1, h4 {
    text-align: center;
    color: #ecf0f1;
    border: 0;
}
p {
    text-align: center;
    line-height: 16pt;
    color: #666666;
}

a {
  text-decoration: none;
  font-size: 1.75em;
  color: #bbb;
  font-weight: 700;
  letter-spacing: 1px;
}
.button {
    margin-left:auto;
    margin-right:auto;
    text-align: center;
}

button {
  outline: 0;
}

#playAgain {
    font-size: 3em;;
    color: #ecf0f1;
    background-color: #2F2F2F;
    padding: 5px 15px 5px 15px;
    width: 250px;
    height: 40px;
    border: 0;
    font-family: 'Gochi Hand', cursive;
    margin-bottom: 30px;
    margin-top: 50px;
}

 #board-outer {
    height: 350px;
    width: 350px;
    overflow: hidden;
   margin-bottom: 30px;
}
#board {
    width: 354px;
    height: 354px;
    border-spacing:0;
    margin-left: -2px;
    margin-top: -2px;
    font-family: 'Gochi Hand', cursive;
}
td {
    height: 116px;
    width: 116px;
    text-align: center;
    font-size: 7em;
    border-collapse: collapse;
    border: 2px dotted #ecf0f1;
    text-transform: uppercase;
    color: #ecf0f1;
}
.x {
    width: 100%;
    height: 100%;
    background-color: blue;
}
.o {
    width: 100%;
    height: 100%;
    background-color: red;
}

/*style for hidden modal*/
.winAlert, .loseAlert, .tieAlert {
	width: 100%;
	height: 100%;
	color: #fff;
	background: #333;
	position: absolute;
	top: 0;
	left: 0;
	margin: 0;
	z-index: 1000;
	display: none;
}


.content {
	color: #ecf0f1;
	position: relative;
	height: auto;
	width: 600px;
	border-radius: 3px;
	top: 15%;
	margin: auto auto;
	
}

.content h3 {
	margin-bottom: 15px;
	padding: 0.4em;
	text-align: center;
	font-size: 4em;
	font-weight: 700;
  color: #a2dcf4;
  
}

.content p {
  margin-bottom: 25px;
  color: #ecf0f1;
  cursor: pointer;
  font-size: 2em;
}

            
          
!
            
              var xWins = false;
var oWins = false;
var turn = 0;

var human = 'X'; // turn = 0
var computer = 'O'; // turn = 1

$(".loseFade").click(function(){
        $(".loseAlert").fadeOut(1000);             
});

$(".winFade").click(function(){
        $(".winAlert").fadeOut(1000);             
});

$(".tieFade").click(function(){
        $(".tieAlert").fadeOut(1000);             
});


//board variables
var a1,a2,a3,b1,b2,b3,c1,c2,c3;


//Checks the current state of the board and updates the variables.
currentBoardState = function(){
  a1 = $('#a1').html();
  a2 = $('#a2').html();
  a3 = $('#a3').html();
  b1 = $('#b1').html();
  b2 = $('#b2').html();
  b3 = $('#b3').html();
  c1 = $('#c1').html();
  c2 = $('#c2').html();
  c3 = $('#c3').html();
};

//Checks each row, column, and diagonal to see if they all contain an "X". If they do
//Xwins is set to "true" and a win message is displayed.
//Otherwise, oWins is set to "true" and a "you lose" message is displayed.
checkForWinner = function(){
  if(((a1 == "X") && (a1 == a2 && a1 == a3))||
    ((b1 == "X") && (b1 == b2 && b1 == b3))||
    ((c1 == "X") && (c1 == c2 && c1 == c3))||
    ((a1 == "X") && (a1 == b1 && a1 == c1))||
    ((a2 == "X") && (a2 == b2 && a2 == c2))||
    ((a3 == "X") && (a3 == b3 && a3 == c3))||
    ((a1 == "X") && (a1 == b2 && a1 == c3))||
    ((a3 == "X") && (a3 == b2 && a3 == c1))) {
       xWins = true;
       winModal(); 
    }
  else {
    if (((a1 == "O") && (a1 == a2 && a1 == a3))||
    ((b1 == "O") && (b1 == b2 && b1 == b3))||
    ((c1 == "O") && (c1 == c2 && c1 == c3))||
    ((a1 == "O") && (a1 == b1 && a1 == c1))||
    ((a2 == "O") && (a2 == b2 && a2 == c2))||
    ((a3 == "O") && (a3 == b3 && a3 == c3))||
    ((a1 == "O") && (a1 == b2 && a1 == c3))||
    ((a3 == "O") && (a3 == b2 && a3 == c1))) {
       oWins = true; 
       winModal();
    } else {
       if (((a1 == "X") || (a1 == "O")) && ((b1 == "X") || (b1 == "O")) && ((c1 == "X") || (c1 == "O")) && ((a2 == "X") || (a2 == "O")) && ((b2 == "X") || (b2 == "O")) && ((c2 == "X") || (c2 == "O")) && ((a3 == "X") || (a3 == "O")) && ((b3 == "X") || (b3 == "O")) && ((c3 == "X") || (c3 == "O"))) {
                //alert("It's a tie!");
                winModal();
            }
            }  
    }
  };

//Check for a winner and then displays a message
var winModal = function () {
    if (xWins == true) {
        $(".winAlert").fadeIn(1000);
    } 
    else if (oWins == true) {
            $(".loseAlert").fadeIn(1000);
        }
    else {
      $(".tieAlert").fadeIn(1000);
    }
};

// When "new game" button is clicked the board is refreshed, win variables are
// set to "false" and the newGame function is called
var startOver = $('#playAgain').click(function(){
    a1 = $('#a1').text("");
    b1 = $('#b1').text("");
    c1 = $('#c1').text("");
    a2 = $('#a2').text("");
    b2 = $('#b2').text("");
    c2 = $('#c2').text("");
    a3 = $('#a3').text("");
    b3 = $('#b3').text("");
    c3 = $('#c3').text("");
    xWins = false;
    oWins = false;
    newGame();
    window.top.location.reload();  
});

// Looking for two "x" in a row.  If it finds them...it places an "o" there.
var twoExes = function () {
    if (a1 == "" && ((a3 == "X" && a2 == "X") || (c3 == "X" && b2 == "X") || (c1 == "X" && b1 == "X"))) {
        $('#a1').text("O");
        turn = 0;
    } else {
      if (a2 == "" && ((a1 == "X" && a3 == "X") || (c2 == "X" && b2 == "X"))) {
        $('#a2').text("O");
        turn = 0;
        }
        else{
        if (a3 == "" && ((a1 == "X" && a2 == "X") || (c1 == "X" && b2 == "X") || (c3 == "X" && b3 == "X"))) {
            $('#a3').text("O");
            turn = 0;
        }
            else{
            if (c3 == "" && ((c1 == "X" && c2 == "X") || (a1 == "X" && b2 == "X") || (a3 == "X" && b3 == "X"))) {
                $('#c3').text("O");
                turn = 0;
        }
                else{
                if (c1 == "" && ((c3 == "X" && c2 == "X") || (a3 == "X" && b2 == "X") || (a1 == "X" && b1 == "X"))) {
                    $('#c1').text("O");
                    turn = 0;
        }
                    else{
                    if (c2 == "" && ((c3 == "X" && c1 == "X") || (a2 == "X" && b2 == "X"))) {
                        $('#c2').text("O");
                        turn = 0;
        }
                        else{
                        if (b1 == "" && ((b3 == "X" && b2 == "X") || (a1 == "X" && c1 == "X"))) {
                            $('#b1').text("O");
                            turn = 0;
        }
                            else{
                            if (b3 == "" && ((a3 == "X" && c3 == "X") || (b2 == "X" && b1 == "X"))) {
                                $('#b3').text("O");
                                turn = 0;
        }
                                else{
                                if (b2 == "" && ((a3 == "X" && c1 == "X") || (c3 == "X" && a1 == "X") || (b3 == "X" && b1 == "X") || (c2 == "X" && a2 == "X"))) {
                                    $('#b2').text("O");
                                    turn = 0;
        }
                                   else{ // If no opportunity to block a win...play one of these.
                                    if (b2 == "") {
                                        $('#b2').text("O");
                                        turn = 0;
                                       
                                    }
                                        else{
                                        if (a1 == "") {
                                            $('#a1').text("O");
                                            turn = 0;
                                            
                                    }
                                            else{
                                            if (c3 == "") {
                                            $('#c3').text("O");
                                            turn = 0;
                                          
                                    } 
                                                else {
                                                if (c2 == "") {
                                            $('#c2').text("O");
                                            turn = 0;
                                          
                                    }
                                                    else{
                                                    if (b1 == "") {
                                            $('#b1').text("O");
                                            turn = 0;
                                          
                                    }
                                                    }
                                                }
                                            }
                                   
                                    
                                        }
                                   }
                                }
                            }
                        }
                    }
                }
            }
        }
    }   
};

var newGame = function () {
    $('td').one('click', function (event) {
        if (turn == 0) {
            $(this).text(human);
            currentBoardState();
            checkForWinner();
            turn == 1;
            twoExes();         
            currentBoardState();
            checkForWinner();
        }
    });
};

// 
$(document).ready(function () {
    newGame();
});
            
          
!
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.

Console