简体   繁体   中英

Sudoku Checker java

public class Sudoku {
public static void main(String[] args) {
    // Row and column Latin but with invalid subsquares 
    String config1 = "1234567892345678913456789124567891235678912346" + "78912345789123456891234567912345678";
    String[][] puzzle1 = makeSudoku(config1);

    if (isValidSudoku(puzzle1)) {
        System.out.println("This puzzle is valid.");
    } else {
        System.out.println("This puzzle is invalid.");
    }


    System.out.println(getPrintableSudoku(puzzle1));

    System.out.println("--------------------------------------------------");

    // Row Latin but column not Latin and with invalid subsquares 
    String config2 = "12345678912345678912345678912345678912345678" + "9123456789123456789123456789123456789";
    String[][] puzzle2 = makeSudoku(config2);

    if (isValidSudoku(puzzle2)) {
        System.out.println("This puzzle is valid.");
    } else {
        System.out.println("This puzzle is invalid.");
    }

    System.out.println(getPrintableSudoku(puzzle2));
    System.out.println("--------------------------------------------------");

    // A valid sudoku 
    String config3 = "25813764914698532779324685147286319558149273663" + "9571482315728964824619573967354218";
    String[][] puzzle3 = makeSudoku(config3);
    if (isValidSudoku(puzzle3)) {
        System.out.println("This puzzle is valid.");
    } else {
        System.out.println("This puzzle is invalid.");
    }
    System.out.println(getPrintableSudoku(puzzle3));
    System.out.println("--------------------------------------------------");
}

public static String[][] makeSudoku(String s) {
    int SIZE = 9;
    int k = 0;
    String[][] x = new String[SIZE][SIZE];
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            x[i][j] = s.substring(k, k + 1);
            k++;
        }
    }
    return x;
}

public static String getPrintableSudoku(String[][] x) {
    int SIZE = 9;
    String temp = "";
    for (int i = 0; i < SIZE; i++) {
        if ((i == 3) || (i == 6)) {
            temp = temp + "=================\n";
        }
        for (int j = 0; j < SIZE; j++) {
            if ((j == 3) || (j == 6)) {
                temp = temp + " || ";
            }
            temp = temp + x[i][j];
        }
        temp = temp + "\n";
    }
    return temp;
}

//sudoku validation
public static boolean isValidSudoku(String[][] x) {
    return rowsAreLatin(x) && colsAreLatin(x) && goodSubsquares(x);
}

public static boolean rowsAreLatin(String[][] x) {
    // fill in your code here 
    boolean result = true; // Assume rows are latin 
    for (int i = 0; i < 9; i++) {
        result = result && rowIsLatin(x, i); // Make sure each row is latin 
    }
    return result;
}
public static boolean rowIsLatin(String[][] x, int i) {

    boolean[] found = new boolean[9];

    for (int j = 0; j < 9; j++) {
        found[Integer.parseInt(x[i][j])] = true;
    }

    for (int j = 0; j < 9; j++) {
        if (!found[j]) {
            return false;
        }
    }
    return true;
}

public static boolean colsAreLatin(String[][] x) {
    // fill in your code here 
    boolean result = true; // Assume cols are latin 
    for (int j = 0; j < 9; j++) {
        result = result && colIsLatin(x, j); // Make sure each row is latin 
    }
    return result;
}

public static boolean colIsLatin(String[][] x, int j) {
    // fill in your code here 
    boolean[] found = new boolean[9];
    for (int i = 0; i < 9; i++) {
        found[Integer.parseInt(x[i][j])] = true;
    }

    for (int i = 0; i < 9; i++) {
        if (!found[i]) {
            return false;
        }
    }
    return true;
}

public static boolean goodSubsquares(String[][] x) {

    return true;
}
public static boolean goodSubsquare(String[][] x, int i, int j) {

    boolean[] found = new boolean[9];
    // We have a 3 x 3 arrangement of subsquares 
    // Multiplying each subscript by 3 converts to the original array subscripts 
    for (int p = i * 3, rowEnd = p + 3; p < rowEnd; p++) {
        for (int q = j * 3, colEnd = q + 3; q < colEnd; q++) {

            found[Integer.parseInt(x[p][q])] = true;

        }
    }
    for (int p = 0; p < 9; p++) {
        if (!found[p]) {
            return false;
        }
    }
    return true;
}
}

This the error I am getting. Help!

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Sudoku.rowIsLatin(Sudoku.java:104)
at Sudoku.rowsAreLatin(Sudoku.java:93)
at Sudoku.isValidSudoku(Sudoku.java:85)
at Sudoku.main(Sudoku.java:8)
Java Result: 1

The problem is with the line:

Integer.parseInt(x[i][j])

Sudoku numbers range from [1, 9], but indices for an array (of length 9) range from [0, 8]. So, when the (i, j) element is a 9, the index is 9 and therefore the IndexOutOfBoundsException is being thrown.

You'll have to change it to

found[Integer.parseInt(x[i][j]) - 1] = true;

Note that you also make the same mistake in the column's respective method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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