<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

<channel>
<title>Knights tour Prolog in ProLog users forum</title>
<link>http://www.dslreports.com/forum/r19848932</link>
<description></description>
<language>en</language>
<pubDate>Tue, 15 Dec 2009 06:53:57 EDT</pubDate>
<lastBuildDate>Tue, 15 Dec 2009 06:53:57 EDT</lastBuildDate>

<item>
<title>Knights tour Prolog</title>
<link>http://www.dslreports.com/forum/remark,19848932</link>
<description><![CDATA[<A HREF="/useremail/u/0"><b>anon</b></A> : Hello People,<br><br>I am currently trying to write a knights tour program on a 5 x 5 chessboard that visits every square at least <br><br>once. At the moment it only maps the path from 1 square to another and does not visit all squares on the <br><br>board.What I am trying to get the knight to do is visit all the squares rather than map a path from one square to <br><br>another. <br><br>I have tried to get the program to visit all the squares by using a list length that matches the number of squares <br><br>visited by the knight on the board i.e. 25 on 5 x 5 board. Once it has visited all squares it will output the <br><br>solution path on screen. Although at the moment it infinite loops and does not find a search result. I have also <br><br>tried to do this using depth_first and breadth_first search and I am still having the same problems.<br><br>A copy of code I have got so far can be found below, any help you could give would be much appreciated:<br><br>/* **************************************************************<br>Simplified Knight's tour - In this simplified version of the knight's<br>tour, a knight's path is built between any two squares on the chess board.<br>The main predicate,  path(X,Y), prints out a path between X and Y.<br><br>This version extends the initial representation<br> to a 5 x 5 grid, with the grid numbered as follows:<br><br>&#9;1  2  3  16 25<br>&#9;4  5  6  15 24<br>&#9;7  8  9  14 23<br>&#9;10 11 12 13 22<br>&#9;17 18 19 20 21<br><br>This version uses a recursive  path/3 predicate, with the third <br>argument representing the list of squares visited along the path:<br><br>  path(Start,End,List)<br><br>To run the program, type path(X,Y)<br><br>*************************************************************/<br><br>/* An exhaustive list of moves. These are moves for a 3 x 3 board */<br><br>move(2,9). move(2,7). move(3,4). move(3,8). move(4,9). move(4,3).<br>move(6,1). move(6,7). move(7,2). move(7,6). move(8,3). move(8,1).<br>move(9,2). move(9,4). move(1,6). move(1,8).  <br><br>/* The additional moves for the 4 x 4 board */<br><br>move(2,15). move(3,14). move(4,11). move(5,10). move(5,12). move(5,14).<br>move(5,16). move(6,11). move(6,13). move(7,12). move(8,15). move(8,13).<br>move(9,10). move(9,16). move(10,5). move(10,9). move(11,4). move(11,6).<br>move(11,14). move(12,5). move(12,7). move(12,15). move(13,8). move(13,6).<br>move(14,11). move(14,5). move(14,3). move(15,12). move(15,8). move(15,2).<br>move(16,5). move(16,9).<br><br>/* The additional moves for the 5 x 5 board */<br><br>move(3,24). move(6,23). move(6,25). move(7,18). move(8,17). move(8,19).<br>move(9,18). move(9,20). move(9,22). move(9,24). move(10,19). move(11,20).<br>move(12,17). move(12,21). move(12,23). move(13,18). move(13,24). move(14,19).<br>move(14,21). move(14,25). move(15,22). move(16,23). move(17,8). move(17,12).<br>move(18,7). move(18,9). move(18,13). move(19,8). move(19,10). move(19,14).  <br>move(19,22). move(20,9). move(20,11). move(20,23). move(21,12). move(21,14).<br>move(22,9). move(22,15). move(22,19). move(23,6). move(23,12). move(23,16).<br>move(23,20). move(24,3). move(24,9). move(24,13). move(25,6). move(25,14). <br><br>/*********** PROGRAM STARTS HERE ********************/<br><br>path(X,Y) :- path(X,Y,[X]).      /* Build a path between X and Y by<br>                                 calling path/3.                  */<br><br>path(Z,Z,L):- !,printlist(L),size(L,N), N = 25,nl, write(N). /* Base case: prints the solution */<br>path(X,Y,L):- move(X,Z),&#9;/* Recursive case: find a move from X to Z */ <br>      not(member(Z,L)),          /*   Cycle prevention */<br>      path(Z,Y,[Z|L]).&#9;         /*   Find a path from Z to Y */<br><br>                                 /* Print the list L in reverse order */    <br>printlist([]).                   <br>printlist([H|T]) :- printlist(T),nl,write(H).<br><br>                                  /* Basic utility predicates */<br><br>/*not(P):- call(P),!,fail,*/<br>/*not(P). */<br><br>/* member(H,[H|T]). */<br>/* member(M,[Y|T]) :- member(M,T). */<br><br>member(X,[X|T]).<br>member(X,[H|T]) :- member(X,T).<br><br>collect(L):-<br>&#9;retract(queue(X)),<br>&#9;!,<br>&#9;(X == bottom, !, L = [] <br>&#9;; L = [X | Rest], collect(Rest)).<br><br>findall(X, Goal, Xlist):-<br>&#9;call(Goal),<br>&#9;assertz(queue(X)),<br>&#9;fail;<br>&#9;assertz(queue(bottom)),<br>&#9;collect(Xlist).<br><br>append([],L,L). <br>append([X|L1],L2,[X|L12]) :- <br>&#9;append(L1,L2,L12). <br><br>next_node(Current, Next,Path) :-<br>&#9;move(Current, Next),<br>&#9;write('next node: '), write(Next), nl, <br>&#9;not(member(Next,Path)).<br><br>depth_first(Start, Goal, Path) :-<br>&#9;depth_first1(Start, Goal, [Start], Path),<br>&#9;size(Path, N),<br>&#9;N  25.<br><br>size([],0).<br>size([H|T],N):- size(T,N1),N is N1+1.<br>depth_first1(Current,Goal, _, [Goal]):-<br>&#9;size(Current,N),<br>&#9;/*size(Goal, N), */<br>&#9;N = 25. <br>depth_first1(Start, Goal, Visited, [Start | Path]) :-<br>&#9;Goal = 25,<br>&#9;next_node(Start, Next_node, Visited),<br>&#9;write('Visited: '), write(Visited), nl,<br>&#9;depth_first1(Next_node, Goal, [Next_node|Visited], Path), !.<br><br>breadth_first(Start, Goal, Path) :-<br>&#9;breadth_first1([[Start]], Goal, Path).<br><br>breadth_first1([[Goal|Path]|_], Goal, [Goal|Path]).<br><br>breadth_first1([[Current|Trail]|OtherPaths], Goal, Path) :-<br>&#9;Goal = 25,<br>&#9;findall([Next,Current|Trail], next_node(Current, Next, Trail), NewPaths),<br>&#9;write('New paths: '), write(NewPaths), nl,<br>&#9;append(OtherPaths, NewPaths, AllPaths),<br>&#9;breadth_first1(AllPaths, Goal, Path), !.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/remark,19848932</guid>
<pubDate>Mon, 21 Jan 2008 21:13:43 EDT</pubDate>
</item>

</channel>
</rss>
