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.

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

            
              h1 Sudoku in a Base 4 instead of 3
p A true Base 4 sudoku would be 16 layers of this, and you would have to worry about the z dimension as well. This is just a slice of the true base 4 experience.
div id="wrapper"
span id="btns"
  button class="control" onclick="check()" check
  button class="control" onclick="restart()" restart
  button class="control" onclick="random()" random
  button class="control" onclick="solve()" solve
  div id="counterWrapper"
    div id="solvedCount"

div id="selection"
  span id="s1" onmouseup="chooseSelection(s1)" 1
  span id="s2" onmouseup="chooseSelection(s2)" 2
  span id="s3" onmouseup="chooseSelection(s3)" 3
  span id="s4" onmouseup="chooseSelection(s4)" 4
  div class="split"
  span id="s5" onmouseup="chooseSelection(s5)" 5
  span id="s6" onmouseup="chooseSelection(s6)" 6
  span id="s7" onmouseup="chooseSelection(s7)" 7
  span id="s8" onmouseup="chooseSelection(s8)" 8
  div class="split"
  span id="s9" onmouseup="chooseSelection(s9)" 9
  span id="s10" onmouseup="chooseSelection(s10)" 10
  span id="s11" onmouseup="chooseSelection(s11)" 11
  span id="s12" onmouseup="chooseSelection(s12)" 12
  div class="split"
  span id="s13" onmouseup="chooseSelection(s13)" 13
  span id="s14" onmouseup="chooseSelection(s14)" 14
  span id="s15" onmouseup="chooseSelection(s15)" 15
  span id="s16" onmouseup="chooseSelection(s16)" 16
            
          
!

CSS

            
              @import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono&display=swap');

* {
  font-family: 'Share Tech Mono', monospace;
}

h1 { color: #274180; margin-left: 20px; }
p { max-width: 600px; margin-left: 20px; }

#btns { display: flex; margin-left: 20px; margin-top: 10px; align-items: center; }

button {
  width: 30px;
  height: 30px;
  
  margin: 0px;
  padding: 0px;
  border: 0px;
  
  border-left: 1px solid;
  border-top: 1px solid;
}

#counterWrapper {
  display: flex;
  width: 40px;
  height: 26px;
  margin: 0px;
  padding: 0px;
  margin-left: 4px;
  border: 2px solid black;
}

#solvedCount {
  text-align: center;
  margin: auto;
}

button:nth-child(4n) {
  border-right: 1px solid blue;
}

#wrapper {
  width: 480px;
  margin-left: 22px;
}

div:nth-child(4n) {
  border-bottom: 1px solid blueviolet;
}

button:hover {
  box-shadow: inset 0 0 2px 1px blueviolet;
}

button:focus {
  outline: none;
  box-shadow: inset 0 0 1px 1px white;
}

.control {
  width: 100px;
  margin: 4px;
  border: 2px solid black;
}

.control:hover {
  box-shadow: inset 0 0 2px 1px blueviolet;
}

.control:nth-child(4n) {
  border-right: 2px solid black;
}

#selection {
  display: none;
  position: fixed;
}

#selection > .split {
  display: block;
}

#selection > span {
  display: inline-block;
  width: 20px;
  border: 1px solid blueviolet;
  background: #323;
  color: #ddd;
  padding: 2px;
  cursor: pointer;
}

#selection > span:hover {
  background: #55a;
}

.wrong {
  box-shadow: inset 0 0 2px 1px red;
}
            
          
!

JS

            
              const possibleValues = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16'];

var currentlyEditing;
var currentlyEditingTimeout;
var wrap = document.getElementById('wrapper');
restart();

function update() {
  var counter = document.getElementById("solvedCount");
  var count = 0;
  for (var x = 0; x < 16; ++x) {
    for (var y = 0; y < 16; ++y) {
      if (cellIsFull(XYID(x, y))) {
        count++;
      }
    }
  }
  counter.innerHTML = count;
}

function restart() {
  wrap.innerHTML = "";
  for (var i = 0; i < 16; i++) {
    var row = document.createElement('div');
    for (var j = 0; j < 16; j++) {
      var newb = document.createElement('button');
      newb.innerHTML = '<span style="color: grey;">0</span>';
      newb.id = "c" + String(i).padStart(2, '0') + String(j).padStart(2, '0');
      newb.title = String(i).padStart(2, '0') + ", " + String(j).padStart(2, '0');
      newb.setAttribute("onmousedown", "showOptions(" + newb.id + ")");
      row.appendChild(newb);
    }
    wrap.appendChild(row);
  }
  update();
}

