HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's 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 it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<link href="https://fonts.googleapis.com/css?family=Rock Salt"
rel="stylesheet" type="text/css">
<div class='container-fluid text-center'>
<br>
<br>
<h1>TiC-tAc-ToE</h1>
<br>
<br>
<h3 id='result'>cHOosE:</h3><br>
<a href='#' class='btn btn-primary turnBig' id='turnX'>X</a>
<a href='#' class='btn btn-secondary turnSmall' id='turnO'>O</a>
<br><br>
<div id='board'>
<div class='row justify-content-center'>
<div class='col-xs-4'>
<button class='tic t1' id='0'>#</button><button class='tic t2' id='1'>#</button><button class='tic t3' id='2'>#</button>
</div>
<!-- end col 1 -->
</div>
<div class='row justify-content-center'>
<div class='col-xs-4'>
<button class='tic t4' id='3'>#</button><button class='tic t5' id='4'>#</button><button class='tic t6' id='5'>#</button>
</div>
<!-- end col 2 -->
</div>
<div class='row justify-content-center'>
<div class='col-xs-4'>
<button class='tic t7' id='6'>#</button><button class='tic t8' id='7'>#</button><button class='tic t9' id='8'>#</button>
</div>
<!-- end col 3 -->
</div>
<!-- end of row -->
</div>
<br>
<a href='#' class = 'btn btn-danger bottom' id='reset'>rEseT</a>
<br>
<br><br>
<p>cReaTEd by JoSh FilIpPi</p><br>
</div>
<!-- end of container -->
body{
font-family: Rock Salt;
}
p{
font-size:13px;
}
.btn-secondary{
color: #006dcc;
border-style: none;
}
.btn-primary:visited{
color:white;
}
.btn-warning:visited{
color:white;
}
.btn-danger:visited{
color:white;
}
#reset{
font-size: 20px;
}
.turnBig{
font-size: 33px;
margin: 4px;
}
.turnSmall{
font-size: 19px;
margin: 4px;
}
#board{
padding: 20px;
}
button{
font-family:Rock Salt;
line-height: 30px;
font-size: 40px;
background: white;
color: white;
border-color:black;
border-radius: 0px;
border-style: solid;
border-width: 3px;
margin: 0px;
cursor: pointer;
cursor: hand;
width: 70px;
height: 80px;
}
button:focus {outline:0;}
.clickedOn{
color: black
}
.unClicked{
color:White;
}
.t1{
border-style: none;
}
.t2{
border-top-style: none;
border-bottom-style: none;
}
.t3{
border-style: none;
}
.t4{
border-left-style: none;
border-right-style: none;
}
.t6{
border-left-style: none;
border-right-style: none;
}
.t7{
border-style: none;
}
.t8{
border-top-style: none;
border-bottom-style: none;
}
.t9{
border-style: none;
}
.bottom{
width: 105px;
}
$(document).ready(function() {
var turn = 'x'; // player default symbol
var compTurn = 'o'; // computer default symbol
var turns = ['#', '#', '#', '#', '#', '#', '#', '#', '#'];
var gameOn = false; // checks if the game is over
var count = 0;
$('#turnX').click(function() { // change player symbol button
turn = 'x';
compTurn = 'o';
$('#turnX').addClass('btn-primary');
$('#turnX').removeClass('turnSmall');
$('#turnX').addClass('turnBig');
$('#turnO').removeClass('btn-primary');
$('#turnO').removeClass('turnBig');
$('#turnO').addClass('turnSmall');
}); // end of turnX function
$('#turnO').click(function() { // change player symbol button
turn = 'o';
compTurn = 'x';
$('#turnX').removeClass('btn-primary');
$('#turnX').removeClass('turnBig');
$('#turnX').addClass('turnSmall');
$('#turnO').removeClass('turnSmall');
$('#turnO').addClass('turnBig');
$('#turnO').removeClass('btn-secondary');
$('#turnO').addClass('btn-primary');
}); // end of turnO function
function computerTurn() {
var computerMove = (Math.random() * 10).toFixed(); // gets random number
var move = $('#' + computerMove).text();
if (count === 5 || gameOn === false) {
return
}
//console.log($('#4').text())
if(turns[4] === turn && count === 1){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
return;
}
if (turns[4] === '#') {
$('#4').text(compTurn);
$('#4').removeClass('unClicked');
$('#4').addClass('clickedOn');
turns[4] = compTurn;
return;
}
if(turns[0] === compTurn && turns[1] === compTurn && turns[2] === '#'){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === compTurn && turns[1] === compTurn && turns[0] === '#'){
$('#0').text(compTurn);
$('#0').removeClass('unClicked');
$('#0').addClass('clickedOn');
turns[0] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === compTurn && turns[0] === compTurn && turns[1] === '#'){
$('#1').text(compTurn);
$('#1').removeClass('unClicked');
$('#1').addClass('clickedOn');
turns[1] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[3] === compTurn && turns[4] === compTurn && turns[5] === '#'){
$('#5').text(compTurn);
$('#5').removeClass('unClicked');
$('#5').addClass('clickedOn');
turns[5] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[4] === compTurn && turns[5] === compTurn && turns[3] === '#'){
$('#3').text(compTurn);
$('#3').removeClass('unClicked');
$('#3').addClass('clickedOn');
turns[3] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === compTurn && turns[7] === compTurn && turns[8] === '#'){
$('#8').text(compTurn);
$('#8').removeClass('unClicked');
$('#8').addClass('clickedOn');
turns[8] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[8] === compTurn && turns[7] === compTurn && turns[6] === '#'){
$('#6').text(compTurn);
$('#6').removeClass('unClicked');
$('#6').addClass('clickedOn');
turns[6] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === compTurn && turns[8] === compTurn && turns[7] === '#'){
$('#7').text(compTurn);
$('#7').removeClass('unClicked');
$('#7').addClass('clickedOn');
turns[7] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === compTurn && turns[3] === compTurn && turns[6] === '#'){
$('#6').text(compTurn);
$('#6').removeClass('unClicked');
$('#6').addClass('clickedOn');
turns[6] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === compTurn && turns[6] === compTurn && turns[3] === '#'){
$('#3').text(compTurn);
$('#3').removeClass('unClicked');
$('#3').addClass('clickedOn');
turns[3] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === compTurn && turns[3] === compTurn && turns[0] === '#'){
$('#0').text(compTurn);
$('#0').removeClass('unClicked');
$('#0').addClass('clickedOn');
turns[0] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[1] === compTurn && turns[4] === compTurn && turns[7] === '#'){
$('#7').text(compTurn);
$('#7').removeClass('unClicked');
$('#7').addClass('clickedOn');
turns[7] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[7] === compTurn && turns[4] === compTurn && turns[1] === '#'){
$('#1').text(compTurn);
$('#1').removeClass('unClicked');
$('#1').addClass('clickedOn');
turns[1] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === compTurn && turns[5] === compTurn && turns[8] === '#'){
$('#8').text(compTurn);
$('#8').removeClass('unClicked');
$('#8').addClass('clickedOn');
turns[8] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === compTurn && turns[8] === compTurn && turns[5] === '#'){
$('#5').text(compTurn);
$('#5').removeClass('unClicked');
$('#5').addClass('clickedOn');
turns[5] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[8] === compTurn && turns[5] === compTurn && turns[2] === '#'){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === compTurn && turns[4] === compTurn && turns[8] === '#'){
$('#8').text(compTurn);
$('#8').removeClass('unClicked');
$('#8').addClass('clickedOn');
turns[8] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[8] === compTurn && turns[4] === compTurn && turns[0] === '#'){
$('#0').text(compTurn);
$('#0').removeClass('unClicked');
$('#0').addClass('clickedOn');
turns[0] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === compTurn && turns[4] === compTurn && turns[6] === '#'){
$('#6').text(compTurn);
$('#6').removeClass('unClicked');
$('#6').addClass('clickedOn');
turns[6] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === compTurn && turns[4] === compTurn && turns[2] === '#'){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === turn && turns[1] === turn && turns[2] === '#'){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === turn && turns[2] === turn && turns[1] === '#'){
$('#1').text(compTurn);
$('#1').removeClass('unClicked');
$('#1').addClass('clickedOn');
turns[1] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[1] === turn && turns[2] === turn && turns[0] === '#'){
$('#0').text(compTurn);
$('#0').removeClass('unClicked');
$('#0').addClass('clickedOn');
turns[0] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[3] === turn && turns[4] === turn && turns[5] === '#'){
$('#5').text(compTurn);
$('#5').removeClass('unClicked');
$('#5').addClass('clickedOn');
turns[5] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[4] === turn && turns[5] === turn && turns[3] === '#'){
$('#3').text(compTurn);
$('#3').removeClass('unClicked');
$('#3').addClass('clickedOn');
turns[3] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === turn && turns[7] === turn && turns[8] === '#'){
$('#8').text(compTurn);
$('#8').removeClass('unClicked');
$('#8').addClass('clickedOn');
turns[8] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[7] === turn && turns[8] === turn && turns[6] === '#'){
$('#6').text(compTurn);
$('#6').removeClass('unClicked');
$('#6').addClass('clickedOn');
turns[6] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === turn && turns[8] === turn && turns[7] === '#'){
$('#7').text(compTurn);
$('#7').removeClass('unClicked');
$('#7').addClass('clickedOn');
turns[7] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === turn && turns[3] === turn && turns[6] === '#'){
$('#6').text(compTurn);
$('#6').removeClass('unClicked');
$('#6').addClass('clickedOn');
turns[6] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === turn && turns[6] === turn && turns[3] === '#'){
$('#3').text(compTurn);
$('#3').removeClass('unClicked');
$('#3').addClass('clickedOn');
turns[3] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[3] === turn && turns[6] === turn && turns[0] === '#'){
$('#0').text(compTurn);
$('#0').removeClass('unClicked');
$('#0').addClass('clickedOn');
turns[0] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[1] === turn && turns[4] === turn && turns[7] === '#'){
$('#7').text(compTurn);
$('#7').removeClass('unClicked');
$('#7').addClass('clickedOn');
turns[7] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[4] === turn && turns[7] === turn && turns[1] === '#'){
$('#1').text(compTurn);
$('#1').removeClass('unClicked');
$('#1').addClass('clickedOn');
turns[1] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === turn && turns[5] === turn && turns[8] === '#'){
$('#8').text(compTurn);
$('#8').removeClass('unClicked');
$('#8').addClass('clickedOn');
turns[8] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === turn && turns[8] === turn && turns[5] === '#'){
$('#5').text(compTurn);
$('#5').removeClass('unClicked');
$('#5').addClass('clickedOn');
turns[5] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[5] === turn && turns[8] === turn && turns[2] === '#'){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[4] === turn && turns[8] === turn && turns[0] === '#'){
$('#0').text(compTurn);
$('#0').removeClass('unClicked');
$('#0').addClass('clickedOn');
turns[0] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[0] === turn && turns[4] === turn && turns[8] === '#'){
$('#8').text(compTurn);
$('#8').removeClass('unClicked');
$('#8').addClass('clickedOn');
turns[8] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[2] === turn && turns[4] === turn && turns[6] === '#'){
$('#6').text(compTurn);
$('#6').removeClass('unClicked');
$('#6').addClass('clickedOn');
turns[6] = compTurn;
winCondition(turns, compTurn);
return;
}
if(turns[6] === turn && turns[4] === turn && turns[2] === '#'){
$('#2').text(compTurn);
$('#2').removeClass('unClicked');
$('#2').addClass('clickedOn');
turns[2] = compTurn;
winCondition(turns, compTurn);
return;
}
if (move === '#') { // if square is empty
$('#' + computerMove).text(compTurn);
$('#' + computerMove).removeClass('unClicked');
$('#' + computerMove).addClass('clickedOn');
turns[computerMove] = compTurn;
} else if (move !== '#') { // if square is not empty restart function
computerTurn();
}
winCondition(turns, compTurn); // checks for winning combinations
}
function playerTurn(turn, id) {
var spotTaken = $('#' + id).text(); // value of square
if (spotTaken === '#') { // if square is empty
count++;
$('#' + id).text(turn); // add player turn to square
turns[id] = turn; // adds player turn to corresponding index of turns array
winCondition(turns, turn); // call win condition function
if (gameOn === true) {
setTimeout(function() {
computerTurn();
}, 500);
}
}
}
function winCondition(turnArray, currentTurn) {
if (turnArray[0] === currentTurn && turnArray[1] === currentTurn && turnArray[2] === currentTurn) { // if top 3 squares are all same
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[3] === currentTurn && turnArray[4] === currentTurn && turnArray[5] === currentTurn) { // middle 3 squares
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[6] === currentTurn && turnArray[7] === currentTurn && turnArray[8] === currentTurn) { // and so on
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[0] === currentTurn && turnArray[3] === currentTurn && turnArray[6] === currentTurn) {
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[1] === currentTurn && turnArray[4] === currentTurn && turnArray[7] === currentTurn) {
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[2] === currentTurn && turnArray[5] === currentTurn && turnArray[8] === currentTurn) {
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[0] === currentTurn && turnArray[4] === currentTurn && turnArray[8] === currentTurn) {
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else if (turnArray[2] === currentTurn && turnArray[4] === currentTurn && turnArray[6] === currentTurn) {
$('#result').text("plAyeR '" + currentTurn + "' wInS!");
$('#result').addClass("animated zoomIn");
gameOn = false;
setTimeout(function() {
reset()
}, 1700);
} else {
gameOn = true;
if(turns[0] !== '#' && turns[1] !== '#' && turns[2] !== '#' && turns[3] !== '#' && turns[4] !== '#' && turns[5] !== '#' && turns[6] !== '#' && turns[7] !== '#' && turns[8] !== '#' && gameOn === true){
$('#result').text('DrAW');
$('#result').removeClass("animated fadeOut");
$('#result').addClass("animated zoomIn");
setTimeout(function() {
reset()
}, 1700);
}
}
} // end of winCondition function
$('.tic').click(function() { // identifies square clicked on by player
//console.log($(this).text());
$('#result').addClass('animated fadeOut');
$(this).removeClass('unClicked');
$(this).addClass('clickedOn');
var slot = $(this).attr('id'); // identifies square clicked on by player
playerTurn(turn, slot); // adds player turn symbol to square
});
function reset() { // reset game
turns[0] = '#';
turns[1] = '#';
turns[2] = '#';
turns[3] = '#';
turns[4] = '#';
turns[5] = '#';
turns[6] = '#';
turns[7] = '#';
turns[8] = '#'; // reset turns array
count = 0;
$('.tic').removeClass('clickedOn');
$('.tic').addClass('unClicked');
$('.tic').text('#');
gameOn = false;
$('#result').text('cHOosE:');
$('#result').removeClass("animated zoomIn");
$('#result').removeClass("animated fadeOut");
}
$('#reset').click(function() {
reset(); // calls reset function when reset button is clicked
});
}); // end of document ready
Also see: Tab Triggers