Louis, I'm not entirely clear on what you're aiming to do with this code, but a couple of comments that might hopefully help.
Things that start with a capital letter in Prolog are variables to be matched against in rules. _
is a special symbol that can be used in place of a variable name to indicate that any value can match.
next_safe(_)
is therefore only capable of providing you with a true/false answer if you give it a specific value. One of the major benefits of Prolog is the ability to unify against a variable through backtracking (as ony said). This would mean that when written correctly you could just ask Prolog next_safe(X).
and it would return all the possible values (safe moves) that unify with X.
To go back to the first point about capital letters. This means that OK
is actually a variable waiting to be matched. It is effectively an empty box that you are trying to match against another empty box. I think what you're intending is to use the value ok
which is different. You do not assign to variables in the same way that you do in other programming styles. Something like the following might be closer to what you are looking for, though I'm still not sure it's right as it looks like you're trying to assign things but I'm not certain how your nth1
works.
Cells = [ok, _, _, _, _, _] .
...
next_safe(NewLoc) :-
facing(CurrentDirection),
delta(CurrentDirection, Delta),
in_cell(OldLoc),
NewLoc is OldLoc + Delta,
nth1(NewLoc, Cells, ok).