0

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");
                }
            }
        }
    }
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ausops
0
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..

Edited by ausops: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.