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 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.
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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
body
div
#box
.jumbotron
.container
.row
h1(class=h1color) Tic Tac Toe
h3
-var col = "pink";
div
h1#choice(class="input") Choose your symbol
#choice.container
.row
div(class="col-xs-12 text-center")
h3(class="pink")
label(for="x") X
input(id="x" type="radio" name="selection" value="x" checked="checked")
label(for="o") O
input(id="o" type="radio" name="selection" value="o")
button(class="btn btn-primary aqua" onclick="start();") Start
div#test22
#box_area(class="container hidden")
//if has_game_started
.row
.block
button#r0c0(class=col box onclick="play_handler(r0c0['id'])" )
button#r0c1(class=col box onclick="play_handler(r0c1['id'])")
button#r0c2(class=col box onclick="play_handler(r0c2['id'])")
.row
.block
button#r1c0(class=col box onclick="play_handler(r1c0['id'])")
button#r1c1(class=col box onclick="play_handler(r1c1['id'])")
button#r1c2(class=col box onclick="play_handler(r1c2['id'])")
//button(class="pill-button") -
.row
.block
button#r2c0(class=col onclick="play_handler(r2c0['id'])")
button#r2c1(class=col onclick="play_handler(r2c1['id'])")
button#r2c2(class=col onclick="play_handler(r2c2['id'])")
br
.container
.row
#reset_area(class="col-sm-12 hidden")
button(class="btn btn-primary aqua" onclick="reset();") Reset
#quack
audio#beep(src="http://abir.x10.mx/jstest/ding.wav")
@import "bourbon";
.h1color {color: red}
//$button
.numfonts {
color: #333333;
font-family: ‘Palatino Linotype’, ‘Book Antiqua’, Palatino, serif;
}
$background: #3F51B5; //rgba(51, 15, 10, 100)
$button: #330f0a; //rgba(51, 15, 10, 100)
$color2: #394f49; //rgba(57, 79, 73, 100)
$color3: #65743a; //rgba(101, 116, 58, 100)
$color4: #efdd8d;
$color5: #efdd8d; //rgba(239, 221, 141, 100)
$color6: #f4fdaf; //rgba(244, 253, 175, 100)
$button-text: #f4fdaf;
//colors end
//buttons
.brown { @include button(pill, $button);}
.pill-button { @include button(pill, #ff9900);}
.red{ @include button(pill, red);}
.pink { @include button(pill,#FF4081
)}
.aqua {@include button(pill,#18FFFF
)}
.purple {@include button(pill,#3F51B5
)}
body,
html {
width: 100%;
background-color: $background;
color: $background;
}
hr {
color: $background;
}
h1 {
color: red;
}
#quack{
color: white;
}
.calculator {
width: 1050px;
height: 635px;
background-color: $color4;
margin: 0 auto;
}
.row {
display: block;
text-align: center;
.block
button {
width: 100px;
height: 100px;
background-color: $button;
color: $button-text;
margin: 6px;
border: 1px solid $color2;
border-top: 2px solid lighten($color2, 20);
font-size: 4em;
/* offset-x | offset-y | blur-radius | spread-radius | color */
box-shadow: 12px -7px $color2;
}
}
.modal.modal-wide .modal-dialog {
width: 90%;
}
.modal-wide .modal-body {
overflow-y: auto;
}
/* irrelevant styling */
body { text-align: center; }
body p {
max-width: 400px;
margin: 20px auto;
}
#tallModal .modal-body p { margin-bottom: 900px }
var game_board = clear_board();
var last_symbol_played="";
var latest_position;
//GUI FUNCTIONS START HERE
function disable_buttons(){
$('#r0c0').attr('disabled','disabled');
$('#r0c1').attr('disabled','disabled');
$('#r0c2').attr('disabled','disabled');
$('#r1c0').attr('disabled','disabled');
$('#r1c1').attr('disabled','disabled');
$('#r1c2').attr('disabled','disabled');
$('#r2c0').attr('disabled','disabled');
$('#r2c1').attr('disabled','disabled');
$('#r2c2').attr('disabled','disabled');
}
function draw_board(board){
//var new_board=remove_underscores(board);
var positions=get_position(board);
for (var i in positions){
if (positions[i]==="_"){
$('#'+i).html(" ");
}
else{
$('#'+i).html(positions[i]);
}
}
}
function enable_buttons(){
$('#r0c0').removeAttr('disabled');
$('#r0c1').removeAttr('disabled');
$('#r0c2').removeAttr('disabled');
$('#r1c0').removeAttr('disabled');
$('#r1c1').removeAttr('disabled');
$('#r1c2').removeAttr('disabled');
$('#r2c0').removeAttr('disabled');
$('#r2c1').removeAttr('disabled');
$('#r2c2').removeAttr('disabled');
}
function start(){
$("#choice").addClass('hidden');
$("#box_area").removeClass('hidden');
$("#reset_area").removeClass('hidden');
enable_buttons();
var comp_symbol = flip_symbol($('input[name=selection]:checked').val());
// $('#quack').html(comp_symbol);
var done=false;
computer_play_handler(comp_symbol);
/*have button to start
option to choose x and o
ALL code should happen here
don't change any other functions
*/
}
function computer_play_handler(comp_symbol){
var comp_symbol;
if (last_symbol_played.length === 0){
// comp_symbol="x";
}
else{
comp_symbol=flip_symbol(last_symbol_played);
}
//5 seconds delay and display a "spinner" that shows the computer is thinking
var move=next_move(comp_symbol,game_board);
if (move!==-1){
var cp=play(comp_symbol,move,game_board);
game_board=cp;
last_symbol_played=comp_symbol;
draw_board(game_board);
//5 seconds delay and display a "spinner" that shows the computer is thinking
}
if (winner(game_board)==="x" || winner(game_board)==="o"){
// $("#box_area").addClass('hidden');
disable_buttons();
beep.play();
}
}
function play_handler(id){
var position={};
position['r0c0']=0;
position['r0c1']=1;
position['r0c2']=2;
position['r1c0']=3;
position['r1c1']=4;
position['r1c2']=5;
position['r2c0']=6;
position['r2c1']=7;
position['r2c2']=8;
latest_position=position[id];
var cp;
var new_symbol;
if (last_symbol_played.length === 0){
new_symbol="x";
cp=play(new_symbol,latest_position,game_board)
}
else {
new_symbol=flip_symbol(last_symbol_played);
cp=play(new_symbol,latest_position,game_board)
}
if (cp!==null){
last_symbol_played=new_symbol;
game_board=cp;//this may need to be taken out
}
if (winner(game_board)==="x" || winner(game_board)==="o"){
draw_board(game_board); //this may be a new function that will draw a line through teh winning board
beep.play();
}
else{
draw_board(cp);
computer_play_handler();
}
//
}
function get_position(board){
var position={};
position['r0c0']=board[0];
position['r0c1']=board[1];
position['r0c2']=board[2];
position['r1c0']=board[3];
position['r1c1']=board[4];
position['r1c2']=board[5];
position['r2c0']=board[6];
position['r2c1']=board[7];
position['r2c2']=board[8];
return position;
}
function tester(){
//var board=["x","x","x","x","_","o","x","o","x"]
play_handler();
location=3;
//draw_board(board);
//clear_board(board);
}
//GUI FUNCTIONS END HERE
//ENGINE FUNCTIONS START HERE
function reset(){
game_board=clear_board();
draw_board(game_board);
last_symbol_played="";
$("#choice").removeClass('hidden');
$("#box_area").addClass('hidden');
$("#reset_area").addClass('hidden');
}
function flip_symbol(symbol){
if (symbol==="_"){return "_";}
if (symbol==="x"){
symbol="o";
}
else{
symbol="x"
}
return symbol;
}
function show_initial_board(){
//game_board=clear_board();
//draw_board(game_board);
disable_buttons();
// $("#box_area").hide();
}
function play(symbol,location,board){
//play (symbol, location, board) updates the board with the symbol returns the updated board. return null if board cannot be updated
var new_board=[];
for (var i in board){
new_board.push(board[i]);
}
if (board[location] === "_"){
new_board[location]=symbol;
}
else{
new_board=null;
}
//console.log(new_board);
return new_board;
}
function winner(board) {
// returns x or o if one of them is a winner - if not returns null
var result=null;
var i=0;
var done=false;
var all_wins=['012','345','678','036','147','258','048','246'];
while (!done && i < all_wins.length){
var a=Number(all_wins[i][0]);
var b=Number(all_wins[i][1]);
var c=Number(all_wins[i][2]);
if (board[a]===board[b] && board[b]===board[c] && board[a]!=="_" ){
done=true;
result=board[a];
}
i++
}
return result;
}
function next_move(symbol,board){
var move=-1;
/*The following may appear strange but I am doing so because javascript objects are mutable
if board is an object which it is (it is an array and arrays are objects) and I use:
var new_board=board;
new_board is not a copy of board. It is board. Both point to the same object. Any changes to new_board will also change board, because they are the same object. For the CS kinds this is pass by reference not pass by value
*/
var new_board=[];
for (var i in board){
new_board.push(board[i]);
}
var os=open_slots(new_board);
if (os.length===0){
return null;
}
//console.log(os.length)
var done=false;
var i=0;
while (i<os.length && !done){
if(symbol===winner(play(symbol,os[i],new_board))){
done=true;
move=os[i];
}
i++ ;
}
if (move ===-1) {
symbol=flip_symbol(symbol);
i=0;
while (i<os.length && !done){
if(symbol===winner(play(symbol,os[i],new_board))){
done=true;
move=os[i];
}
i++ ;
}
}
//code is not elegant
if (move===-1){
for (var i in os){
if (os[i]===4){
move=os[4]
}
}
if (move===-1){
move=os[0];
}
}
return move;
}
function clear_board(){
game_board=["_","_","_","_","_","_","_","_","_"];
return game_board;
}
function open_slots(board){
//board=["x","_","x","_","o","_","_","_","_"];
var open_slots=[];
for (var i=0; i<board.length; i++){
if(board[i] === "_"){
open_slots.push(i);
}
}
return open_slots;
}
Also see: Tab Triggers