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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 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.

HTML

              
                
              
            
!

CSS

              
                body{
    display:grid;
    grid-template-columns:repeat(10,50px);
    grid-template-rows:repeat(10,50px);
    margin:50px auto;
    width:500px;
}
div{
  border-radius: 0px;
}
              
            
!

JS

              
                var lignes = [0,0,0,0,0,0,0,0,0,0]; // dix lignes vides
var pieces = [
    ['10',
     '10',
     '11'], 
    ['111',
     '010'],
    ['1',
     '1',
     '1'],
    ['11',
     '11'],
    ['01',
     '10'],
    ['10',
     '01']  
]; // mes pièces
var colors = ["#F60","#60F","#556","#112","#ccd"]; // mes couleurs
var ligneWidth = 10; // la longueur des lignes
var ligneHeight = 10; // hauteur
var loop = 500; // le nombre d'itérations

/* un chiffre entier entre 0 et max-1 */
function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

/* une valeur random du tableau */
function getRandom(arr){ 
  var max = (arr.length);
  var randIndex = getRandomInt(max);
  return arr[randIndex];
}

/* test, placement x/y et couleur */
function construire(ligne,piece,color,x){
  if(ligneHeight - ligne - piece.length<0) {
    console.log('trop haut/bas');
    return false; // ça dépasse en hauteur, on sort de la function
  } 

  /* on teste pour chaque ligne */
  for(var i = 0;i<piece.length;i++){
    if(ligneWidth - x - piece[i].length<0) {
      return false; // ça dépasse en x, on sort de la function
    } 

    var pieceInti = parseInt(piece[i],2) << (ligneWidth - x - piece[i].length); // on ajoute des 0 selon x -> 111000000
    var ligneInt = parseInt(lignes[ligne+i],2); // la ligne "000011100000" en chiffre
    if((pieceInti | ligneInt) !== (pieceInti ^ ligneInt)){ // test de la place
      return false;  // pas de place, on sort
    }

  }
  /* mise à jour des lignes */
  for(var i = 0;i<piece.length;i++){
    var pieceInti = parseInt(piece[i],2) << (ligneWidth - x - piece[i].length); // on ajoute des 0 selon x -> 111000000
    var ligneInt = parseInt(lignes[ligne+i],2); // la ligne "000011100000" en chiffre
    lignes[ligne+i] = (pieceInti | ligneInt).toString(2); // mise à jour de la ligne 
  }
  
  /* dessin et coloriage */
  for(var i = 0;i<piece.length;i++){

    var el = document.createElement("div");
    el.style.backgroundColor = color;
    el.style.gridColumn = (x+1)+piece[i].indexOf('1')+" / span "+(piece[i].match(/1/g) || []).length;
    el.style.gridRow = ligne+1+i+ " / span 1";  
    document.body.appendChild(el);
  }
}



/* on lance n fois */

for(var i = 0;i<loop;i++ ){
  var l = getRandomInt(10);
  var c = getRandom(colors);
  var p = getRandom(pieces);
  var x = getRandomInt(ligneWidth);
  construire(l,p,c,x);
}
              
            
!
999px

Console