Scratched a whole afternoon yesterday, finally made the result, after seeing the answer of others: I’m piece of ×××× ×

Create a function that takes a 9×9 two-dimensional array with nine items, each of which contains nine numbersdoneOrNot, check whether each row, column, and 3×3 area contain only one digit 1-9

Legend:


My thinking:

* To check whether a row, column, or block meets the criteria, you can rearrange the entire array into three different two-dimensional arrays by row, column, or block. * Declare a new array valid to hold the verification result. * Create a check method to check whether the items in the row, column, and block array match the numbers 1-9 only once. * Row, column, and block arrays are returned 9 times each, and their results are saved as Boolean to a new array VALID. * Traverses the valid array to determine whether the sudoku check is passed by checking whether all of the arrays are true.Copy the code

This time, I will use the for loop to write it all again.

So first, we need to get an array of rows, columns, and blocks. Because a row array is passed into the array itself, you only need to get columns and blocks separately.

function doneOrNot(board){
    let column = []
    let block = []
    // Get the column array column
    for(let i in board){
        let arr = []
        for(let j=1; j<10; j++){ arr.push(board[j-1][i])
            // Only if arR contains 9 items will it be pushed to column
            if(j === 9){
                column.push(arr)
            }   
        }
    }
    // Get block array block, four times loop, one word: ugly!
    for(let n=0; n<3; n++){for(let m=0; m<3; m++){let arr = []
            // x = 3*n and y = 3*m; // X = 3*n and y = 3*m
            // the range of x and y is 0- 2,3-5,6 -8
            // Go through each of the 9 blocks
            for(let x=3*n; x<3*n+3; x++){for(let y=3*m; y<3*m+3; y++){ arr.push(board[x][y])// Push arR to block only if it contains 9 items
                    if(arr.length === 9){
                        block.push(arr)
                    }
                }
            }
        }
    }
}
    
Copy the code

Now that we have the array of columns and blocks, we need a check method:

Add a new array valid = [1,2,3,4,5,6,7,8,9] and diff the valid and the array to be checked: Call valid minus all identical items in the array to be checked. Return true if the array to be validated does contain the numbers 1-9, if diff ends and the array is empty, otherwise return false.

Of course, there is an even easier way: check whether the incoming array contains the same items directly, because the array is fixed to be 9 in length. If the array contains the same items, then the array must not contain all the numbers from 1 to 9 at the same time.

I’m just going to do it in diff for review purposes, but that’s going to make it more complicated. (Actually, I didn’t think of judging the same item when I wrote it.)

function validator(arr){
    let valid = [1.2.3.4.5.6.7.8.9]
    function diff(arr,valid){
        let diffArr = valid
        for(let i in arr){
            for(let j in diffArr){
                if(diffArr[j] === arr[i]){
                    // If the same entry is found, the diffArr entry is deleted and the array length changes, requiring a new diff
                    diffArr.splice(j,1)
                    diff(arr,diffArr)
                    }
                }
          }
      // Check whether diffArr is valid. If the diffArr is valid, the verification succeeds.
      return diffArr.length===0?true:false
      }
    // The result of diff is returned by the validator function
      return diff(arr,valid)
  }
Copy the code

After the verification method is complete, the row, column, and block elements are passed into it for verification

// Declare an array to store the check result of a row and column block array
let rua = []
for(let i=0; i<9; i++){// It doesn't matter if the order is in order because we only need to check if there are any errors
    rua.push(validator(board[i]))
    rua.push(validator(column[i]))
    rua.push(validator(block[i]))
}
Copy the code

Rua will get a total of 27 checks for three two-dimensional arrays, and finally traverse RUA.

for(let i in rua){
    // If any of the values in ruA are not true, the sudoku check fails and false is returned
    if(! rua[i]){return false}}// All true, return true.
return true
Copy the code

Call it a day!


Attached complete code (for reference only and explain ideas, do not learn this pull code writing method)

function doneOrNot(board){
    let column = []
    let block = []
    // Get the column array column
    for(let i in board){
        let arr = []
        for(let j=1; j<10; j++){ arr.push(board[j-1][i])
            // Only if arR contains 9 items will it be pushed to column
            if(j === 9){
                column.push(arr)
            }   
        }
    }
    // Get block array block, four times loop, one word: ugly!
    for(let n=0; n<3; n++){for(let m=0; m<3; m++){let arr = []
            // x = 3*n and y = 3*m; // X = 3*n and y = 3*m
            // the range of x and y is 0- 2,3-5,6 -8
            // Go through each of the 9 blocks
            for(let x=3*n; x<3*n+3; x++){for(let y=3*m; y<3*m+3; y++){ arr.push(board[x][y])// Push arR to block only if it contains 9 items
                    if(arr.length === 9){
                        block.push(arr)
                    }
                }
            }
        }
    }
    function validator(arr){
    let valid = [1.2.3.4.5.6.7.8.9]
    function diff(arr,valid){
        let diffArr = valid
        for(let i in arr){
            for(let j in diffArr){
                if(diffArr[j] === arr[i]){
                    // If the same entry is found, the diffArr entry is deleted and the array length changes, requiring a new diff
                    diffArr.splice(j,1)
                    diff(arr,diffArr)
                    }
                }
            }
        // Check whether diffArr is valid. If the diffArr is valid, the verification succeeds.
        return diffArr.length===0?true:false
        }
    // The result of diff is returned by the validator function
        return diff(arr,valid)
    }
    // Declare an array to store the check result of a row and column block array
    let rua = []
    for(let i=0; i<9; i++){// It doesn't matter if the order is in order because we only need to check if there are any errors
        rua.push(validator(board[i]))
        rua.push(validator(column[i]))
        rua.push(validator(block[i]))
    }
    for(let i in rua){
        // If any of the values in ruA are not true, the sudoku check fails and false is returned
        if(! rua[i]){return false}}// All true, return true.
    return true
}
Copy the code

Write in the back

Isn’t it ugly? Isn’t it ugly? Isn’t it ugly?

As the saying goes, “It’s not impossible!” But always optimize your code anyway, otherwise it becomes a little hard to do when the method gets too long…

So here is all the content, write very messy, there is a lot of room for optimization, and then slowly optimize to update the content.

I hope this idea can be used as a reference for someone to solve a problem. Thank you for reading this. I’ll run first.