// 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 =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.