Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with this Sudoku code.

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with this Sudoku code.

    I can run the code but it does not solve, I can not figure it out. Thx

    public class SudokuTestSolver {
    int Unassigned = 0;
     
     
    public boolean solveSudoku(int[][] grid) {
    	int x = 0;
    	int y = 0;
     
    	// If there is no unassigned location, we are done
    	if (!findUnassignedLocation(grid, x, y)) {
    	   return true;
    	}
     
    	for (int num = 1; num <= 9; num++) {
     
    		if (isSafe(grid, x, y, num)) {
     
    			grid[x][y] = num;
     
    			if (solveSudoku(grid)) {
    				return true;
    			}
     
    			grid[x][y] = Unassigned;
    		}
     
    	}
    	return false; 
    }
     
     
     
    public boolean findUnassignedLocation(int[][] grid, int x, int y) {
    	for (int i = 0; i < 9; i++) {
    		for (int j = 0; j < 9; j++) {
    			if (grid[x][y] == 0) {
    				return true;   
    			}
    		}
    	}
    	return false;
     
    }
     
    public boolean usedInRow(int[][] grid, int x, int num) {
    	for (int y = 0; y < 9; y++) {
    		if (grid[x][y] == num) {
    			return true;
    		}
    	}
    	return false;
    }
     
     
     
    public boolean usedInCol(int[][] grid, int y, int num) {
    	for (int x = 0; x < 9; x++) {
    		if (grid[x][y] == num) {
    			return true;
    		}
    	}
    	return false;
    }
     
     
    public boolean usedInBox(int[][] grid, int boxStartRow, int boxStartCol, int num) {
    	for (int x = 0; x < 3; x++) {
    		for (int y = 0; y < 3; y++) {
    			if (grid[x + boxStartRow * 3][y + boxStartCol * 3] == num) {
    				return true;
    			}
    		}
    	}
    	return false;
    }
     
     
     
    public boolean isSafe(int grid[][], int x, int y, int num) {
     
        return !usedInRow(grid, x, num) && 
               !usedInCol(grid, y, num) && 
               !usedInBox(grid, x - x%3 , y - y%3, num);
    }
     
     
    public void printGrid(int grid[][]) {
        for (int x = 0; x < 9; x++) {
           for (int y = 0; y < 9; y++)
              System.out.print(grid[x][y] + "  ");
            System.out.println();
        }
    }
     
     
    public static void main(String[] args) {
                  int grid[][] = {{3, 0, 0,  0, 0, 0,  0, 0, 0},
                                  {0, 0, 0,  0, 0, 0,  0, 0, 0},
                                  {0, 0, 0,  1, 0, 0,  0, 0, 0},
     
                                  {0, 0, 0,  0, 0, 0,  0, 0, 0},
                                  {0, 0, 0,  0, 0, 0,  0, 0, 0},
                                  {0, 0, 0,  0, 0, 0,  0, 0, 0},
     
                                  {0, 0, 0,  0, 0, 0,  2, 0, 0},
                                  {0, 0, 0,  0, 0, 0,  0, 0, 0},
                                  {0, 0, 0,  0, 0, 0,  0, 0, 0}};
     
    SudokuTestSolver s = new SudokuTestSolver();
     
     
     
    int grid2[][] = new int[9][9];
     
     
    for (int x = 0; x < 9; x++) {
       for (int y = 0; y < 9; y++) {
          grid2[x][y] = grid[x][y];
       }
    }
     
    if (s.solveSudoku(grid2) == true) {
     
       s.printGrid(grid2);
    }
    else {
       System.out.println("Can not be solved"); 
    }  
     
    }//end main
     
     
    }//end class

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with this Sudoku code.

    it does not solve
    Why do you think the code should be able to solve?

    If you think the code is designed to use a good algorithm, how are you trying to debug the code to see if it is following the design?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help With Sudoku GUI
    By Ezra Zike in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 26th, 2014, 04:46 AM
  2. Sudoku
    By Puleng in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2014, 03:50 AM
  3. Sudoku
    By davasile in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2013, 12:18 AM
  4. Help with turning Sudoku into Magic Sudoku
    By Murlio in forum Java Theory & Questions
    Replies: 1
    Last Post: November 4th, 2012, 02:49 PM
  5. Sudoku
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2009, 10:09 PM