Advertisement
Guest User

Untitled

a guest
Jul 24th, 2011
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Sudoku = (function() {
  2.  
  3.     var matrix;
  4.    
  5.     var createMatrix = function() {
  6.         matrix = new Array(9);
  7.         for (var i=0; i<matrix.length; i++) {
  8.             matrix[i] = new Array(9);
  9.         }
  10.         populateMatrix();
  11.     };
  12.    
  13.     var populateMatrix = function() {
  14.         var possibles = generatePossibleNumbersArray();
  15.         var found = false;
  16.         for(var i=0; i< matrix.length; i++) {
  17.             for(var j=0; j< matrix[i].length; j++) {
  18.                 while(possibles[i][j].length > 0) {
  19.                     var rnd = Math.floor(Math.random() * possibles[i][j].length);
  20.                     var num = possibles[i][j].splice(rnd, 1)[0];
  21.                     if(isValid(i, j, num)) {
  22.                         matrix[i][j] = num;
  23.                         found = true;
  24.                         break;
  25.                     } else {
  26.                         found = false;
  27.                         continue;
  28.                     }
  29.                 }
  30.                 if(!found) {
  31.                     matrix[i][j] = undefined;
  32.                     possibles[i][j] = [1,2,3,4,5,6,7,8,9];
  33.                     j -= 2;
  34.                 }
  35.             }
  36.         }
  37.     }  
  38.    
  39.     var generatePossibleNumbersArray = function() {
  40.         var possibles = new Array(9);
  41.         for(var i=0; i<possibles.length; i++) {
  42.             possibles[i] = new Array(9);
  43.             for(var j=0; j< possibles[i].length; j++) {
  44.                 possibles[i][j] = [1,2,3,4,5,6,7,8,9];
  45.             }
  46.         }
  47.         return possibles;
  48.     }
  49.    
  50.     var getRow = function(index) {
  51.         var row = [];
  52.         for (var i=0; i< 9; i++) {
  53.             row.push(matrix[index][i]);
  54.         }
  55.         return row;
  56.     }
  57.    
  58.     var getCol = function(index) {
  59.         var col = [];
  60.         for (var i=0; i<9; i++) {
  61.             col.push(matrix[i][index]);
  62.         }
  63.         return col;
  64.     }
  65.    
  66.     var isValid = function(row, col, val) {
  67.         var valid = true;
  68.         if(inRow(row, val) || inCol(col, val) || inGrid(row, col, val)) {
  69.             valid = false;
  70.         }
  71.         return valid;
  72.     }
  73.    
  74.     var inGrid = function(row, col, val) {
  75.         var x, y;
  76.         var found = false;
  77.        
  78.         x = Math.floor(row / 3) * 3;
  79.         y = Math.floor(col / 3) * 3;
  80.        
  81.         for (var i=x; i < x+3; i++) {
  82.             for (var j=y; j < y+3; j++) {
  83.                 if(matrix[i][j] === val) {
  84.                     found = true;
  85.                 }
  86.             }
  87.         }
  88.         return found;  
  89.     }
  90.    
  91.     var inRow = function(row, val) {
  92.         var theRow = getRow(row);
  93.         return inArr(theRow, val);
  94.     }
  95.    
  96.     var inCol = function(col, val) {
  97.         var theCol = getCol(col);
  98.         return inArr(theCol, val);
  99.     }
  100.    
  101.     var inArr = function(arr, val) {
  102.         var found = false;
  103.         var length = arr.length;
  104.         for (var i=0; i< length; i++) {
  105.             if(arr[i] === val && val !== undefined) {
  106.                 found = true;
  107.                 break;
  108.                 console.log("Matched "+arr[i] + " with "+val);
  109.             }
  110.         }
  111.         return found;
  112.     }
  113.    
  114.     var createBoard = function() {
  115.         var body = document.getElementsByTagName("body")[0];
  116.         var input;
  117.         for (var i=0; i<9; i++) {
  118.             for (var j=0; j<9; j++) {
  119.                 var br = document.createElement('br');
  120.                 input = document.createElement('input');
  121.                 input.name = 'cell'+i+j;
  122.                 input.id = 'cell'+i+j;
  123.                 input.style.width = "20px";
  124.                 input.style.height = "20px";
  125.                 input.style.margin = "1px";
  126.                 document.getElementsByTagName('body')[0].appendChild(input);
  127.                 if((j+1) % 9 === 0) {
  128.                     document.getElementsByTagName('body')[0].appendChild(br);
  129.                 }
  130.             }
  131.         }
  132.         updateBoard();
  133.     }
  134.    
  135.     var updateBoard = function() {
  136.         for (var i=0; i<9; i++) {
  137.             for (var j=0; j<9; j++) {
  138.                 var input = document.getElementById("cell"+i+j);
  139.                 input.value = matrix[i][j];
  140.             }
  141.         }
  142.     }
  143.    
  144.     var randomNum = function() {
  145.         return Math.floor(Math.random() * 9 + 1);
  146.     }
  147.    
  148.     return {
  149.         createMatrix : createMatrix,
  150.         populateMatrix : populateMatrix,
  151.         createBoard : createBoard,
  152.         updateBoard : updateBoard
  153.     };
  154.  
  155. })();
  156.  
  157. Sudoku.createMatrix();
  158. Sudoku.populateMatrix();
  159. Sudoku.createBoard();
  160. Sudoku.populateBoard();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement