Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

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.

Behavior

Auto Save

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

              
                
              
            
!

CSS

              
                body {
  background: radial-gradient(hsl(200, 83%, 12%), hsl(200, 83%, 5%));
  padding: 1em;
  display: flex;
  justify-content: center;
}
div {
  box-shadow: rgba(255, 255, 255, 0.5) 0 0 5px;
  background: black;
  display: flex;
  flex-flow: wrap;
  width: 273px;
  border: solid 1px white;
  border-radius: 3px;
  padding: 2px 2px 5px;
}
p {
  color: white;
  font-size: 30px;
  width: 30px;
  height: 30px;
  text-align: center;
  margin: 0;
}
p.original {
  color: hsl(0, 80%, 40%);
}
p.valid {
  color: hsl(120, 40%, 50%);
}
p:nth-of-type(3),
p:nth-of-type(6),
p:nth-of-type(12),
p:nth-of-type(15),
p:nth-of-type(21),
p:nth-of-type(24),
p:nth-of-type(30),
p:nth-of-type(33),
p:nth-of-type(39),
p:nth-of-type(42),
p:nth-of-type(48),
p:nth-of-type(51),
p:nth-of-type(57),
p:nth-of-type(60),
p:nth-of-type(66),
p:nth-of-type(69),
p:nth-of-type(75),
p:nth-of-type(78) {
  border-right: solid 1px rgba(255, 255, 255, 0.5);
}
p:nth-of-type(19),
p:nth-of-type(20),
p:nth-of-type(21),
p:nth-of-type(22),
p:nth-of-type(23),
p:nth-of-type(24),
p:nth-of-type(25),
p:nth-of-type(26),
p:nth-of-type(27),
p:nth-of-type(46),
p:nth-of-type(47),
p:nth-of-type(48),
p:nth-of-type(49),
p:nth-of-type(50),
p:nth-of-type(51),
p:nth-of-type(52),
p:nth-of-type(53),
p:nth-of-type(54)
{
  border-bottom: solid 1px rgba(255, 255, 255, 0.5);
  padding-bottom: 4px;
}
              
            
!

JS

              
                const sudoku = [
  8, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 3, 6, 0, 0, 0, 0, 0,
  0, 7, 0, 0, 9, 0, 2, 0, 0,
  0, 5, 0, 0, 0, 7, 0, 0, 0,
  0, 0, 0, 0, 4, 5, 7, 0, 0,
  0, 0, 0, 1, 0, 0, 0, 3, 0,
  0, 0, 1, 0, 0, 0, 0, 6, 8,
  0, 0, 8, 5, 0, 0, 0, 1, 0,
  0, 9, 0, 0, 0, 0, 4, 0, 0
]

let getLineArray = (nth, grid) => {
  // Base line index
  let b = 9 * (nth - 1)
  return [grid[b], grid[b+1], grid[b+2], grid[b+3], grid[b+4], grid[b+5], grid[b+6], grid[b+7], grid[b+8]]
}
let getColArray = (nth, grid) => {
  // Base column index
  let b = nth - 1
  return [grid[b], grid[b+9], grid[b+18], grid[b+27], grid[b+36], grid[b+45], grid[b+54], grid[b+63], grid[b+72]]
}
let squareNbToBaseIndex = (nth) => {
  let b;
  switch (nth) {
    case 1:
      b = 0;
      break;
    case 2:
      b = 3;
      break;
    case 3:
      b = 6;
      break;
    case 4:
      b = 27;
      break;
    case 5:
      b = 30;
      break;
    case 6:
      b = 33;
      break;
    case 7:
      b = 54;
      break;
    case 8:
      b = 57;
      break;
    case 9:
      b = 60;
      break;
  }
  return b
}
let getSquareArray = (nth, grid) => {
  // Base square index
  let b = squareNbToBaseIndex(nth);
  
  return [grid[b], grid[b+1], grid[b+2], grid[b+9], grid[b+9+1], grid[b+9+2], grid[b+18], grid[b+18+1], grid[b+18+2]]
  
}

let isNumberInLine = (nb, lineIndex, grid) => {
  let result = false
  let lineArray = getLineArray(lineIndex, grid)
  for (let i = 0; i < 9; i++) {
    if (lineArray[i] == nb) result = true
  }
  return result
}
let isNumberInCol = (nb, colIndex, grid) => {
  let result = false
  let colArray = getColArray(colIndex, grid)
  for (let i = 0; i < 9; i++) {
    if (colArray[i] == nb) result = true
  }
  return result
}
let isNumberInSquare = (nb, squareIndex, grid) => {
  let result = false
  let squareArray = getSquareArray(squareIndex, grid)
  for (let i = 0; i < 9; i++) {
    if (squareArray[i] == nb) result = true
  }
  return result
}

