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: Sudoko grid conversion from 9*9 to all grid size

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

    Default Sudoko grid conversion from 9*9 to all grid size

    I have coded for Sudoku puzzle in java.The thing is my code has limitation for giving inputs for 9*9 grid.How do I make my code adaptable for all the grids.Please have patience.I am new to java.

    What changes do I need to make so that the code can run on all grid sizes?The grid is square not a rectangle.

    class Solution {
        public void solveSudoku(char[][] board) {
            if(solveSudoku2(board)) {
                return;
            }
        }
     
        public boolean solveSudoku2(char[][] board) {
            boolean isEmpty = true;
            int row = -1;
            int col = -1;
            int n = board.length;
     
            //this code is used to check if there exists any empty cell in sudoku board
            //if there is any empty cell, that means we are not done yet and we need to solve it further,
            // so we cannot return true at any point until all the cells are full
            //by empty cell, I mean cells having '.' as the value
            for(int i = 0; i < board.length; i++) {
                for(int j = 0; j < board[0].length; j++) {
                   if(board[i][j] == '.') {
                       row = i;
                       col = j;
                       isEmpty = false;
                       break;
                   }
                }
                if(!isEmpty) {
                    break;
                }
            }
     
            if(isEmpty) {
                return true;
            }
     
            //loop for all the numbers and start placing in the empty cells
            //numbers start from 1 to n
     
            for(int num = 1; num <= n; num++) {
                //convert number to char
                char char_num = (char)(num + '0');
                //check if the number we are adding satisfies all the sudoku rules,
                // if it does, then we place that number in the cell
                if(checkSafe(board,char_num,row,col)) {
                    board[row][col] = (char)(num + '0');
     
                    //using this number in place row,col, we check for all the other empty places and see if the board is returning true or not
                    // if the board is not filled that means that we need to use other number in row,col place.
                    //hence backtrack.
                    if(solveSudoku2(board)) {
                        return true;
                    } else {
                        board[row][col] = '.';
                    }
                }
            }
            return false;
        }
     
        public boolean checkSafe(char[][] board, char num, int row, int col) {
            //checkk if num is present in the row
            for(int i = 0; i< board.length; i++ ) {
                if(board[row][i] == num) {
                    return false;
                }
            }
     
            for(int j = 0; j < board[0].length; j++) {
                if(board[j][col] == num) {
                    return false;
                }
            }
     
            int checknum = (int)Math.sqrt(board.length);
            //check for the current grid. grid will be basically checknum*checknum matrix. where every matrix will start from startrow to  startrow + checknum having checknum length.
            // so, we we have row = 0, then matrix will start from 0 to 2, i.e. the first 3x3 matrix.
            // however, we have row = 2, then also the matrix will start from 0 to 2 - the first 3x3 matrix.
            //however, if row = 3, then we will start our matrix from 3 and cotinute upto 5.
     
            int startrow = row - row % checknum;
            int startcol = col - col % checknum;
            for(int k = startrow; k < startrow + checknum; k++) {
                for(int l = startcol; l < startcol + checknum; l++) {
                    if(board[k][l] == num) {
                        return false;
                    }   
                }
            }
            return true;
        }
    }
    Last edited by Norm; February 22nd, 2019 at 06:10 PM. Reason: Changed QUOTE tags to CODE tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Sudoko grid conversion from 9*9 to all grid size

    Also posted here: https://www.dreamincode.net/forums/t...all-grid-size/
    and here: https://coderanch.com/t/706644/java/...sion-grid-size
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. A grid game
    By LuluMM in forum Paid Java Projects
    Replies: 3
    Last Post: April 3rd, 2012, 12:44 PM
  2. Replies: 1
    Last Post: December 12th, 2011, 05:03 AM
  3. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  4. [SOLVED] Grid Pattern problem:
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 30th, 2010, 06:45 AM
  5. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 08:53 PM