Re: sudoku with swi-prolog
- From: "Rainer Hahnekamp" <rainer.hahnekamp@xxxxxx>
- Date: 8 Nov 2005 09:41:46 -0800
Hi,
I've also written my own version of sudoku. In my opinion it is very
different from the one posted above so I would appreciate it if you
could give me some comments - especially on the programming style
because I am not sure if I am using prolog the way it is meant to be:
data(['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']).
element(1). element(2). element(3).
element(4). element(5). element(6).
element(7). element(8). element(9).
set(Position, Value, [H|L1], [H|L2], CurPosition) :-
CurPosition \= Position,
NextPosition is CurPosition + 1,
set(Position, Value, L1, L2, NextPosition).
set(Position, Value, [_|L], [Value|L], Position).
set(_, _, [], _).
do :-
data(Data),
fillOut(1, Data, X, Data),
output(1, X),
!.
output(_, []).
output(Position, [H|L]) :-
X is Position mod 9,
X = 0,
write(H), writeln(' '),
NewPosition is Position + 1,
output(NewPosition, L).
output(Position, [H|L]) :-
X is Position mod 9,
X > 0,
write(H), write(' '),
NewPosition is Position + 1,
output(NewPosition, L).
fillOut(_, ResultList, ResultList, []).
fillOut(Position, List, ResultList, [Element|L]) :-
not(number(Element)),
element(X),
isPossible(X, Position, List),
set(Position, X, List, NewList, 1),
NewPosition is Position + 1,
fillOut(NewPosition, NewList, ResultList, L).
fillOut(Position, List, ResultList, [Element|L]) :-
number(Element),
NewPosition is Position + 1,
fillOut(NewPosition, List, ResultList, L).
isPossible(Value, Position, List) :-
getHorizontal(Position, List, Horiz),
not(member(Value, Horiz)),
getVertical(Position, List, Vertical),
not(member(Value, Vertical)),
getArea(Position, List, Area),
not(member(Value, Area)).
getHorizontal(Position, List, Horiz) :-
calcHoriz(Position, NewPosition),
Start is NewPosition - 1,
goForward(List, NewList, Start),
getForward(NewList, Horiz, 9).
getVertical(Position, List, [X1, X2, X3, X4, X5, X6, X7, X8, X9]) :-
calcVert(Position, NewPosition),
Start is NewPosition - 1,
goForward(List, [X1|List1], Start),
goForward(List1, [X2|List2], 8),
goForward(List2, [X3|List3], 8),
goForward(List3, [X4|List4], 8),
goForward(List4, [X5|List5], 8),
goForward(List5, [X6|List6], 8),
goForward(List6, [X7|List7], 8),
goForward(List7, [X8|List8], 8),
goForward(List8, [X9|_], 8).
getArea(Position, List, [X1, X2, X3, X4, X5, X6, X7, X8, X9]) :-
calcArea(Position, NewPosition),
Start is NewPosition - 1,
goForward(List, [X1|[X2|[X3|List1]]], Start),
goForward(List1, [X4|[X5|[X6|List2]]], 6),
goForward(List2, [X7|[X8|[X9|_]]], 6).
getForward(_, [], 0).
getForward([H|L1], [H|L2], Counter) :-
NewCounter is Counter - 1,
getForward(L1, L2, NewCounter).
goForward(Rest, Rest, 0).
goForward([_|L1], Rest, Step) :-
NewStep is Step - 1,
goForward(L1, Rest, NewStep).
calcHoriz(X, BeginX) :-
BeginX is X - (X-1) mod 9.
calcVert(X, BeginX) :-
Row is (X - 1) // 9,
BeginX is X - Row * 9.
calcArea(P, BeginP) :-
%X - Part
X is (P - 1) mod 3,
%Y - Part
Row is (P - 1) // 9,
Y is 9 * (Row mod 3),
BeginP is P - X - Y.
.
- References:
- sudoku with swi-prolog
- From: entsafter
- sudoku with swi-prolog
- Prev by Date: Re: sudoku with swi-prolog
- Next by Date: Re: sudoku with swi-prolog
- Previous by thread: Re: sudoku with swi-prolog
- Next by thread: Re: sudoku with swi-prolog
- Index(es):