% Domains % get the numbers from predicate board num(X) :- board(X). num(X-1) :- num(X), X>1. cell(X,Y) :- num(X), num(Y). number(X,Y) :- number(X,Y,N). neighbor(X,Y,X+1,Y,x) :- cell(X,Y), cell(X+1,Y), not number(X+1,Y). neighbor(X,Y,X-1,Y,x) :- cell(X,Y), cell(X-1,Y), not number(X-1,Y). neighbor(X,Y,X,Y+1,y) :- cell(X,Y), cell(X,Y+1), not number(X,Y+1). neighbor(X,Y,X,Y-1,y) :- cell(X,Y), cell(X,Y-1), not number(X,Y-1). neighbor(X1,Y1,X2,Y2) :- neighbor(X1,Y1,X2,Y2,D). % Generate { link(X1,Y1,X2,Y2) : neighbor(X1,Y1,X2,Y2) } <= 1 :- cell(X1,Y1). % Test :- number(X1,Y1), #count{ X2,Y2 : link(X1,Y1,X2,Y2) } = 0. from(X1,Y1) :- link(X1,Y1,X2,Y2), not number(X1,Y1). goto(X2,Y2) :- link(X1,Y1,X2,Y2). :- from(X,Y), not goto(X,Y). meeting(X,Y) :- goto(X,Y), not from(X,Y). :- #count{ X,Y : meeting(X,Y) } > 1. :- meeting(X2,Y2), #count{ X,Y : number(X,Y) } = M, not #count{ X1,Y1 : link(X1,Y1,X2,Y2) } = M. :- cell(X2,Y2), not meeting(X2,Y2), #count{ X1,Y1 : link(X1,Y1,X2,Y2) } > 1. reach(X,Y,X2,Y2) :- number(X,Y), link(X,Y,X2,Y2). reach(X,Y,X2,Y2) :- reach(X,Y,X1,Y1), link(X1,Y1,X2,Y2). :- number(X,Y), meeting(X2,Y2), not reach(X,Y,X2,Y2). :- goto(X2,Y2), #count{ X,Y : reach(X,Y,X2,Y2) } = 0. linked(X1,Y1,X2,Y2) :- link(X1,Y1,X2,Y2). linked(X2,Y2,X1,Y1) :- link(X1,Y1,X2,Y2), not number(X1,Y1). turn(X,Y) :- linked(X1,Y1,X,Y), linked(X2,Y2,X,Y), neighbor(X1,Y1,X,Y,x), neighbor(X2,Y2,X,Y,y), not meeting(X,Y). turn(X,Y,X2,Y2) :- reach(X,Y,X2,Y2), turn(X2,Y2). :- number(X,Y,N), not #count{ X2,Y2 : turn(X,Y,X2,Y2) } = N. % Display #show link/4.