0
votes

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
So what error are you seeing? - user1812457
Syntax error at first foreach - Bogdan
Just add the exact error output to the question. And where did you see this I in X-1..X+1 syntax? - user1812457
I saw the syntax in a prolog example on the net, i don't think it was for SWI-Prolog, I found that for loops the syntax is like this: "?- foreach(between(1,4,X), dif(X,Y)), Y = 5." so how should i rewrite the loop? - Bogdan
This is about B-Prolog's loops! - false

1 Answers

0
votes

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.