0
votes

I am trying to make a game similar to minesweeper and i am trying to count the number of bombs near a point on a map but it only enters in one countneighbour and it stops, how can I make it enter the other countneighbor predicates?

checkneighbours(X,Y) :- nb_setval(vecini,0),
        X1 is X-1,
        X2 is X+1,
        Y1 is Y-1,
        Y2 is Y+1,
        countneighbours(X1,Y),
        countneighbours(X1,Y1),
        countneighbours(X1,Y2),
        countneighbours(X,Y1),
        countneighbours(X,Y2),
        countneighbours(X2,Y1),
        countneighbours(X2,Y),
        countneighbours(X2,Y2),
        nb_getval(V,vecini),
         write(V).

countneighbours(X,Y) :- map(X,Y,Z),
                ( Z=:= "O"
                        -> nb_getval(V,vecini),
                        V1 is V+1,
                        nb_setval(vecini,V1)
                ).
1
What is your map predicate? And really, a global variable?user1812457

1 Answers

3
votes

The whole approach is a bit questionable, global variables, copy-pasting instead of using back-tracking, etc. How do you represent the whole playing field?

Anyway, the if-else construct will fail when the else hits. You need to write something like:

(  if_condition
-> action
;  true
).

if there is no action associated with the else.

But it could be something else, of course... What does map do?