Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:49:44+00:00 2026-05-29T11:49:44+00:00

I had written a Sudoku game including a solver in C, and wanted to

  • 0

I had written a Sudoku game including a solver in C, and wanted to try it out in Java so people could use it a little easier (portability). I figured the port would be fairly simple because of the vast similarities between the languages, but it seems it is being a bit of a pain.

My solver is recursing infinitely, which never happened in C. Here is my original C function for solving the puzzle:

int sudoku_solve(struct sudoku* sudoku)
{
    if(!sudoku) return 0;

    int mask = 0x1ff;
    int best_x = 0, best_y = 0;
    int best_mask = 0x2ff;


    for(int y = 0; y < 9; ++y){
        for(int x = 0; x < 9; ++x){
            if( sudoku->grid[y][x] != 0 ) continue;
            mask = sudoku_get_mask(sudoku, x, y);
            if( mask < best_mask ){
                best_mask = mask;
                best_x = x;
                best_y = y;
            }
        }
    }

    if( best_mask == 0x2ff ) return 1; // this puzzle is already solved!

    if( best_mask == 0x000 ) return 0; // this puzzle can't be solved!

    int start_c = rand() % 9;
    int c = start_c;
    do{
        if( (best_mask & (1<<c)) ){
            sudoku->grid[best_y][best_x] = c+1;
            if( sudoku_solve(sudoku) ) return 1;
        }
        c = (c+1) % 9;
    } while( c != start_c );

    sudoku->grid[best_y][best_x] = 0;


    return 0;
}

I know this isn’t necessarily the fastest or best written solver, but it worked. It simply finds the tile with the least possible values and then starts at a random values and tries all possible values until one results in a solvable puzzle (using recursion). The sudoku_get_mask returns an integer with the first 9 bits set for the corresponding value. It checks the horizontal, vertical, and sub-squares for values which are already used and removes them from the mask.

Now, here is the Java port:

public int Solve()
{
    int mask = 0x2FF;
    int bmask = 0x2FF, bx = 0, by = 0;

    for(int y = 0; y < 9; ++y){
        for(int x = 0; x < 9; ++x){
            if( grid[y][x] != 0 ) continue; // ignore spaces with values already set
            mask = GetMask(x, y);
            if( mask < bmask ) // less bits set == less possible choices
            {
                bmask = mask;
                bx = x;
                by = y;
            }
        }
    }

    if( bmask == 0x2FF ) // the puzzle had no good slots, it must be solved
        return 1;

    if( bmask == 0 ) // the puzzle is unsolvable
        return -1;

    int start_c = rand() % 9;
    int c = start_c;
    do{
        if( (bmask & (1<<c)) != 0 ){
            grid[by][bx] = (char) (c+1);
            if( Solve() == 1 ) return 1;
        }
        c = (c+1)%9;
    }while( c != start_c );

    grid[by][bx] = 0; // restore old value

    return 0;
}

They are almost identical, so I cannot figure out why the Java port is recursing infinitely! The solver should always either 1. find a solution or 2. find that there is no solution. By my logic, I can’t see a way it should recurse infinitely.

Here is the GetMask Java code:

protected int GetMask(int x, int y)
{
    int mask = 0x1FF;
    for(int cx = 0; cx < 9; ++cx){
        mask &= (grid[y][cx] == 0 ? mask : ~(1 << (grid[y][cx]-1)));
    }
    for(int cy = 0; cy < 9; ++cy){
        mask &= (grid[cy][x] == 0 ? mask : ~(1 << (grid[cy][x]-1)));
    }
    int idx = squareIndex[y][x];
    int[] pt = null;
    for(int c = 0; c < 9; ++c){
        pt = squarePoint[idx][c];
        mask &= (grid[pt[1]][pt[0]] == 0 ? mask : ~(1 << (grid[pt[1]][pt[0]]-1)));
    }
    return mask;
}

Here is squareIndex, and squarePoint (just lookup tables for the sub-squares):

static int squareIndex[][] = {
    {0,0,0,1,1,1,2,2,2},
    {0,0,0,1,1,1,2,2,2},
    {0,0,0,1,1,1,2,2,2},
    {3,3,3,4,4,4,5,5,5},
    {3,3,3,4,4,4,5,5,5},
    {3,3,3,4,4,4,5,5,5},
    {6,6,6,7,7,7,8,8,8},
    {6,6,6,7,7,7,8,8,8},
    {6,6,6,7,7,7,8,8,8}
};

static int[] squarePoint[][] = {
    { {0,0}, {1,0}, {2,0}, {0,1}, {1,1}, {2,1}, {0,2}, {1,2}, {2,2} },
    { {3,0}, {4,0}, {5,0}, {3,1}, {4,1}, {5,1}, {3,2}, {4,2}, {5,2} },
    { {6,0}, {7,0}, {8,0}, {6,1}, {7,1}, {8,1}, {6,2}, {7,2}, {8,2} },
    { {0,3}, {1,3}, {2,3}, {0,4}, {1,4}, {2,4}, {0,5}, {1,5}, {2,5} },
    { {3,3}, {4,3}, {5,3}, {3,4}, {4,4}, {5,4}, {3,5}, {4,5}, {5,5} },
    { {6,3}, {7,3}, {8,3}, {6,4}, {7,4}, {8,4}, {6,5}, {7,5}, {8,5} },
    { {0,6}, {1,6}, {2,6}, {0,7}, {1,7}, {2,7}, {0,8}, {1,8}, {2,8} },
    { {3,6}, {4,6}, {5,6}, {3,7}, {4,7}, {5,7}, {3,8}, {4,8}, {5,8} },
    { {6,6}, {7,6}, {8,6}, {6,7}, {7,7}, {8,7}, {6,8}, {7,8}, {8,8} }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:49:45+00:00Added an answer on May 29, 2026 at 11:49 am

    I guess Mister Smith isn’t going to submit an official answer (I figured I’d let him for the points).

    the problem was that the std C function rand() returns integers in the range: [0,INT_MAX] and the Java function Randomizer.nextInt() is in the range [INT_MIN,INT_MAX]. I had to replace “generator.nextInt() % 9” with “generator.randInt(9)” and it worked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to use qDebug in the Qt unit testing, i had written the
I had earlier written a program in java which takes in as input a
I had started working on Eclipse IDe 3.6. I had written a normal java
I had written this code out using Adobe CS3 and have recently upgraded to
The program above is, as said, a Sudoku game. I have written down ideas
I had written a function to generate a word document by using the Office
I had written a jquery code to hide some labels, textbox contained in tables
I had written an event handler for MouseMove for my form but When I
I had written a program in Python 3, but now want to convert it
I had written a small utility app just for my phone, which stopped the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.