let isLineOK = (nth, grid) => {
  // Line is OK if all numbers are detected in it
  return (isNumberInLine(1, nth, grid) && isNumberInLine(2, nth, grid) && isNumberInLine(3, nth, grid) && isNumberInLine(4, nth, grid) && isNumberInLine(5, nth, grid) && isNumberInLine(6, nth, grid) && isNumberInLine(7, nth, grid) && isNumberInLine(8, nth, grid) && isNumberInLine(9, nth, grid))
}
let isColOK = (nth, grid) => {
  // Col is OK if all numbers are detected in it
  return (isNumberInCol(1, nth, grid) && isNumberInCol(2, nth, grid) && isNumberInCol(3, nth, grid) && isNumberInCol(4, nth, grid) && isNumberInCol(5, nth, grid) && isNumberInCol(6, nth, grid, grid) && isNumberInCol(7, nth, grid) && isNumberInCol(8, nth, grid) && isNumberInCol(9, nth, grid))
}
let isSquareOK = (nth, grid) => {
  // Square is OK if all numbers are detected in it
  return (isNumberInSquare(1, nth, grid) && isNumberInSquare(2, nth, grid) && isNumberInSquare(3, nth, grid) && isNumberInSquare(4, nth, grid) && isNumberInSquare(5, nth, grid) && isNumberInSquare(6, nth, grid) && isNumberInSquare(7, nth, grid) && isNumberInSquare(8, nth, grid) && isNumberInSquare(9, nth, grid))
}

let vizInit = () => {
  let body = document.getElementsByTagName('body')[0]
  let div = document.createElement('div')
  body.append(div)
  for (let i = 0; i < sudoku.length; i++) {
      let p = document.createElement("p")
      div.append(p)
  }
}

// Hahaha deep cloning dirty
let candidate = JSON.parse(JSON.stringify(sudoku))

// Memory of states (true -> keep, false -> discard)
let keeper = []
let initKeeper = () => {
  keeper = []
  for (let i = 0; i < 81; i++) {
    keeper.push(sudoku[i] !== 0 ? true : false)
  }
}
let saveKeeperLine = (nth) => {
  // Base line index
  let b = 9 * (nth - 1)
  keeper[b] = true; keeper[b+1] = true; keeper[b+2] = true; keeper[b+3] = true; keeper[b+4] = true; keeper[b+5] = true; keeper[b+6] = true; keeper[b+7] = true; keeper[b+8] = true;
}
let saveKeeperCol = (nth) => {
  // Base column index
  let b = nth - 1
  keeper[b] = true; keeper[b+9] = true; keeper[b+18] = true; keeper[b+27] = true; keeper[b+36] = true; keeper[b+45] = true; keeper[b+54] = true; keeper[b+63] = true; keeper[b+72];
}
let saveKeeperSquare = (nth) => {
  // Base square index
  let b = squareNbToBaseIndex(nth);
  
  keeper[b] = true; keeper[b+1] = true; keeper[b+2] = true; keeper[b+9] = true; keeper[b+9+1] = true; keeper[b+9+2] = true; keeper[b+18] = true; keeper[b+18+1] = true; keeper[b+18+2] = true;
}

let modelInit = () => {
  initKeeper()
}
let modelLoop = () => {
  // Change unkept numbers
  for (let i = 0; i < sudoku.length; i++) {
    if (!keeper[i]) {
      candidate[i] = Math.ceil(9 * Math.random())
    }
  }
  
  // Check validity of lines, if so tell keeper to save correponding numbers
  if (isLineOK(1, candidate)) { saveKeeperLine(1); }
  if (isLineOK(2, candidate)) { saveKeeperLine(2); }
  if (isLineOK(3, candidate)) { saveKeeperLine(3); }
  if (isLineOK(4, candidate)) { saveKeeperLine(4); }
  if (isLineOK(5, candidate)) { saveKeeperLine(5); }
  if (isLineOK(6, candidate)) { saveKeeperLine(6); }
  if (isLineOK(7, candidate)) { saveKeeperLine(7); }
  if (isLineOK(8, candidate)) { saveKeeperLine(8); }
  if (isLineOK(9, candidate)) { saveKeeperLine(9); }
  
  if (isColOK(1, candidate)) { saveKeeperCol(1); }
  if (isColOK(2, candidate)) { saveKeeperCol(2); }
  if (isColOK(3, candidate)) { saveKeeperCol(3); }
  if (isColOK(4, candidate)) { saveKeeperCol(4); }
  if (isColOK(5, candidate)) { saveKeeperCol(5); }
  if (isColOK(6, candidate)) { saveKeeperCol(6); }
  if (isColOK(7, candidate)) { saveKeeperCol(7); }
  if (isColOK(8, candidate)) { saveKeeperCol(8); }
  if (isColOK(9, candidate)) { saveKeeperCol(9); }
  
  if (isSquareOK(1, candidate)) { saveKeeperSquare(1); }
  if (isSquareOK(2, candidate)) { saveKeeperSquare(2); }
  if (isSquareOK(3, candidate)) { saveKeeperSquare(3); }
  if (isSquareOK(4, candidate)) { saveKeeperSquare(4); }
  if (isSquareOK(5, candidate)) { saveKeeperSquare(5); }
  if (isSquareOK(6, candidate)) { saveKeeperSquare(6); }
  if (isSquareOK(7, candidate)) { saveKeeperSquare(7); }
  if (isSquareOK(8, candidate)) { saveKeeperSquare(8); }
  if (isSquareOK(9, candidate)) { saveKeeperSquare(9); }
}

let vizLoop = () => {
    // Refresh
    for (let i = 0; i < sudoku.length; i++) {
      let p = document.getElementsByTagName('p')[i]
      p.innerHTML = candidate[i]
    }
    
    // Check validity and highlights
    for (let i = 0; i < sudoku.length; i++) {
      let p = document.getElementsByTagName('p')[i]
      p.className = candidate[i] == sudoku[i] ? "original" : (keeper[i] ? "valid" : "")
    }
}

modelInit()
vizInit()

setInterval(() => {
  modelLoop()
  vizLoop()
}, 50)


              
            
!
999px

Console