Skip to content

Instantly share code, notes, and snippets.

@adriancooney
Created April 20, 2012 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriancooney/2431944 to your computer and use it in GitHub Desktop.
Save adriancooney/2431944 to your computer and use it in GitHub Desktop.
Generate a sudoku game
var sudoku = [], iterationCount = 0;
function check(type, pos, num) {
var bool = false;
for(var i = 0; i < 9; i++) {
var tile = (type === 1) ? (pos * 9) + i : //row
(type === 2) ? (i * 9) + pos : null; //Column
(((((pos - (pos % 3))/3) * 3)) * 9) + ((((pos % 3) * 3)) + (i % 3)); //Quadrant
iterationCount++;
if(sudoku[tile] == num) return false;
else bool = true;
}
return bool;
}
for(var y = 0; y < 9; y++)
for(var x = 0; x < 9; x++) {
for(var i = 0; i < 9; i++) {
var tile = (y*9) + x,
num = i + 1;
if(check(1, y, num) && check(2, x, num) && check(3, ((y % 3) * 3) + (x % 3), num)) {
sudoku[tile] = num;
}
}
if(!sudoku[tile]) sudoku[tile] = "null";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment