I have a 2D array, which represents a Sudoku game.
I'm trying to check the game for errors as with a typical Sudoku game:
No number (1-9) is repeated within a row, column or 3x3 square. No empty cells.
I'm very new to Java, so I had a limited knowledge to approach this with. I was going to try a long if, else statement comparing all the cells. That didn't work, because -1 was repeated. (-1 represents an empty square). I tried to get around this, but realized that this if statement was too messy and there had to be a better way.
I now think the best way to do it would be with a nested for loop to go through each row, column, and 3x3 square. Checking for empty cells on the other hand, I think I've figured out. (nested for statement checking for -1 within the 2D array).
I was also thinking of just adding up all the numbers in a row, col, or 3x3 square, and if it doesn't equal 45 then have the game remain incomplete?
As far as checking repeat values, I'm not sure how to implement the nested for.
EDIT: Let me clarify a bit, I don't really want to check for repeat values per say, I just want the game to remain incomplete if there is a repeat value. (e.g. Allow repeat values, just doesn't win you the game as with a real Sudoku puzzle). I feel like the adding to 45 method would work best.
public boolean hasDuplicates(int... numbers). If you can implement it just once, then it's just a matter of changing what you pass in for a column, row, or 3x3 square. Give that a shot, and show what you've tried.