0

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?

false
  • 10,264
  • 13
  • 101
  • 209
Bogdan
  • 402
  • 2
  • 8
  • 18

1 Answers1

0

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.
  • I tried this "forall( between(X-1,X+1,I), format('~d~n', [I]) )" but if i give the value 6 I get this error: ERROR: between/3: Type error: `integer' expected, found `6-1' – Bogdan May 15 '13 at 14:21
  • Yes, you first need to evaluate the integer expression `6-1` (or `X-1`) to an integer using `is/2`; then you can stick it into `between/3`. Altogether, this is why I said that there are no real loops in Prolog, it is not meant to be used in this way and accordingly, you need to write more. –  May 15 '13 at 14:46