0
votes

From my understanding, if I have a predicate in disjunctive normal form (like (A && B) || (C && D) --> pred) then I can do the followinging.

pred(parameters) :-
    A, B;
    C, D.

Is this correct? If so, look at the following code.

I have the following three predicates...

maze(X, Y, Maze, Path, Score) :-
  find_eggs_and_pika(X, Y, Maze, Path, Score, 0, 0, 0, Masterball_found),
  write(Score).

find_eggs_and_pika(X, Y, Maze, Path, Score, PrevDirection, Has_egg, Egg_steps, Masterball_found) :-

  is_masterball(X, Y, Maze, Masterball_found);

  ...more predicates

is_masterball(X, Y, Maze, Masterball_found) :-
  nth1(Y, Maze, Row),
  nth1(X, Row, mb),
  Masterball_found is 1,
  false.

When I use trace. I get the following:

{trace}
| ?- maze(1,1,[[ o,  e,  j,  p,  o], [ o,  j,  o,  o,  o], [ o,  j, mt,  j,  o], [ o,  o,  e, o, o],[ p,  o,  j, mb,  o]], Path, 0).
      1    1  Call: maze(1,1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],_339,0) ?
      2    2  Call: find_eggs_and_pika(1,1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],_339,0,0,0,0,_373) ?
      3    3  Call: is_masterball(1,1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],_373) ?
      4    4  Call: nth1(1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],_470) ?
      4    4  Exit: nth1(1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],[o,e,j,p,o]) ?
      5    4  Call: nth1(1,[o,e,j,p,o],mb) ?
      5    4  Fail: nth1(1,[o,e,j,p,o],mb) ?
      3    3  Fail: is_masterball(1,1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],_373) ?
      3    3  Call: '$call'(0,find_eggs_and_pika,9,true) ?
      3    3  Exception: '$call'(0,find_eggs_and_pika,9,true) ?
      2    2  Exception: find_eggs_and_pika(1,1,[[o,e,j,p,o],[o,j,o,o,o],[o,j,mt,j,o],[o,o,e,o,...],[p,o,j,...]],_339,0,0,0,0,_373) ?

is_masterball() fails (as it should in this example) but then it's like Prolog is trying to call find_eggs_and_pika() again? Instead of moving onto the ...more predicates portion.

2

2 Answers

0
votes

In your code you are using nth1(Y, Maze, Row) and nth1(X, Row, mb), I think you need to write "Mb" instead of "mb". You should know that in prolog the words starting with capital letters are treated as Variables.

0
votes

Turns out the predicate following the is_masterball() predicate is what was causing an exception.