I'm working on a constructor which lets the player input a starting sudoku grid and also checks if it's valid and can be played, ie, it doesn't already have duplicate numbers. The code listed here is my newest attempt and works on making each number +1 to another array. So game[j] = 1, so checkarray[(game[j])-2))] ++

then I check to see that checkArray all has 1s, otherwise, it probably ain't valid. This or some form of it will also be used for columm, 3x3 square checking. But it's not working and keeps on throwing the illegalargumentexception and I honestly can't figure out why. So help please?


public Sudoku(int[][] game) //constructor to allow player-inputted starting sudoku grid and see if it's valid
    {
        for (int i=0; i<9; i++){
            int[] checkarray = {0, 0, 0, 0, 0, 0, 0, 0, 0};
            for (int j=0; j<9; j++){
                checkarray[((game[i][j])+1)] = checkarray[((game[i][j])+1)] + 1;
            }
            for (int k=0; k<9; k++){
                if (checkarray[k] != 1){
                        throw new IllegalArgumentException("error");
                }
            }
        }
    }
public Sudoku(int[][] game)
    {
        int temp = 0;
        for (int i=0; i<9; i++){
            int[] checkarray = {0, 0, 0, 0, 0, 0, 0, 0, 0};
            for (int j=0; j<9; j++){
                temp = game[i][j];
                if (game[i][j] != 0){
                    checkarray[temp-1] = checkarray[temp-1] + 1;
                }
            }
            for (int k=0; k<9; k++){
                if (checkarray[k] == 2){
                        throw new IllegalArgumentException("error");
                }
            }
        }
    }

hmm, I have a bad habit of asking here and then sort of solving it after I've posted. I'm not too sure if this new constructor works perfectly but it's passed my first test. I'm doing some more but if anyone can spot any errors please tell me! thanks

basically what I did wrong was not realize game[][] had 0 values which would stuff up everything else and also, it was just easier to search for a 2 value on the checkarray rather than mess around with 1s and 0s. And a lot of other stuff..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.