Solving the Numberlink puzzle with prolog -


i have assignment seems out of scope of class (i because barely taught prolog), have write prolog program solve game "flow free" on android. in assignment called numberlink. solve in c++ in hour because i'm not familiar prolog giving me trouble. here's do:

  1. make list holds boolean indicate whether has been visited or used.
  2. recursively search possible paths given starting point end point using breadth first search find shortest paths.
  3. go there guess.

my attempt included searching web on how make list. of course prolog not documented @ anywhere came blank , gave up. friend told me use maplist don't understand how use make list including need.

thanks in advance.

edit: link, looking make 2d list represent board being played on. function this:

makelist(size, list):-

where size integer representing size of 1 dimension in square list ex. (7x7).

here's implementation of @capellic's solution. code self-explanatory. 2 blocks connected if adjacent , have same color, or adjacent connected block of same color. (i used x , y instead of row , column, made writing conditions @ end little confusing.)

solving in swi-prolog

enter image description here

https://flowfreesolutions.com/solution/?game=flow&pack=green&set=5&level=1

connected(p1, p2, m, visited) :-     adjacent(p1, p2),     maplist(dif(p2), visited),     color(p1, c, m),     color(p2, c, m). connected(p1, p2, m, visited) :-     adjacent(p1, p3),     maplist(dif(p3), visited),     color(p1, c, m),     color(p3, c, m),     connected(p3, p2, m, [p3|visited]).  adjacent(p(x,y1), p(x,y2)) :- y2 y1+1. adjacent(p(x,y1), p(x,y2)) :- y2 y1-1. adjacent(p(x1,y), p(x2,y)) :- x2 x1+1. adjacent(p(x1,y), p(x2,y)) :- x2 x1-1.  color(p(x,y), c, m) :-     nth1(y, m, r),     nth1(x, r, c).  sol(m) :-     m = [[1,_,_,_,1],          [2,_,_,_,_],          [3,4,_,4,_],          [_,_,_,_,_],          [3,2,5,_,5]],     connected(p(1,1), p(5,1), m, [p(1,1)]),     connected(p(1,2), p(2,5), m, [p(1,2)]),     connected(p(1,3), p(1,5), m, [p(1,3)]),     connected(p(2,3), p(4,3), m, [p(2,3)]),     connected(p(3,5), p(5,5), m, [p(3,5)]). 

sample query:

?- sol(m). m = [[1, 1, 1, 1, 1],      [2, 2, 2, 2, 2],       [3, 4, 4, 4, 2],      [3, 2, 2, 2, 2],       [3, 2, 5, 5, 5]]. 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -