Computer Science Canada

Sudoku Solver

Author:  Raknarg [ Mon Mar 19, 2012 1:14 pm ]
Post subject:  Sudoku Solver

As Aange10 was trying to figure out how to make a program, I decided to try it out for him. You can try putting in your own puzzle if you want. Save the text file and the program in the same spot, and call the text file "stest.txt", or name it whatever you want and change it in the program. If you make your own, make it exactly 81 numbers, and leave all empty squares as 0. Here's an example that works:

1 0 4 0 0 3 0 0 2
2 0 0 1 0 4 0 0 3
3 0 0 2 0 0 1 0 4
4 1 0 3 0 0 2 0 0
0 2 0 4 1 0 3 0 0
0 3 0 0 2 0 4 1 0
0 4 1 0 3 0 0 2 0
0 0 2 0 4 1 0 3 0
0 0 3 0 0 2 0 4 1

Also, if anyone actually wants this documented, as it's probably incomprehensible, just ask and I'll reupload it.

Author:  Raknarg [ Mon Mar 19, 2012 1:17 pm ]
Post subject:  RE:Sudoku Solver

Also, if anyone tries something complex and it doesnt work, let me know. I've only tried for simple problems like the one up there.

EDIT: @Aange10 sorry about the topic name, i forgot that was what yours was named Razz

Also I should mention this line here: sudoku_solver (map, 2, 1, 0, 1)
the first two numbers are the initial x and y coordinates. If you custom make a map, that coordinate should start on a zero. The grid is set up the same way as a regular graph, where up and right are positive.

Author:  mirhagk [ Mon Mar 19, 2012 8:21 pm ]
Post subject:  RE:Sudoku Solver

AI Escargot is solved by it, so this is successful. Now the next step, develop soduku's that are extremely difficult to solve.

Author:  Aange10 [ Mon Mar 19, 2012 11:55 pm ]
Post subject:  RE:Sudoku Solver

@Raknarg


No hard feelings! :3

Author:  Raknarg [ Tue Mar 20, 2012 9:10 am ]
Post subject:  RE:Sudoku Solver

@mirghak thanks, I'll look into that.

Author:  Yves [ Wed Mar 21, 2012 12:22 am ]
Post subject:  Re: Sudoku Solver

Here's a puzzle your code marks as unsolvable:

0 0 0 0 0 0 4 1 0
9 0 0 3 0 0 0 0 0
3 0 0 0 5 0 0 0 0
0 4 8 0 0 7 0 0 0
0 0 0 0 0 0 0 5 2
0 1 0 0 0 0 0 0 0
6 0 0 2 0 0 0 0 5
0 7 0 0 0 0 8 0 0
0 0 0 0 9 0 0 0 0

It is in fact solvable (though extremely difficult!); the only solution is:

8 5 7 9 6 2 4 1 3
9 2 4 3 1 8 5 6 7
3 6 1 7 5 4 2 9 8
5 4 8 1 2 7 9 3 6
7 9 3 4 8 6 1 5 2
2 1 6 5 3 9 7 8 4
6 8 9 2 7 1 3 4 5
1 7 5 6 4 3 8 2 9
4 3 2 8 9 5 6 7 1

By the by, if you encounter an unsolvable sudoku, it should only print it once then quit instead of endlessly outputting "Unsolvable" Wink

Oh, and your program should detect illegal puzzles; for instance, the following puzzle gets "solved":

1 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 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

Author:  Dreadnought [ Wed Mar 21, 2012 12:37 am ]
Post subject:  Re: Sudoku Solver

Yves wrote:
Here's a puzzle your code marks as unsolvable

Actually, the program does solve it eventually (took my computer ~30-40 seconds).

The spam of "unsolvable" is the program trying various combinations and deciding whether it is a solution (perhaps "unsolvable" is not the correct word to use in this case).

Author:  Raknarg [ Wed Mar 21, 2012 8:59 am ]
Post subject:  RE:Sudoku Solver

Haha yeah, I forgot to change that unsolvable thing Razz
It was because in all my programs, it became unsolvable after a certain amount of time because there was a bug I fixed later... Just take it out I guess.

Author:  evildaddy911 [ Thu Mar 22, 2012 3:40 pm ]
Post subject:  RE:Sudoku Solver

it keeps asking me for keyboard input instead of reading the file

Author:  Dreadnought [ Thu Mar 22, 2012 4:19 pm ]
Post subject:  Re: Sudoku Solver

Just hit space then enter.

Author:  Raknarg [ Fri Mar 23, 2012 1:05 pm ]
Post subject:  RE:Sudoku Solver

Oh yeah, I forgot one line of code...

loop
exit when eof (stream) or y = 0
get : stream, map (x, y)
x += 1
if x > 9 then
x := 1
y -= 1
end if
end loop

I used eof instead of eof (stream). If you use eof (stream) instead, it wont ask for input.

Author:  evildaddy911 [ Fri Mar 23, 2012 3:00 pm ]
Post subject:  RE:Sudoku Solver

ok, now it works.
i also added a cls right before it displays the completed sudoku; it was overlaping the "Unsolvable"s

i tried a different one and it stopped working.... see for yourself

Author:  Dreadnought [ Fri Mar 23, 2012 8:32 pm ]
Post subject:  Re: Sudoku Solver

That is an illegal sudoku, you have two eights in the 8th column from the left.

3 0 9 0 0 0 0 0 0
4 8 0 2 0 0 3 0 0
0 0 0 0 9 3 0 8 0
0 0 0 0 0 0 0 8 0
0 0 0 5 7 1 0 0 0
5 0 2 0 0 0 0 0 0
0 9 0 1 6 0 0 0 0
0 0 5 0 0 9 0 3 7
0 0 0 0 0 0 1 0 4

Author:  evildaddy911 [ Sat Mar 24, 2012 10:37 am ]
Post subject:  RE:Sudoku Solver

Oh, so i wrote it down wrong... Copied the hardest puzzle from my 1 book
Ok, ill fix it...


: