The problem is that when you have a call reset_grid(1, 8)
, your reset_grid(X, 8)
clause fires, but after that reset_grid(X, Y)
also fires.
To fix this you can add cut '!' to the reset_grid(X, 8)
clause or add Y < 8
to the reset_grid(X, Y)
, or do both (to get a so-called 'green cut').
The similar problem with call reset_grid(8, 8)
: reset_grid(8, _)
will match, but after that reset_grid(X, Y)
will match. Fix in the similar manner.
UPDATE.
Try to change you reset cell definition to just logging X and Y. With this code:
reset_cell(X, Y) :- write([X, Y]), nl.
reset_grid(X, _):- X > 7, write('finished resetting').
reset_grid(X, Y):- Y > 7, X1 is X + 1, reset_cell(X1, 1), reset_grid(X1, 1).
reset_grid(X, Y):- X < 8, Y < 8, reset_cell(X, Y), Y1 is Y + 1, reset_grid(X, Y1).
I get this result:
?- reset_grid(1, 1).
[1,1]
[1,2]
[1,3]
[1,4]
[1,5]
[1,6]
[1,7]
[2,1]
[2,1]
[2,2]
[2,3]
[2,4]
[2,5]
[2,6]
[2,7]
[3,1]
[3,1]
[3,2]
[3,3]
[3,4]
[3,5]
[3,6]
[3,7]
[4,1]
[4,1]
[4,2]
[4,3]
[4,4]
[4,5]
[4,6]
[4,7]
[5,1]
[5,1]
[5,2]
[5,3]
[5,4]
[5,5]
[5,6]
[5,7]
[6,1]
[6,1]
[6,2]
[6,3]
[6,4]
[6,5]
[6,6]
[6,7]
[7,1]
[7,1]
[7,2]
[7,3]
[7,4]
[7,5]
[7,6]
[7,7]
[8,1]
finished resetting
true ;
false.
So the loop works and terminates. Maybe the problem in the last call reset_cell(8, 1)
? Otherwise it's with the reset_cell
predicate itself or the predicates called from reset_cell
. Show the rest of your code so it can be tested.