-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysudoku.pl
70 lines (55 loc) · 998 Bytes
/
mysudoku.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
isDistinct([]).
isDistinct([H|T]):-
\+member1(H,T),
isDistinct(T).
isDistinct([H|T]):-
member1(H,T),
!,
fail.
member1(_,[]):-
fail.
member1(X,[H|T]):-
\+var(H),
X = H,!.
member1(X,[_|T]):-
member1(X,T).
checkIfRowsDistinct([]).
checkIfRowsDistinct([H|T]):-
isDistinct(H),
checkIfRowsDistinct(T).
noFree([]).
noFree([H|T]):-
\+var(H),
noFree(T).
allAssigned([]).
allAssigned([Row|Rows]):-
noFree(Row),
allAssigned(Rows).
sudoku(Rows):-
member(X,[1,2,3,4,5,6]),
bind(X,Rows),
checkIfRowsDistinct(Rows),
transpose(Rows,Columns),
checkIfRowsDistinct(Columns),
%R = [A,B,C,D,E,F],
%blocks(A,B),blocks(C,D),blocks(E,F),
%blockCheck(Rows),
( allAssigned(Rows)
-> write(Rows)
; sudoku(Rows)
).
blockCheck([A,B,C,D,E,F]):-
blocks(A,B),
blocks(C,D),
blocks(E,F).
blocks([],[]).
blocks([A,B,C|Bs1],[D,E,F|Bs2]):-
isDistinct([A,B,C,D,E,F]),
blocks(Bs1,Bs2).
bind(X,[Row|Rows]):-
bind1(X,Row).
bind1(X,[H|T]):-
var(H),
H is X.
bind1(X,[_|T]):-
bind1(X,T).