shinta vroeg mij de oplossing van de sudoku. Eerst een disclaimer: het ding werkt ni zo goe, en is slecht geprogrammeert, dus geen gezaag van: da's verkeerd
![Very Happy :D](./images/smilies/icon_biggrin.gif)
Code: Select all
uniek(X,[]).
uniek(X, [H|T]) :- (X =\= H), uniek(X,T).
checkLine([]).
checkLine([X | Rest]) :-
checkLine(Rest),
member(X,[1,2,3,4,5,6,7,8,9]),
uniek(X, Rest).
checkLines([]).
checkLines([X | Rest]) :-
checkLines(Rest),
checkLine(X).
turnAround([[],[],[],[],[],[],[],[],[]],[]).
turnAround([[A|AS],[B|BS],[C|CS],[D|DS],[E|ES],[F|FS],[G|GS],[H|HS],[I|IS]], X) :-
turnAround([AS,BS,CS,DS,ES,FS,GS,HS,IS], Y),
append([[A,B,C,D,E,F,G,H,I]],Y,X).
checkSudoku(Sud) :-
checkLines(Sud),
turnAround(Sud,RevSud),
checkLines(RevSud),
checkBlocks(Sud).
checkBlocks(Sud) :-
Sud = [
[S11,S12,S13,S14,S15,S16,S17,S18,S19],
[S21,S22,S23,S24,S25,S26,S27,S28,S29],
[S31,S32,S33,S34,S35,S36,S37,S38,S39],
[S41,S42,S43,S44,S45,S46,S47,S48,S49],
[S51,S52,S53,S54,S55,S56,S57,S58,S59],
[S61,S62,S63,S64,S65,S66,S67,S68,S69],
[S71,S72,S73,S74,S75,S76,S77,S78,S79],
[S81,S82,S83,S84,S85,S86,S87,S88,S89],
[S91,S92,S93,S94,S95,S96,S97,S98,S99]
],
checkLine([S11,S12,S13,S21,S22,S23,S31,S32,S33]),
checkLine([S41,S42,S43,S51,S52,S53,S61,S62,S63]),
checkLine([S71,S72,S73,S81,S82,S83,S91,S92,S93]),
checkLine([S14,S15,S16,S24,S25,S26,S34,S35,S36]),
checkLine([S44,S45,S46,S54,S55,S56,S64,S65,S66]),
checkLine([S74,S75,S76,S84,S85,S86,S94,S95,S96]),
checkLine([S17,S18,S19,S27,S28,S29,S37,S38,S39]),
checkLine([S47,S48,S49,S57,S58,S59,S67,S68,S69]),
checkLine([S77,S78,S79,S87,S88,S89,S97,S98,S99]).
solve(Sud) :-
checkSudoku(Sud),
pretty_print(Sud).
pretty_print([]).
pretty_print([H|T]) :-
write(H),nl,
pretty_print(T).
testSudoku2 :- solve([
[8,6,1,4,5,9,7,2,3],
[_,5,_,3,1,7,6,_,8],
[_,9,_,6,_,2,_,1,4],
[2,_,_,8,3,5,4,7,9],
[9,8,4,2,7,6,1,3,5],
[3,_,5,1,_,_,_,6,2],
[5,4,7,9,2,1,3,8,6],
[1,_,9,5,6,8,_,4,7],
[6,2,8,7,4,3,9,5,1]
]).