Sudoku Grid

by Jayson Buquia

HTML

<!-- <div class="legends">
  <span>boxIndex</span>
  <span>columnIndex</span>
  <span>rowIndex</span>
</div> -->
<div class="sudoku"></div>
<!-- <img src="https://cdn.iconverticons.com/files/png/abe31f4448fb9bc7_256x256.png" alt="" class="troll-face"> -->

SCSS

:root {
  --color-1: black;
  --color-2: blue;
  --color-3: teal;
}

* {
  box-sizing: border-box;
  font-family: sans-serif;
}

.legends {
  margin: 10px 0 0 10px;
  font-size: 10px;
  text-transform: uppercase;
}

.sudoku {
  background-color: #fefefe;
  display: inline-grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;
  gap: 0;
  border: 2px solid #aaa;
  margin: 10px;
  
  &__3x3 {
    display: inline-grid;
    grid-template-columns: repeat(3, 1fr);
    grid-auto-rows: 1fr;
    gap: 0;
    border: 2px solid #aaa;
  }
  
  &__1x1 {
    display: inline-flex;
    align-items: center;
    justify-content: space-around;
    border: 1px solid #aaa;
    width: 50px;
    height: 50px;
    text-align: left;
    
    font-size: 10px;
    line-height: 1;
    
    span {
      display: block;
    }
  }
}

@each $i in (1, 2, 3) {
  .legends span:nth-child(#{$i}) {
    color: var(--color-#{$i});
    margin-right: 10px;
  }
  
  .sudoku__1x1 span:nth-child(#{$i}) {
    color: var(--color-#{$i});
  }
}

.troll-face {
  position: fixed;
  top: 150px;
  left: 80px;
  width: 300px;
  opacity: .07;
}

JavaScript

// Fisher–Yates Shuffle
// https://bost.ocks.org/mike/shuffle/
function shuffle(array) {
  var m = array.length, t, i;

  // While there remain elements to shuffle…
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }

  return array;
}

function getShuffledNumbers() {
	return shuffle(`123456789`.split('').map(num => Number(num)));
}

function getEntryFor(boxIndex, columnIndex, rowIndex) {
	if ( !availableEntries.length ) {
  	availableEntries.push(...getShuffledNumbers());
  }
  
	// Loop through available entries until we get an entry for the cell  
  for (let i = availableEntries.length - 1; i >= 0; i-- ) {
  	const testEntry = availableEntries[i];
    
    console.log(isSuited(testEntry, boxIndex, columnIndex, rowIndex));
    
    // Skip this entry if already present  
  	if ( !isUnique(testEntry, boxIndex, columnIndex, rowIndex) )
    {
       continue;
		}
    
    // We got an available entry, let's pop it out from the availables
    availableEntries.splice(i, 1);
    
    // Save this entry to the containers
		boxContents[boxIndex].push(testEntry);
    columnContents[columnIndex].push(testEntry);
    rowContents[rowIndex].push(testEntry);
    
    // Return to use this available entry
		return testEntry;
  }
}

// Checks if unique in box, column, and row
function isUnique(number, boxIndex, columnIndex, rowIndex) {
	return !boxContents[boxIndex].includes(number)
  				&& !columnContents[columnIndex].includes(number)
          && !rowContents[rowIndex].includes(number);

	// Get starting column and row index
	const startingColumnIndex = (boxIndex % 3) * 3;
	const startingRowIndex = Math.floor(boxIndex / 3);
  // Get containers
	const boxContainer = boxContents[boxIndex];
  const columnContainers = columnContents.slice(startingColumnIndex, startingColumnIndex + 2);
  const rowContainers =...