JavaScript Sudoku - Checking Rules

后端 未结 2 510
無奈伤痛
無奈伤痛 2021-01-21 02:43

I\'m working on a javascript sudoku, and as far as validation of input goes, I\'m stumped. As each value is entered into the sudoku table, I have an onkeyup triggering a functio

相关标签:
2条回答
  • 2021-01-21 03:07

    You can use this function to validate a solution :

    // returns true if arraySolution is valid, false otherwise
    function valid(arraySolution) {
        for (var y = 0; y < 9; ++y) {
            for (var x = 0; x < 9; ++x) {
                var value = arraySolution[y][x];
    
                if (value) {
                    // Check the line
                    for (var x2 = 0; x2 < 9; ++x2) {
                        if (x2 != x && arraySolution[y][x2] == value) {
                            return false;
                        } 
                    }
    
                    // Check the column
                    for (var y2 = 0; y2 < 9; ++y2) {
                        if (y2 != y && arraySolution[y2][x] == value) {
                            return false;
                        } 
                    }
    
                    // Check the square
                    var startY = Math.floor(y/3)*3;
                    for (var y2 = startY; y2 < startY + 3; ++y2) {
                        var startX = Math.floor(x/3)*3;
                        for (x2 = startX; x2 < startX + 3; ++x2) {
                            if ((x2 != x || y2 != y) && arraySolution[y2][x2] == value) {
                                return false;
                            }
                        }
                    }
                }
            }
        }
    
        return true;
    }
    
    0 讨论(0)
  • 2021-01-21 03:12

    jsBin demo

    var x = 0, map = [
      [x, x, 4, 5, x, 3, x, 7, x],
      [x, x, x, x, x, x, 3, 1, x],
      [3, 5, x, x, x, 9, 2, x, x],
      [x, x, x, x, 2, x, 9, 3, 7],
      [6, x, 9, x, x, x, 4, x, 8],
      [4, 7, 2, x, x, x, x, x, x],
      [x, x, 1, x, x, x, x, 5, 2],
      [x, 4, 5, x, x, x, x, x, x],
      [x, 6, x, 8, x, 1, 7, x, x]
    ];
    
    
    // This is what the user played
    // after every user input update the "user" object.
    var user = {};
    
    
    function numberExists() {
    
      var err = [];
    
      for(var r=0; r<9; r++){ // iterate map rows
    
        if(map[r][user.cell] === user.num){ // current row, specific cell == Match?
          err.push(user.num +" exists in Row"+ r +" Cell"+ user.cell);
        }
    
        if(r === user.row){  // Is this row selected by the user?
           for(var c=0; c<9; c++){ // Iterate all cells of this row
              if(map[r][c] === user.num){ // Match?
                 err.push(user.num +" exists in Row"+ r + " Cell"+ c);
              }
           }
        }
    
      }
    
      if(err.length){
        console.log( err.join(" and ") );
      } else { // No errors
        console.log( "GOOD! Row"+ user.row  +" Cell"+ user.cell );
      }
    
    }
    
    // TEST /////
    user.row  = 4;
    user.cell = 3;
    user.num  = 8;
    numberExists(); // Trigger search!
    

    will trigger (in the above example using r4, c3, n8)

    "8 exists in Row4 Cell8 and 8 exists in Row8 Cell3"

    0 讨论(0)
提交回复
热议问题