Created
April 20, 2012 21:13
-
-
Save adriancooney/2431944 to your computer and use it in GitHub Desktop.
Generate a sudoku game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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