Hi guys, I am quite new to this forum but ive been asking around everywhere and no one can help me solve my problem.
I had a crazy urge to make a sudoku solver that can output the answer to every possible answer to any kind of sudoku it is given, including a blank board.
So pretty well I wanted to make a program that could create every possible answer to a blank sudoku board.
I also wanted to make it super efficient and spent hours upon hours simplifying until I came to a near completed solution in almost 100 lines.
My issue is that the recursion process isn't working, even thought it really seems like it should. Please try and fix it, I commented and formated as best I could to make it simple to solve.
Thank you in advance! ~Raginpirate
P.S. if you scroll to the bottom of the program you can see a detail description of what is causing the error of this program. I however have no idea how to fix it.
class ReSu { // PLEASE HELP ME public static void main (String [] args) { recur(input()); } //Gets the input, for now I just want this program to work with a blank board, so I just have it return all 0's in the array. public static int[][] input() { int [][] sudoku = new int [9][9]; return sudoku; } //The main recursion method. Something about this and startRecursion is causing my issue of leaking values. public static void recur(int [][] sudoku) { //Simple array to hold the return value of checkSolveFill which tells me if the board is solvable / works as a sudoku (checked[0]) and if the board is filled in completely (checked[1]) boolean [] checked = new boolean [2]; checked = checkSolveFill(sudoku); //If the board is both a valid sudoku and is completely filled in it outputs this board as a final answer if (checked[0] && checked[1]) finalOutput(sudoku); // If it is not completely filled in but still a valid (or doesnt have any issues being a sudoku) sudoku it will start the recursion process if (checked[0]) startRecursion(sudoku); } //This checks to see if the sudoku board is valid or filled in. Do not worry this works fine, I have tested this several times public static boolean[] checkSolveFill(int [][] sudoku) { int [][][] rowColumnBoxPossible = new int [3][9][9]; boolean [] solveFill = new boolean [2]; solveFill[0] = true; solveFill[1] = true; for (int x = 0; x<9; x++) { for (int y = 0; y<9; y++) { //If the spot on the board is not filled in it will set the later of the two return values to be false if (sudoku[x][y]>0) { //If the spot on the board has a value it will check if the row, column, and box it belongs to contains this value, and if it does the sudoku is not valid and the first of the two return values will be false if (rowColumnBoxPossible[0][x][sudoku[x][y]-1]==0 && rowColumnBoxPossible[1][y][sudoku[x][y]-1]==0 && rowColumnBoxPossible[2][boxChecker(x, y)][sudoku[x][y]-1]==0) { //If it did not have the value in all three, it will then set the section of all three arrays to not be zero. Trust me this works, its hard to think about though. rowColumnBoxPossible[0][x][sudoku[x][y]-1]=1; rowColumnBoxPossible[1][y][sudoku[x][y]-1]=1; rowColumnBoxPossible[2][boxChecker(x, y)][sudoku[x][y]-1]=1; } else { solveFill[0] = false; } } else { solveFill[1] = false; } } } return solveFill; } //Simple box check method done by looking at coordinates public static int boxChecker(int x, int y) { if (x<3) { if (y<3) return 0; if (y<6) return 1; return 2; } if (x<6) { if (y<3) return 3; if (y<6) return 4; return 5; } if (y<3) return 6; if (y<6) return 7; return 8; } //Outputs the sudoku public static void finalOutput(int [][] sudoku) { System.out.println(); for (int x = 0; x<9; x++) { for (int y = 0; y<9; y++) { System.out.print(sudoku[x][y] + " "); } System.out.println(); } } //My main problem. This method should work, but my understanding of recursion might be lacking. public static void startRecursion(int [][] sudoku) { int lastCoord=0; //First it finds all blank spots on the board and remebers the last one it found. for (int x = 0; x<9; x++) { for (int y = 0; y<9; y++) { if (sudoku[x][y] == 0) lastCoord = x*10+y; } } //It then reruns the whole program with this spot in the sudoku being all the values between 1-9. for (int z = 1; z<10; z++) { sudoku[lastCoord/10][lastCoord%10] = z; recur(sudoku); } } } /*If you try running this program however it will do nothing. * I have no idea how to fix this issue, because my recursion is kind of "leaking over" * the program works like this: * It finds a spot on the board which is blank and guesses 1-9 on it * it then checks to see if the sudoku is still solvable and if it is does the same guessing method again * if it is not solvable that pathway should end, and it should try the previous recursion pathway with its next number * for example: * bottom row of a blank sudoku 000000021 * it would then guess 000000121 * it would then realise this cant work and try 000000221 * it would then realise this cant work and try 000000321 * it would then continue on to guess 000001321 because this pathway worked and so on... * but here is where the program no longer works * when we reach this: (as it is the simplist pathway for the program to reach) * 009321654 * 987654321 * The program should revert to the last possible pathway and become: * 000421654 * 987654321 * however it "leaks" the value 9 into the last pathway and instead does this: * 009421654 * 987654321 * the program realises this cant work and clocks from 4-9 and then repeats the same issue with the next step and so on * until the program finally ends when it becomes * 009999999 * 999999999 * Instead of reseting when it moves back one pathway it carrys the previous recursive pathway backwards and I cannot * figure out why. Please help me and I hope I did the best I could to explain why it doesnt work!