Guest User

Sudoku JavaScript

a guest
Nov 19th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('app', []);
  2. angular.module('app').config(['$controllerProvider', function($controllerProvider) {
  3.   $controllerProvider.allowGlobals();
  4. }]);
  5.  
  6. function emptyRow() {
  7.     return [0, 0, 0, 0, 0, 0, 0, 0, 0];
  8. }
  9.  
  10. function emptyMatrix() {
  11.     var sudokuMatrix = [];
  12.     for (var i = 0; i < 9; i++) {
  13.         sudokuMatrix[i] = emptyRow();
  14.     }
  15.     return sudokuMatrix;
  16. }
  17.  
  18. function updateValue(sudokuMatrix, row, col, val) {
  19.     row = parseInt(row);
  20.     col = parseInt(col);
  21.     val = parseInt(val);
  22.     sudokuMatrix[row][col] = val;
  23. }
  24.  
  25. function exampleMatrix() {
  26.     matrix = emptyMatrix();
  27.     updateValue(matrix, 0, 2, 6);
  28.     updateValue(matrix, 0, 3, 3);
  29.     updateValue(matrix, 0, 4, 2);
  30.  
  31.     updateValue(matrix, 1, 2, 1);
  32.     updateValue(matrix, 1, 5, 5);
  33.     updateValue(matrix, 1, 6, 3);
  34.     updateValue(matrix, 1, 7, 6);
  35.  
  36.     updateValue(matrix, 2, 0, 9);
  37.     updateValue(matrix, 2, 1, 4);
  38.     updateValue(matrix, 2, 3, 6);
  39.  
  40.     updateValue(matrix, 3, 2, 4);
  41.    
  42.     updateValue(matrix, 4, 0, 8);
  43.     updateValue(matrix, 4, 1, 7);
  44.     updateValue(matrix, 4, 3, 1);
  45.     updateValue(matrix, 4, 4, 3);
  46.     updateValue(matrix, 4, 5, 4);
  47.     updateValue(matrix, 4, 7, 5);
  48.     updateValue(matrix, 4, 8, 6);
  49.  
  50.     updateValue(matrix, 5, 6, 1);
  51.  
  52.     updateValue(matrix, 6, 5, 9);
  53.     updateValue(matrix, 6, 7, 4);
  54.     updateValue(matrix, 6, 8, 7);
  55.  
  56.     updateValue(matrix, 7, 1, 1);
  57.     updateValue(matrix, 7, 2, 7);
  58.     updateValue(matrix, 7, 3, 5);
  59.     updateValue(matrix, 7, 6, 2);
  60.  
  61.     updateValue(matrix, 8, 4, 7);
  62.     updateValue(matrix, 8, 5, 2);
  63.     updateValue(matrix, 8, 6, 5);
  64.    
  65.     return matrix;
  66. }
  67.  
  68. function TestController($scope) {
  69.     $scope.matrix = exampleMatrix();
  70.     $scope.updateValue = updateValue;
  71.     $scope.getMatrixVal = function getMatrixVal(row, col) {
  72.         return $scope.matrix[row][col];
  73.     };
  74. }
Add Comment
Please, Sign In to add comment