简体   繁体   English

Sudoku Checker 2D数组Java

[英]Sudoku Checker 2d array Java

So im having a bit of problem with my code.. It's suppose to cross check rows and columns for same integers. 所以我的代码有点问题。.假设交叉检查行和列是否为相同的整数。

this is what i have so far.. but when i run it, it only seems to check the first integer only. 这是我到目前为止所拥有的..但是当我运行它时,它似乎只检查第一个整数。 (for example the first line of the sudoku board reads. 1 2 2 2 2 2 2 2 2 2) it wont detect the obvious multiple 2's but if i change the input to 1 1 2 2 2 2 2 2 2 the error will come up of multiple 1's in this case. (例如,数独板的第一行显示为1 2 2 2 2 2 2 2 2 2 2)它不会检测到明显的倍数2,但是如果我将输入更改为1 1 2 2 2 2 2 2 2 2,错误就会来临在这种情况下最多为1。 the multiple any suggestions to tweak my loops to make it go through the columns? 有很多建议可以调整我的循环以使其遍历各列?

public static void validate(final int[][] sudokuBoard) {
    int width = sudokuBoard[0].length;
    int depth = sudokuBoard.length;

    for (int i = 0; i < width; i++) {
            int j = i;
            int reference = sudokuBoard[i][j];

            while (true) {
                if ((j >= width) || (j >= depth)) {
                    break;
                } 
                else if (i == j){
                    // do nothing
                }
                else if (j < width) {
                    int current = sudokuBoard[i][j];

                    if (current == reference) {
                        System.out.print("Invalid entry found (width)" + "\n");
                        System.out.print(current + "\n");


                        // invalid entry found do something
                    }
                } else if (j < depth) {
                    // note reversed indexes
                    int current = sudokuBoard[j][i];

                    if (current == reference) {
                        System.out.print("Invalid entry found (depth)" + "\n");
                        System.out.print(current + "\n");

                        // invalid entry found do something
                    }
                }
                j++;
            }

Your code is more complex than it should be. 您的代码比应该的要复杂。 Why put everything in one single function when you could split in several different functions? 当您可以拆分成几个不同的函数时,为什么将所有内容都放在一个函数中?

public static void Validate(final int[][] sudokuBoard)
{
    int width = sudokuBoard[0].length;
    int depth = sudokuBoard.length;

    for(int i = 0; i < width; i++)
        if(!IsValidRow(sudokuBoard, i, width))
        {
          //Do something - The row has repetitions
        }
    for(int j = 0; j < height; j++)
        if(!IsValidColumn(sudokuBoard, j, width))
        {
          //Do something - The columns has repetitions
        }
}

static bool IsValidRow(int[][] sudokuBoard, int referenceRow, int width)
{
    //Compare each value in the row to each other
    for(int i = 0; i < width; i++)
    {
        for(int j = i + 1; j < width; j++)
        {
            if(sudokuBoard[referenceRow][i] == sudokuBoard[referenceRow][j])
                return false
        }
    }
    return true;
}

static bool IsValidColumn(int[][] sudokuBoard, int referenceColumn, int height)
{
    //Compare each value in the column to each other
    for(int i = 0; i < height; i++)
    {
        for(int j = i + 1; j < height; j++)
        {
            if(sudokuBoard[i][referenceColumn] == sudokuBoard[j][referenceColumn])
                return false
        }
    }
    return true;
}

That way, your code is much more easily maintainable/readable. 这样,您的代码将更易于维护/阅读。 This code above hasn't been tested, but it should be about right. 上面的代码尚未经过测试,但应该正确。

I suggest debugging this code step by step to really understand what's going on, if that's not clear for you. 我建议逐步调试该代码,以真正了解发生的事情(如果您不清楚)。

Given the constraints of sudoku (a row of n cells must contain the numbers 1-n only) you don't need an order n^2 search (per row or column), you can do it order n by keeping a bit array indicating which numbers you've seen. 鉴于数独的限制(n个单元格的行必须仅包含数字1-n),您无需进行n ^ 2阶搜索(每行或每列),就可以通过保留一个表示以下内容的位数组来对n阶进行搜索您看到了哪些数字。 Here's the pseudo-code for checking rows, do the same for columns: 这是检查行的伪代码,对列执行相同的操作:

for i in 0 to depth-1 // rows
  boolean seen[] = new seen[width];
  for j in 0 to width-1 // columns
    if seen[board[i][j]-1] == true
      duplicate number
    else
      seen[board[i][j]-1] = true

I would break the functionality into smaller boolean checks. 我会将功能分解为较小的布尔检查。 This way, you can validate row by row, column by column, and square by square. 这样,您可以逐行,逐列和逐平方验证。 For instance 例如

private boolean isValidRow(int[] row) {
    // Code here to check for valid row (ie, check for duplicate numbers)
}

private boolean isValidColumn(int[] column) {
    // Code here to check for valid column
}

private boolean isValidSquare(int[][] square) {
    // Code here to check for valid square
}

Note that rows and columns only need to be passed a 1 dimensional array. 请注意,行和列仅需要传递一维数组。 Squares are a 2 dimensional array as you need to check a 3x3 area. 正方形是二维数组,您需要检查3x3区域。 You can also treat these methods as static as their functionality is independent of the Sudoku board instance. 您也可以将这些方法视为static方法,因为它们的功能独立于Sudoku板实例。

Edit: A suggestion on row/column/square validation is to use a HashSet. 编辑:关于行/列/正方形验证的建议是使用HashSet。 Sets can only have 1 element of a certain value, so you can add elements and look for a failure. 集合只能包含1个具有特定值的元素,因此您可以添加元素并查找失败。 For example: 例如:

HashSet<Integer> hs = new HashSet<Integer>();
for(int i = 0; i < 9; i++) {
    if(!hs.add(integerArray[i])) // HashSet.add returns 'false' if the add fails 
                                 // (ie, if the element exists)
        return false;
}
return true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM