Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sudoku creator in Prolog

303 views
Skip to first unread message

abhi

unread,
Jan 21, 2010, 3:49:25 AM1/21/10
to
Hallo ,
can anyone help me out in creating a 9 * 9 Sudoku with prolog and
write in on file .
Would be great if anyone can post me the source code.
Thanks all

abhi

unread,
Jan 21, 2010, 3:56:44 AM1/21/10
to

I can only find sudoku solver , but i need a simple creator !

Martin Riener

unread,
Jan 21, 2010, 4:50:31 AM1/21/10
to

What about a generate and test pattern? Just fill in an empty 9x9 field
randomly, at each step check if there is only one solution left.

hth Martin

Wit Jakuczun

unread,
Jan 21, 2010, 5:03:40 AM1/21/10
to
On 21 Sty, 10:50, Martin Riener <martin.rie...@gmail.com> wrote:

> > I can only find sudoku solver , but i need a simple creator !
>
> What about a generate and test pattern? Just fill in an empty 9x9 field
> randomly, at each step check if there is only one solution left.
>

findall with existing solver should work better.

Best regards,
Wit Jakuczun, http://wlogsolutions.com

abhi

unread,
Jan 21, 2010, 5:49:00 AM1/21/10
to

I really have no idea how to write it in prolog , i did it like this
in java .. can you please give sample code for generate and test
pattern in prolog :)

int[][] best = new int[N][N];
int bestDifficulty = 0;
int bestGiven = N*N+1;
int bestTry = 0;

int tries = 0;
// Generate 100 puzzles and pick the most difficult one
while(tries < 100) {
countGiven = 0;
difficulty = 0;
// Clean the given numbers, but keep the grid intact
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
this.given[i][j] = 0;
}
}

// Select 17 random numbers to show
// 17 is an absolute minimum. Any less and the puzzle is
garanteed to be unsolvable.
this.randomGiven(17);
difficulty = this.solvable();

// Keep adding numbers until the puzzle is solvable
while(difficulty==0) {
this.addRandomGiven();
difficulty = this.solvable();
}
if(countGiven <= bestGiven) {
// If this puzzle is better then anything we've found so
far, store it
bestGiven = countGiven;
bestDifficulty = difficulty;
bestTry = tries;
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
best[i][j] = this.given[i][j];
}
}
if(bestGiven < 35 && bestDifficulty > 960)
break; // Good enough. Stop searching.
}
tries++;
}

// Restore the best grid:
countGiven = bestGiven;
difficulty = bestDifficulty;
if(this.output)
System.out.println("Puzzle "+bestTry+" is best");

String s = "" ;
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
this.given[i][j] = best[i][j];
}
}
for(int x = 0 ;x <= 8 ; x++) {
s+="\n";
for(int i = 0 ; i <= 8 ; i++)
s += best[i][x];

}

Ulrich Neumerkel

unread,
Jan 21, 2010, 8:36:22 AM1/21/10
to

Take an existing Sudoku solver.

Find somehow (e.g. labeling "randomly") a valid solution.

While there is a unique solution: generalize the Sudoku
(replacing an entry by a free new variable).

That is: after each step of generalization you have to
solve again the Sudoku to ensure that there is still
a unique solution.

Chip Eastham

unread,
Jan 21, 2010, 9:21:26 AM1/21/10
to
On Jan 21, 5:49 am, abhi <abhishek9...@gmail.com> wrote:

>
> I really have no idea how to write it in prolog , i did it like this
> in java .. can you please give sample code for  generate and test
> pattern in prolog :)
>
>                 int[][] best = new int[N][N];
>                 int bestDifficulty = 0;
>                 int bestGiven = N*N+1;
>                 int bestTry = 0;
>
>         int tries = 0;
>         // Generate 100 puzzles and pick the most difficult one
>                 while(tries < 100) {
>                         countGiven = 0;
>                         difficulty = 0;
>                         // Clean the given numbers, but keep the grid intact
>                 for(int i=0; i<N; i++) {
>                     for(int j=0; j<N; j++) {
>                         this.given[i][j] = 0;
>                     }
>                 }
>
>           // Select 17 random numbers to show
>           // 17 is an absolute minimum. Any less and the puzzle is
> garanteed to be unsolvable.

[snip]

Hi, abhi:

Since you say you wrote this Java program, let me
ask about the comment that 17 is "an absolute
minimum." Do you have a reference for this?
I'm aware that 17-cell Sudoku exist in some
profusion, and that catalogs of the known
examples have been systematically tested to
see if any of them could be reduced to 16
cells having yet a unique solution, without
success. But I'm not aware of any proof
(by exhaustive search or otherwise) that no
16-cell Sudoku exists.

Also, it might be helpful if you explained
in more detail what the Prolog Sudoko solver
available to you does. I'd be a bit leary
of wrapping a "Sudoku generator" around such
a solver if possibly the solver code does
not terminate when presented with a puzzle
for which no solution or multiple solutions
exist. It would be interesting as well to
know what representation is used for the
solver, so that suggestions about generating
inputs could be more relevant.

regards, chip

A.L.

unread,
Jan 21, 2010, 10:05:30 AM1/21/10
to

What university?

A.L.

al.g...@hotmail.com

unread,
Apr 27, 2017, 12:18:42 AM4/27/17
to
Hi..

Excuse me..
I want the code for sudoku game in Amzi prolog..

please help me..
0 new messages