0

I'm very new to prolog and am having trouble with the 8 puzzle heuristics. I'm not sure how to compare the two lists (goal and start) and figure out my h value. What I am trying to do is send compare_list two list the goal and start. It should compare each head and if they are not equal add 1 to the total else call it's self recursively. I'm not sure how to do if else statements in prolog.

%T1,T2,T3 top
%M1,M2,M3 middle
%B1,B2,B3 bottom

goal([1/2/3/8/0/4/7/6/5]).

%Top row movements
%shift right
shift([T1/0/T3/M1/M2/M3/B1/B2/B3],
      [0/T1/T3/M1/M2/M3/B1/B2/B3]).

shift([T1/T2/0/M1/M2/M3/B1/B2/B3],
      [T1/0/T2/M1/M2/M3/B1/B2/B3]).

%shift left
shift([T1/T2/T3/M1/M2/M3/B1/B2/B3],
      [T1/T2/T3/M1/M2/M3/B1/B2/B3]).

shift([T1/T2/T3/M1/M2/M3/B1/B2/B3],
      [T1/T2/T3/M1/M2/M3/B1/B2/B3]).

%shift down
shift([T1/T2/T3/0/M2/M3/B1/B2/B3],
      [0/T2/T3/T1/M2/M3/B1/B2/B3]).

shift([T1/T2/T3/M1/0/M3/B1/B2/B3],
      [T1/0/T3/M1/T2/M3/B1/B2/B3]).

shift([T1/T2/T3/M1/M2/0/B1/B2/B3],
      [T1/T2/0/M1/M2/T3/B1/B2/B3]).

%Middle row movements
%shift right
shift([T1/T2/T3/M1/0/M3/B1/B2/B3],
      [T1/T2/T3/0/M1/M3/B1/B2/B3]).

shift([T1/T2/T3/M1/M2/0/B1/B2/B3],
      [T1/T2/T3/M1/0/M2/B1/B2/B3]).

%shift left
shift([T1/T2/T3/0/M2/M3/B1/B2/B3],
      [T1/T2/T3/M2/0/M3/B1/B2/B3]).

shift([T1/T2/T3/M1/0/M3/B1/B2/B3],
      [T1/T2/T3/M1/M3/0/B1/B2/B3]).

%shift up
shift([0/T2/T3/M1/M2/M3/B1/B2/B3],
      [M1/T2/T3/0/M2/M3/B1/B2/B3]).

shift([T1/0/T3/M1/M2/M3/B1/B2/B3],
      [T1/M2/T3/M1/0/M3/B1/B2/B3]).

shift([T1/T2/0/M1/M2/M3/B1/B2/B3],
      [T1/T2/M3/M1/M2/0/B1/B2/B3]).

%shift down
shift([T1/T2/T3/M1/M2/M3/0/B2/B3],
      [T1/T2/T3/0/M2/M3/M1/B2/B3]).

shift([T1/T2/T3/M1/M2/M3/B1/0/B3],
      [T1/T2/T3/M1/0/M3/B1/M2/B3]).

shift([T1/T2/T3/M1/M2/M3/B1/B2/0],
      [T1/T2/T3/M1/M2/0/B1/B2/M3]).

%bottom row movements

%shift right
shift([T1/T2/T3/M1/M2/M3/B1/0/B3],
      [T1/T2/T3/M1/M2/M3/0/B1/B3]).

shift([T1/T2/T3/M1/M2/M3/B1/B2/0],
      [T1/T2/T3/M1/M2/M3/B1/0/B2]).

%shift left
shift([T1/T2/T3/M1/M2/M3/0/B2/B3],
      [T1/T2/T3/M1/M2/M3/B2/0/B3]).

shift([T1/T2/T3/M1/M2/M3/B1/0/B3],
      [T1/T2/T3/M1/M2/M3/B1/B3/0]).

%shift up 
shift([T1/T2/T3/0/M2/M3/B1/B2/B3],
      [T1/T2/T3/B1/M2/M3/0/B2/B3]).

shift([T1/T2/T3/M1/0/M3/B1/B2/B3],
      [T1/T2/T3/M1/B2/M3/B1/0/B3]).

shift([T1/T2/T3/M1/M2/0/B1/B2/B3],
      [T1/T2/T3/M1/M2/B3/B1/B2/0]).


h(State):-
    compare_list(State, goal, 1).

compare_list([H1 | T1], [H2 | T2], I) :-
    H1 \= H2,
    compare_list(T1, T2, I1),
    I is I1 +1.
repeat
  • 18,496
  • 4
  • 54
  • 166

1 Answers1

0

Surely you are using a wrong syntax: lists use the comma as separator. For instance

goal([1,2,3,8,0,4,7,6,5]).

I think you can do a global search and replace, to do all changes at once. After that compare_list/3 could start to work, but I wonder about the utility of the counter I. It seems useless.

And I can't spot where you apply the shifts. Then I'm not sure you're on the right track toward a solution.

If you are interested, I posted some days ago this answer. I'm sorry it's completely different than your.

Community
  • 1
  • 1
CapelliC
  • 59,646
  • 5
  • 47
  • 90