Created
March 29, 2023 12:21
-
-
Save afvanwoudenberg/d1f2c1721df7762c665af9252e72f1bb to your computer and use it in GitHub Desktop.
A Prolog solver for the Skyline puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% A Prolog solver for the Skyline puzzle | |
% http://www.constantin-jean-clau.de/ | |
print_solution(X,Y) :- solve(X,Y,Sol), print_board(Sol). | |
pos(X,Y,_) :- member(X,[1,2,3,4,5,6,7]), member(Y,[1,2,3,4,5,6,7]). | |
board(Board) :- findall(pos(X,Y,_),pos(X,Y,_),Board). | |
solve(X,Y,Board) :- | |
board(Board), | |
member(pos(X,Y,' '),Board), | |
solve(1,1,Board,[]). | |
solve(7,7,_,_) :- !. | |
solve(8,Y,Board,Placed) :- | |
Yn is Y + 1, | |
solve(1,Yn,Board,Placed), !. | |
solve(X,Y,Board,Placed) :- | |
member(pos(X,Y,V),Board), | |
nonvar(V), | |
Xn is X + 1, | |
solve(Xn,Y,Board,Placed). | |
solve(X,Y,Board,Placed) :- | |
member(pos(X,Y,V),Board), | |
var(V), | |
member(Piece,[i,g,h,f,e,d,a,c,b]), | |
not(member(Piece,Placed)), | |
piece(Piece,Locs), | |
place_piece(Piece,X,Y,Locs,Board), | |
Xn is X + 1, | |
solve(Xn,Y,Board,[Piece|Placed]). | |
print_board(Board) :- | |
write('+-------+'), nl, | |
findall(_,(member(Y,[1,2,3,4,5,6,7]),print_line(Y,Board)),_), | |
write('+-------+'), nl. | |
print_line(Y,Board) :- | |
write('|'), | |
findall(_,(member(X,[1,2,3,4,5,6,7]),print_piece(X,Y,Board)),_), | |
write('|'), | |
nl. | |
print_piece(X,Y,Board) :- | |
member(pos(X,Y,P),Board), | |
not(var(P)), | |
write(P), !. | |
print_piece(_,_,_) :- | |
write('_'). | |
place_piece(_,_,_,[],_). | |
place_piece(Piece,X0,Y0,[(Xd,Yd)|Locs],Board) :- | |
X is X0 + Xd, X > 0, X =< 7, | |
Y is Y0 + Yd, Y > 0, Y =< 7, | |
member(pos(X,Y,Piece),Board), | |
place_piece(Piece,X0,Y0,Locs,Board). | |
piece(a,[(0,0),(0,1),(0,2),(0,3),(0,4)]). | |
piece(a,[(0,0),(1,0),(2,0),(3,0),(4,0)]). | |
piece(b,[(0,0),(1,0)]). | |
piece(b,[(0,0),(0,1)]). | |
piece(c,[(0,0),(-1,1),(0,1),(1,1)]). | |
piece(c,[(0,0),(1,0),(1,1),(2,0)]). | |
piece(c,[(0,0),(0,1),(-1,1),(0,2)]). | |
piece(c,[(0,0),(0,1),(1,1),(0,2)]). | |
piece(d,[(0,0),(0,1),(1,1),(1,2),(2,2)]). | |
piece(d,[(0,0),(1,0),(-1,1),(0,1),(-1,2)]). | |
piece(d,[(0,0),(1,0),(1,1),(2,1),(2,2)]). | |
piece(d,[(0,0),(-1,1),(0,1),(-2,2),(-1,2)]). | |
piece(e,[(0,0),(0,1),(0,2),(0,3),(1,1)]). | |
piece(e,[(0,0),(1,0),(2,0),(3,0),(2,1)]). | |
piece(e,[(0,0),(0,1),(0,2),(0,3),(-1,2)]). | |
piece(e,[(0,0),(-1,1),(0,1),(1,1),(2,1)]). | |
piece(f,[(0,0),(-1,1),(0,1),(-2,2),(-1,2),(0,2)]). | |
piece(f,[(0,0),(0,1),(1,1),(0,2),(1,2),(2,2)]). | |
piece(f,[(0,0),(1,0),(2,0),(0,1),(1,1),(0,2)]). | |
piece(f,[(0,0),(1,0),(2,0),(1,1),(2,1),(2,2)]). | |
piece(g,[(0,0),(1,0),(0,1),(1,1),(2,1),(0,2),(1,2)]). | |
piece(g,[(0,0),(1,0),(2,0),(0,1),(1,1),(2,1),(1,2)]). | |
piece(g,[(0,0),(1,0),(-1,1),(0,1),(1,1),(0,2),(1,2)]). | |
piece(g,[(0,0),(-1,1),(0,1),(1,1),(-1,2),(0,2),(1,2)]). | |
piece(h,[(0,0),(0,1),(0,2),(0,3),(1,1),(1,2)]). | |
piece(h,[(0,0),(1,0),(2,0),(3,0),(1,1),(2,1)]). | |
piece(h,[(0,0),(-1,1),(0,1),(-1,2),(0,2),(0,3)]). | |
piece(h,[(0,0),(1,0),(-1,1),(0,1),(1,1),(2,1)]). | |
piece(i,[(0,0),(-2,1),(-1,1),(0,1),(-2,2),(-1,2),(0,2),(-1,3)]). | |
piece(i,[(0,0),(1,0),(-1,1),(0,1),(1,1),(0,2),(1,2),(2,2)]). | |
piece(i,[(0,0),(-1,1),(0,1),(1,1),(-1,2),(0,2),(1,2),(-1,3)]). | |
piece(i,[(0,0),(1,0),(2,0),(1,1),(2,1),(3,1),(1,2),(2,2)]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment