Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Sudoku

  1. #1
    Paragon is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Sudoku

    Hello, the thing my coding for sudoku is not working for few inputs... it works fine with all its value initially at 0, but when i place numbers more than 4 at random places it stops responding (it doesn't show any value).

    Please Help.

    my assignment is to get a solved sudoku for these values:
    Java Code:
    //Sample Input:
                {0,2,7,3,8,0,0,1,0},
                {0,1,0,0,0,6,7,3,5},
                {0,0,0,0,0,0,0,2,9},
                {3,0,5,6,9,2,0,8,0},
                {0,0,0,0,0,0,0,0,0},
                {0,6,0,1,7,4,5,0,3},
                {6,4,0,0,0,0,0,0,0},
                {9,5,1,8,0,0,0,7,0},
                {0,8,0,0,6,5,3,4,0}
    My Current code

    Java Code:
    public class Sudoku {
        static int userGrid[][]=new int[][]
               {{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,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,0,0,0,0,0}};//[horizontal][vertical]
            static int grid[][]=new int[9][9];//the grid that the program experiments on
        public static void main(String[] args) {
            for (int i=0;i<9;i++) {
                System.arraycopy(userGrid[i], 0, grid[i], 0, 9);
            }
            print(grid);
            print(loop(0, 0, grid));
           }
        public static int[][] loop(int y, int x, int[][] grid) {
                    while(!validity(8, 8, grid) || grid[8][8]==0)//while not solved
            {
                if (userGrid[y][x]!=0) {
                    int yy, xx;
                    if (x==8) 
                    {
                        yy=y+1; 
                        xx=0;
                    } 
                    else 
                    {
                        yy=y;
                        xx=x+1;
                    }
                    loop(yy, xx, grid);
                } else {
                    if (grid[y][x]<9) {//going forward
                        grid[y][x]++;
                        if (validity(y, x, grid)) {
                            int yy, xx;
                            if (x==8) 
                            {
                                yy=y+1; 
                                xx=0;
                            } 
                            else 
                            {
                                yy=y; 
                                xx=x+1;
                            }
                            loop(yy, xx, grid);
                        }
                    } else {
                        grid[y][x]=0;
                        break;
                    }
                }
            }
            return grid;
        }
        public static boolean validity(int x, int y, int[][] grid) {
            String temp="";
            for (int i=0;i<9;i++) {
                temp+=Integer.toString(grid[i][y]);//horizontal check
                temp+=Integer.toString(grid[x][i]);//verical check
                temp+=Integer.toString(grid[(x/3)*3+i/3][(y/3)*3+i%3]);//square check
            }
            int count=0, idx=0;
            while ((idx=temp.indexOf(Integer.toString(grid[x][y]), idx))!=-1)
            {idx++; count++;}
            return count==3;
        }
        public static void print(int[][] grid) {
            System.out.println();
            for (int i=0;i<9;i++) {
                for (int j=0;j<9;j++) {
                    System.out.print(grid[i][j]);
                }
                System.out.println();
            }
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Sudoku

    Crossposted: Sudoku
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Sudoku

    Quote Originally Posted by KevinWorkman View Post
    Crossposted: Sudoku
    Whaddya know
    Sudoku (Beginning Java forum at JavaRanch)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Paragon is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Sudoku

    Quote Originally Posted by KevinWorkman View Post
    Crossposted: Sudoku
    My apologies, i didn't know that i will get response on every sight, what i had in my mind was that SOMEONE will help me (didnt know i will get help from every site.) I'm going to close this topic here and will discuss my problem where i first posted.
    Thanks for letting me know about Cross posting, im new to the forums thing, and new to java, that's why i didnt know about it.

  5. #5
    Paragon is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Sudoku

    and a request, close this thread, please.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Sudoku

    Quote Originally Posted by Paragon View Post
    im new to the forums thing, and new to java, that's why i didnt know about it.
    You assume too much. Nobody is judging you here, just post the links to duplicate questions yourself the next time. That's a minimal effort on your part (annoying, but minimal) and it helps the people you reach out to a whole lot. I hope you can reason why it is an important common courtesy to make.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Sudoku

    w.r.t. The subject: the 'validity( ... )' method stinks: just checking if a value occurs three times in a row, column or minor square is not correct (e.g. the same value occurs three times on consecutive locations). Also, during 'building' a solution, a value may not occur three times in a row, column etc. yet ... in short: that method stinks.

    kind regards,

    Jos

    edit: I take that back: the method only checks if a certain number occurs once in a row, column or minor square; in short: I goofed.
    Last edited by JosAH; 04-23-2015 at 10:19 AM.
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Paragon is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Sudoku creator
    By bob 6 in forum New To Java
    Replies: 4
    Last Post: 08-23-2012, 06:43 PM
  2. Sudoku checker
    By kennemercollege in forum New To Java
    Replies: 3
    Last Post: 04-27-2011, 12:41 PM
  3. Traversing through a sudoku grid.
    By Lil_Aziz1 in forum New To Java
    Replies: 2
    Last Post: 05-30-2010, 06:05 PM
  4. Sudoku randomizer help
    By mochibon in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 03:05 AM
  5. SUDOKU game problem
    By bumblyb33 in forum AWT / Swing
    Replies: 2
    Last Post: 05-05-2009, 11:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •