the_dawn 0 Newbie Poster

been going through the 8 game puzzle from Ivan Bratko's book there is a section I just cant wrap my head around....


% Manhattan Distance heuristic
manhattan(State, Number) :-
manh(State,State,0,Number).
manh([], _, X, X).
manh([H|T], State, Acc, Result) :-
nth1(Position, State, H),
NewPos is Position - 1,
Xaux1 is NewPos mod 3,
X1 is integer(Xaux1),
Y1 is NewPos // 3,
goal(Goal),
nth1(GoalPosition, Goal, H),
NewGPos is GoalPosition - 1,
Xaux2 is NewGPos mod 3,
X2 is integer(Xaux2),
Y2 is NewGPos // 3,
S1 is abs(X1-X2),
S2 is abs(Y1-Y2),
N is S1+S2,
NewAcc is Acc+N,
manh(T, State, NewAcc, Result).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.