Prolog for loop syntax error

322 views Asked by At

I am trying to make a game similar to minesweeper and i need to check the neighbours of a square in the map but i get a syntax error at my for loop, I am using SWI-Prolog

checkneighbours(X,Y) :-
retractall(vecini(_)),
assert(vecini(0)),
foreach(I in X-1..X+1,
            (foreach J in Y-1..Y+1,
                (map(I,J,Z),
                    ( Z=:= "X"
                                -> vecini(V),
                                V1 is V+1,
                                assert(vecini(V1))
                    )
                )
            )
        ).

didn't I declare the loops right? or how can I loop between X-1 and X+1?

1

There are 1 answers

2
AudioBubble On BEST ANSWER

There are no real loops like that in Prolog. I am also not sure if it is wise to use assert to dynamically change the fact database (it is usually better to represent your data in a structure). But if you really insist to use a "loop" for its side effects, you should be using forall:

?- forall( between(-1,1,X), format('~d~n', [X]) ).
-1
0
1
true.