Re: sudoku with swi-prolog
- From: "Martin Sondergaard" <nobody@xxxxxxxxxxx>
- Date: Fri, 18 Nov 2005 15:14:53 -0000
<entsafter@xxxxxxxxx> wrote in message
news:1131326792.525373.16320@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> hi folks,
>
> it is 2 o'clock in the morning and i'm also trying to solve sudoku with
> swi-prolog.
>
> after a nice copy & paste - search & replace session, i think, i did i
> finally. but now my nice little swi-prolog tells me, that sudoku/81 is
> not defined. hmpf.
>
> i can't manage it, to attach the source code as a file, so i will post
> it here:
>
> %sudoku with prolog
>
> number(1).
> number(2).
> number(3).
> number(4).
> number(5).
> number(6).
> number(7).
> number(8).
> number(9).
>
> sudoku(A1,A2,A3,A4,A5,A6,A7,A8,A9,B1,B2,B3,B4,B5,B6,B7,B8,B9,C1,C2,C3,C4,C5,C6,C7,C8,C9,D1,D2,D3,D4,D5,D6,D7,D8,D9,E1,E2,E3,E4,E5,E6,E7,E8,E9,F1,F2,F3,F4,F5,F6,F7,F8,F9,G1,G2,G3,G4,G5,G6,G7,G8,G9,H1,H2,H3,H4,H5,H6,H7,H8,H9,I1,I2,I3,I4,I5,I6,I7,I8,I9):-
>
I think the problem is that you have used
a space between the word "suduku" and the "(".
This is a common error, for beginners in Prolog.
Also, I think there is a space between the ":" and the "-".
There should be a space before it instead.
Like this...
sudoku(A1, A2, ...etc... I9) :-
Thats the main problem.
Now I will look at the rest of your program...
> number(A1),
> number(A2),
> number(A3),
> number(A4),
> number(A5),
> number(A6),
> number(A7),
> number(A8),
> number(A9),
>
> number(B1),
> number(B2),
> number(B3),
> number(B4),
> number(B5),
> number(B6),
> number(B7),
> number(B8),
> number(B9),
>
> number(C1),
> number(C2),
> number(C3),
> number(C4),
> number(C5),
> number(C6),
> number(C7),
> number(C8),
> number(C9),
>
> number(D1),
> number(D2),
> number(D3),
> number(D4),
> number(D5),
> number(D6),
> number(D7),
> number(D8),
> number(D9),
>
> number(E1),
> number(E2),
> number(E3),
> number(E4),
> number(E5),
> number(E6),
> number(E7),
> number(E8),
> number(E9),
>
> number(F1),
> number(F2),
> number(F3),
> number(F4),
> number(F5),
> number(F6),
> number(F7),
> number(F8),
> number(F9),
>
> number(G1),
> number(G2),
> number(G3),
> number(G4),
> number(G5),
> number(G6),
> number(G7),
> number(G8),
> number(G9),
>
> number(H1),
> number(H2),
> number(H3),
> number(H4),
> number(H5),
> number(H6),
> number(H7),
> number(H8),
> number(H9),
>
> number(I1),
> number(I2),
> number(I3),
> number(I4),
> number(I5),
> number(I6),
> number(I7),
> number(I8),
> number(I9),
>
> A1\=A2, A1\=A3, A1\=A4, A1\=A5, A1\=A6, A1\=A7, A1\=A8, A1\=A9,
> A2\=A3, A2\=A4, A2\=A5, A2\=A6, A2\=A7, A2\=A8, A2\=A9,
> A3\=A4, A3\=A5, A3\=A6, A3\=A7, A3\=A8, A3\=A9,
> A4\=A5, A4\=A6, A4\=A7, A4\=A8, A4\=A9,
> A5\=A6, A5\=A7, A5\=A8, A5\=A9,
> A6\=A7, A6\=A8, A6\=A9,
> A7\=A8, A7\=A9,
> A8\=A9,
>
Thats good.
I like the way you have laid that out,
its easy to see how it works.
I would normally prefer to put spaces around
the "\=" operator.
These spaces are not essential for SWI Prolog to work,
but you should normally use spaces,
they will make your code easier to read.
At least, they usually do.
Like this :
B1 \= B2, B1 \= B3, B1 \= B4, B1 \= B5, B1 \= B6, B1 \= B7, B1 \= B8, B1 \=
B9,
> B1\=B2, B1\=B3, B1\=B4, B1\=B5, B1\=B6, B1\=B7, B1\=B8, B1\=B9,
> B2\=B3, B2\=B4, B2\=B5, B2\=B6, B2\=B7, B2\=B8, B2\=B9,
> B3\=B4, B3\=B5, B3\=B6, B3\=B7, B3\=B8, B3\=B9,
> B4\=B5, B4\=B6, B4\=B7, B4\=B8, B4\=B9,
> B5\=B6, B5\=B7, B5\=B8, B5\=B9,
> B6\=B7, B6\=B8, B6\=B9,
> B7\=B8, B7\=B9,
> B8\=B9,
>
> C1\=C2, C1\=C3, C1\=C4, C1\=C5, C1\=C6, C1\=C7, C1\=C8, C1\=C9,
> C2\=C3, C2\=C4, C2\=C5, C2\=C6, C2\=C7, C2\=C8, C2\=C9,
> C3\=C4, C3\=C5, C3\=C6, C3\=C7, C3\=C8, C3\=C9,
> C4\=C5, C4\=C6, C4\=C7, C4\=C8, C4\=C9,
> C5\=C6, C5\=C7, C5\=C8, C5\=C9,
> C6\=C7, C6\=C8, C6\=C9,
> C7\=C8, C7\=C9,
> C8\=C9,
>
> D1\=D2, D1\=D3, D1\=D4, D1\=D5, D1\=D6, D1\=D7, D1\=D8, D1\=D9,
> D2\=D3, D2\=D4, D2\=D5, D2\=D6, D2\=D7, D2\=D8, D2\=D9,
> D3\=D4, D3\=D5, D3\=D6, D3\=D7, D3\=D8, D3\=D9,
> D4\=D5, D4\=D6, D4\=D7, D4\=D8, D4\=D9,
> D5\=D6, D5\=D7, D5\=D8, D5\=D9,
> D6\=D7, D6\=D8, D6\=D9,
> D7\=D8, D7\=D9,
> D8\=D9,
>
> E1\=E2, E1\=E3, E1\=E4, E1\=E5, E1\=E6, E1\=E7, E1\=E8, E1\=E9,
> E2\=E3, E2\=E4, E2\=E5, E2\=E6, E2\=E7, E2\=E8, E2\=E9,
> E3\=E4, E3\=E5, E3\=E6, E3\=E7, E3\=E8, E3\=E9,
> E4\=E5, E4\=E6, E4\=E7, E4\=E8, E4\=E9,
> E5\=E6, E5\=E7, E5\=E8, E5\=E9,
> E6\=E7, E6\=E8, E6\=E9,
> E7\=E8, E7\=E9,
> E8\=E9,
>
> F1\=F2, F1\=F3, F1\=F4, F1\=F5, F1\=F6, F1\=F7, F1\=F8, F1\=F9,
> F2\=F3, F2\=F4, F2\=F5, F2\=F6, F2\=F7, F2\=F8, F2\=F9,
> F3\=F4, F3\=F5, F3\=F6, F3\=F7, F3\=F8, F3\=F9,
> F4\=F5, F4\=F6, F4\=F7, F4\=F8, F4\=F9,
> F5\=F6, F5\=F7, F5\=F8, F5\=F9,
> F6\=F7, F6\=F8, F6\=F9,
> F7\=F8, F7\=F9,
> F8\=F9,
>
> G1\=G2, G1\=G3, G1\=G4, G1\=G5, G1\=G6, G1\=G7, G1\=G8, G1\=G9,
> G2\=G3, G2\=G4, G2\=G5, G2\=G6, G2\=G7, G2\=G8, G2\=G9,
> G3\=G4, G3\=G5, G3\=G6, G3\=G7, G3\=G8, G3\=G9,
> G4\=G5, G4\=G6, G4\=G7, G4\=G8, G4\=G9,
> G5\=G6, G5\=G7, G5\=G8, G5\=G9,
> G6\=G7, G6\=G8, G6\=G9,
> G7\=G8, G7\=G9,
> G8\=G9,
>
> H1\=H2, H1\=H3, H1\=H4, H1\=H5, H1\=H6, H1\=H7, H1\=H8, H1\=H9,
> H2\=H3, H2\=H4, H2\=H5, H2\=H6, H2\=H7, H2\=H8, H2\=H9,
> H3\=H4, H3\=H5, H3\=H6, H3\=H7, H3\=H8, H3\=H9,
> H4\=H5, H4\=H6, H4\=H7, H4\=H8, H4\=H9,
> H5\=H6, H5\=H7, H5\=H8, H5\=H9,
> H6\=H7, H6\=H8, H6\=H9,
> H7\=H8, H7\=H9,
> H8\=H9,
>
> I1\=I2, I1\=I3, I1\=I4, I1\=I5, I1\=I6, I1\=I7, I1\=I8, I1\=I9,
> I2\=I3, I2\=I4, I2\=I5, I2\=I6, I2\=I7, I2\=I8, I2\=I9,
> I3\=I4, I3\=I5, I3\=I6, I3\=I7, I3\=I8, I3\=I9,
> I4\=I5, I4\=I6, I4\=I7, I4\=I8, I4\=I9,
> I5\=I6, I5\=I7, I5\=I8, I5\=I9,
> I6\=I7, I6\=I8, I6\=I9,
> I7\=I8, I7\=I9,
> I8\=I9,
>
Thats all the horizontal lines done.
They all look correct to me.
Now the vertical lines...
> A1\=B1, A1\=C1, A1\=D1, A1\=E1, A1\=F1, A1\=G1, A1\=H1, A1\=I1,
> B1\=C1, B1\=D1, B1\=E1, B1\=F1, B1\=G1, B1\=H1, B1\=I1,
> C1\=D1, C1\=E1, C1\=F1, C1\=G1, C1\=H1, C1\=I1,
> D1\=E1, D1\=F1, D1\=G1, D1\=H1, D1\=I1,
> E1\=F1, E1\=G1, E1\=H1, E1\=I1,
> F1\=G1, F1\=H1, F1\=I1,
> G1\=H1, G1\=I1,
> H1\=I1,
>
> A2\=B2, A2\=C2, A2\=D2, A2\=E2, A2\=F2, A2\=G2, A2\=H2, A2\=I2,
> B2\=C2, B2\=D2, B2\=E2, B2\=F2, B2\=G2, B2\=H2, B2\=I2,
> C2\=D2, C2\=E2, C2\=F2, C2\=G2, C2\=H2, C2\=I2,
> D2\=E2, D2\=F2, D2\=G2, D2\=H2, D2\=I2,
> E2\=F2, E2\=G2, E2\=H2, E2\=I2,
> F2\=G2, F2\=H2, F2\=I2,
> G2\=H2, G2\=I2,
> H2\=I2,
>
> A3\=B3, A3\=C3, A3\=D3, A3\=E3, A3\=F3, A3\=G3, A3\=H3, A3\=I3,
> B3\=C3, B3\=D3, B3\=E3, B3\=F3, B3\=G3, B3\=H3, B3\=I3,
> C3\=D3, C3\=E3, C3\=F3, C3\=G3, C3\=H3, C3\=I3,
> D3\=E3, D3\=F3, D3\=G3, D3\=H3, D3\=I3,
> E3\=F3, E3\=G3, E3\=H3, E3\=I3,
> F3\=G3, F3\=H3, F3\=I3,
> G3\=H3, G3\=I3,
> H3\=I3,
>
> A4\=B4, A4\=C4, A4\=D4, A4\=E4, A4\=F4, A4\=G4, A4\=H4, A4\=I4,
> B4\=C4, B4\=D4, B4\=E4, B4\=F4, B4\=G4, B4\=H4, B4\=I4,
> C4\=D4, C4\=E4, C4\=F4, C4\=G4, C4\=H4, C4\=I4,
> D4\=E4, D4\=F4, D4\=G4, D4\=H4, D4\=I4,
> E4\=F4, E4\=G4, E4\=H4, E4\=I4,
> F4\=G4, F4\=H4, F4\=I4,
> G4\=H4, G4\=I4,
> H4\=I4,
>
> A5\=B5, A5\=C5, A5\=D5, A5\=E5, A5\=F5, A5\=G5, A5\=H5, A5\=I5,
> B5\=C5, B5\=D5, B5\=E5, B5\=F5, B5\=G5, B5\=H5, B5\=I5,
> C5\=D5, C5\=E5, C5\=F5, C5\=G5, C5\=H5, C5\=I5,
> D5\=E5, D5\=F5, D5\=G5, D5\=H5, D5\=I5,
> E5\=F5, E5\=G5, E5\=H5, E5\=I5,
> F5\=G5, F5\=H5, F5\=I5,
> G5\=H5, G5\=I5,
> H5\=I5,
>
> A6\=B6, A6\=C6, A6\=D6, A6\=E6, A6\=F6, A6\=G6, A6\=H6, A6\=I6,
> B6\=C6, B6\=D6, B6\=E6, B6\=F6, B6\=G6, B6\=H6, B6\=I6,
> C6\=D6, C6\=E6, C6\=F6, C6\=G6, C6\=H6, C6\=I6,
> D6\=E6, D6\=F6, D6\=G6, D6\=H6, D6\=I6,
> E6\=F6, E6\=G6, E6\=H6, E6\=I6,
> F6\=G6, F6\=H6, F6\=I6,
> G6\=H6, G6\=I6,
> H6\=I6,
>
> A7\=B7, A7\=C7, A7\=D7, A7\=E7, A7\=F7, A7\=G7, A7\=H7, A7\=I7,
> B7\=C7, B7\=D7, B7\=E7, B7\=F7, B7\=G7, B7\=H7, B7\=I7,
> C7\=D7, C7\=E7, C7\=F7, C7\=G7, C7\=H7, C7\=I7,
> D7\=E7, D7\=F7, D7\=G7, D7\=H7, D7\=I7,
> E7\=F7, E7\=G7, E7\=H7, E7\=I7,
> F7\=G7, F7\=H7, F7\=I7,
> G7\=H7, G7\=I7,
> H7\=I7,
>
> A8\=B8, A8\=C8, A8\=D8, A8\=E8, A8\=F8, A8\=G8, A8\=H8, A8\=I8,
> B8\=C8, B8\=D8, B8\=E8, B8\=F8, B8\=G8, B8\=H8, B8\=I8,
> C8\=D8, C8\=E8, C8\=F8, C8\=G8, C8\=H8, C8\=I8,
> D8\=E8, D8\=F8, D8\=G8, D8\=H8, D8\=I8,
> E8\=F8, E8\=G8, E8\=H8, E8\=I8,
> F8\=G8, F8\=H8, F8\=I8,
> G8\=H8, G8\=I8,
> H8\=I8,
>
> A9\=B9, A9\=C9, A9\=D9, A9\=E9, A9\=F9, A9\=G9, A9\=H9, A9\=I9,
> B9\=C9, B9\=D9, B9\=E9, B9\=F9, B9\=G9, B9\=H9, B9\=I9,
> C9\=D9, C9\=E9, C9\=F9, C9\=G9, C9\=H9, C9\=I9,
> D9\=E9, D9\=F9, D9\=G9, D9\=H9, D9\=I9,
> E9\=F9, E9\=G9, E9\=H9, E9\=I9,
> F9\=G9, F9\=H9, F9\=I9,
> G9\=H9, G9\=I9,
> H9\=I9,
>
Thats all the vertical lines done.
I think they were all correct.
But I think this bit is wrong...
> A1\=B2, A1\=C2, A1\=B3, A1\=C3,
> B1\=A2, B1\=C2, B1\=A3, B1\=C3,
> C1\=A2, C1\=B2, C1\=A3, C1\=B3,
> A2\=B3, A2\=C3,
> B2\=A3, B2\=C3,
> C2\=A3, C2\=B3,
You must have been trying to make the
9 squares in the top-left-hand corner
all have different numbers.
But I think that it is wrong.
I think that needs to be like this...
A1 \= A2, A1 \= A3,
A1 \= B1, A1 \= B2, A1 \= B3,
A1 \= C1, A1 \= C2, A1 \= C3,
A2 \= A1, A2 \= A3,
A2 \= B1, A2 \= B2, A2 \= B3,
A2 \= C1, A2 \= C2, A2 \= C3,
A3 \= ...etc.
B1\= A1, B1 \= A2,B1 \= A3,
B1 \= ...
B1 \= ...
B2 ...
....
B3 ...
....
C1 ...
C2 ...
C3 ... .
In the lines I have just written, there is some duplication of previous
lines, (e.g. "A1 \= A2" was done previously).
But if you keep these lines here, that will make it easier to read,
so that you can see if your code is correct.
It needs to be complete.
I think the rest of these are also wrong...
>
> A4\=B5, A4\=C5, A4\=B6, A4\=C6,
> B4\=A5, B4\=C5, B4\=A6, B4\=C6,
> C4\=A5, C4\=B5, C4\=A6, C4\=B6,
> A5\=B6, A5\=C6,
> B5\=A6, B5\=C6,
> C5\=A6, C5\=B6,
>
> A7\=B8, A7\=C8, A7\=B9, A7\=C9,
> B7\=A8, B7\=C8, B7\=A9, B7\=C9,
> C7\=A8, C7\=B8, C7\=A9, C7\=B9,
> A8\=B9, A8\=C9,
> B8\=A9, B8\=C9,
> C8\=A9, C8\=B9,
>
> D1\=E2, D1\=F2, D1\=E3, D1\=F3,
> E1\=D2, E1\=F2, E1\=D3, E1\=F3,
> F1\=D2, F1\=E2, F1\=D3, F1\=E3,
> D2\=E3, D2\=F3,
> E2\=D3, E2\=F3,
> F2\=D3, F2\=E3,
>
> D4\=E5, D4\=F5, D4\=E6, D4\=F6,
> E4\=D5, E4\=F5, E4\=D6, E4\=F6,
> F4\=D5, F4\=E5, F4\=D6, F4\=E6,
> D5\=E6, D5\=F6,
> E5\=D6, E5\=F6,
> F5\=D6, F5\=E6,
>
> D7\=E8, D7\=F8, D7\=E9, D7\=F9,
> E7\=D8, E7\=F8, E7\=D9, E7\=F9,
> F7\=D8, F7\=E8, F7\=D9, F7\=E9,
> D8\=E9, D8\=F9,
> E8\=D9, E8\=F9,
> F8\=D9, F8\=E9,
>
> G1\=H2, G1\=I2, G1\=H3, G1\=I3,
> H1\=G2, H1\=I2, H1\=G3, H1\=I3,
> I1\=G2, I1\=H2, I1\=G3, I1\=H3,
> G2\=H3, G2\=I3,
> H2\=G3, H2\=I3,
> I2\=G3, I2\=H3,
>
> G4\=H5, G4\=I5, G4\=H6, G4\=I6,
> H4\=G5, H4\=I5, H4\=G6, H4\=I6,
> I4\=G5, I4\=H5, I4\=G6, I4\=H6,
> G5\=H6, G5\=I6,
> H5\=G6, H5\=I6,
> I5\=G6, I5\=H6,
>
> G7\=H8, G7\=I8, G7\=H9, G7\=I9,
> H7\=G8, H7\=I8, H7\=G9, H7\=I9,
> I7\=G8, I7\=H8, I7\=G9, I7\=H9,
> G8\=H9, G8\=I9,
> H8\=G9, H8\=I9,
> I8\=G9, I8\=H9,
>
The next bit looks correct.
> % that's it ;)
> % now the numbers:
>
> B1=8,
> C1=1,
> D1=7,
> G1=3,
> H1=4,
>
> A2=6,
> B2=4,
> E2=3,
> H2=2,
> I2=5,
>
> A3=2,
> D3=6,
> F3=1,
> I3=9,
>
> A4=3,
> C4=9,
> E4=2,
> G4=8,
>
> A5=1,
> B5=7,
> D5=8,
> F5=3,
> H5=5,
> I5=4,
>
> A6=8,
> C6=4,
> E6=7,
> G6=6,
>
> A7=5,
> D7=2,
> F7=7,
> I7=3,
>
> A8=4,
> B8=9,
> E8=1,
> H8=6,
> I8=7,
>
> B9=2,
> C9=3,
> F9=6,
> G9=5,
> H9=1.
>
>
Thats good.
>
> sorry for that, but i don't know how to do it in a different way.
>
No, that is the right way to do it, that is a good design.
I'm very experienced with Prolog, and that is how I would do it.
If you try to do it using lists, it may get complicated,
and it may become harder to do. You might never get it working.
You method is good, it uses "pure" Prolog,
and is likely to work correctly.
Its good to get practice using pure Prolog;
get good at it before you start to use lists.
(One person posted a different solution to this thread,
which uses lists. It looks very complicated,
and will probably not work.)
Once you can use lists, you can make use of them sometimes,
if they will make things easier. But try to keep things simple.
Here is a predicate that you could define
that you could use in your sudoku program.
Its called "all_different/9".
It can be used like this...
?- all_different( 1, 2, 3, 4, 5, 6, 7, 8, 9 ).
yes
?- all_different( 1, 2, 1, 4, 5, 6, 7, 8, 9 ).
no
?-
You can use this predicate like this :
sudoku( ... ) :-
number(A1),
number(A2),
number(A3),
...etc...
all_different( A1, A2, A3, A4, A5, A6, A7, A8, A9 ),
all_different( B1, B2, B3, B4, B5, B6, B7, B8, B9 ),
all_different( C1, C2, C3, C4, C5, C6, C7, C8, C9 ),
...etc...
% Now the verticals.
all_different( A1, B1, B1, D1, E1, F1, G1, H1, I1 ),
all_different( A2, ...etc... ),
% Then the 3 by 3 squares.
all_different( A1, A2, A3, B1, B2, B3, C1, C2, C3 ),
...etc...
.
> i would be deeply grateful, if someone could explain to me, what i did
> wrong.
It was the space between "sudoku" and the "(".
This is a common error for beginners.
> i admit, my way of doing is not so stylish, like Neng-Fa Zhou did it,
> but i'm just a student and in my it-course we solved these logicals and
> we did it that way, so just wanted to try, whether it works or not.
>
> nevertheless i thank everyone who will help me!
>
> greetings from germany,
>
> maddin
>
Greetings from London, in the U.K.
--
Martin Sondergaard.
.
- Follow-Ups:
- Re: sudoku with swi-prolog
- From: Jan Wielemaker
- Re: sudoku with swi-prolog
- References:
- sudoku with swi-prolog
- From: entsafter
- sudoku with swi-prolog
- Prev by Date: Prolog Server Pages
- Next by Date: Marketing promotions
- Previous by thread: Re: Sudoku with SWI Prolog
- Next by thread: Re: sudoku with swi-prolog
- Index(es):
Relevant Pages
|
|