// convert x, y coordinate -> element ID
function XYID(x, y) {
  return "c" + String(x).padStart(2, '0') + String(y).padStart(2, '0');
}

// convert element ID -> x, y coordinate
function IDXY(cell) {
  var nums = cell.split("c")[1];
  return [parseInt(nums.substring(0, 2)), parseInt(nums.substring(2))]
}

function checkHBar(cell) {
  var c = document.getElementById(cell);
  var value = c.innerHTML;
  var rc = IDXY(cell);
  for (var x = 0; x < 16; x++) {
    if (x !== rc[0]) {
      if (document.getElementById(XYID(x, rc[1])).innerHTML == value) {
        return false;
      }
    }
  }
  return true;
}

function checkVBar(cell) {
  var c = document.getElementById(cell);
  var value = c.innerHTML;
  var rc = IDXY(cell);
  for (var y = 0; y < 16; y++) {
    if (y !== rc[1]) {
      if (document.getElementById(XYID(rc[0], y)).innerHTML == value) {
        return false;
      }
    }
  }
  return true;
}

function checkBox(cell) {
  var c = document.getElementById(cell);
  var value = c.innerHTML;
  var rc = IDXY(cell);

  var xmult = parseInt(rc[0] / 4);
  var ymult = parseInt(rc[1] / 4);
  var xstart = 4 * xmult;
  var ystart = 4 * ymult;
  var xend = 4 * xmult + 4;
  var yend = 4 * ymult + 4;

  for (var x = xstart; x < xend; x++) {
    for (var y = ystart; y < yend; y++) {
      if (x !== rc[0] && y !== rc[1]) {
        if (document.getElementById(XYID(x, y)).innerHTML === value) {
          return false;
        }
      }
    }
  }
  return true;
}

function isThisValid(cell, value) {
  document.getElementById(cell).innerHTML = value;
  return checkBox(cell) && checkVBar(cell) && checkHBar(cell);
}

function cellIsFull(cell) {
  var v = document.getElementById(cell).innerHTML;
  return possibleValues.includes(v);
}

function check() {
  for (var x = 0; x < 16; ++x) {
    for (var y = 0; y < 16; ++y) {
      if (cellIsFull(XYID(x, y))) {
        if (checkBox(XYID(x, y)) && 
            checkVBar(XYID(x, y)) && 
            checkHBar(XYID(x, y))) {
          document.getElementById(XYID(x, y)).classList.remove("wrong");
        }
        else {
          document.getElementById(XYID(x, y)).classList.add("wrong");
        }
      }
    }
  }
}

function random() {
  var attempts = Math.floor(Math.random() * 150) + 50;
  var listCompletedCells = [];
  while (attempts-- > 0) {
    var nn = Math.floor(Math.random() * 16);
    if (nn === 0) nn = 16;
    var xx = Math.floor(Math.random() * 16);
    var yy = Math.floor(Math.random() * 16);
    if (!cellIsFull(XYID(xx, yy))) {
      if (!listCompletedCells.includes(XYID(xx, yy))) {
        while (!isThisValid(XYID(xx, yy), nn)) { 
          nn++;
          if (nn >= 16) {
            document.getElementById(XYID(xx, yy)).innerHTML = '_';
            break;
          }
        }
        listCompletedCells.push(XYID(xx, yy));
      }
    }
  }
  update();
}

// TODO: implement real solver
// This isn't a true solve
function solve() {
  for (var x = 0; x < 16; ++x) {
    for (var y = 0; y < 16; ++y) {
      if (!cellIsFull(XYID(x, y))) {
        for (var n = 1; n <= 16; ++n) {
          if (isThisValid(XYID(x, y), n)) break; 
          else document.getElementById(XYID(x, y)).innerHTML = '_';
        }
      }
    }
  }
  update();
}

function showOptions(newb) {
  clearTimeout(currentlyEditingTimeout);
  currentlyEditing = newb;
  var mousepos = [event.clientX, event.clientY];
  var selector = document.getElementById('selection');
  selector.style.top = +mousepos[1] + 'px';
  selector.style.left = +mousepos[0] + 'px';
  selector.style.display = 'block';
  currentlyEditingTimeout = setTimeout(function(){selector.style.display = 'none';}, 3000);
}

function chooseSelection(sel) {
  var selector = document.getElementById('selection');
  selector.style.display = 'none';
  currentlyEditing.innerHTML = sel.innerHTML;
  currentlyEditing = null;
  update();
  check();
}
            
          
!
999px

Console