I have a 5x5 grid which is described by max_size(5, 5)
. I need to generate a list of all cells from that description using DCG.
Here's the code I have so far:
:- use_module(library(clpfd)).
map_size(5, 5).
natnum(0).
natnum(X) :-
X #= X0 + 1,
natnum(X0).
list_all_cells(Visited) -->
{ length(Visited, 25) },
[].
list_all_cells(Visited) -->
[X-Y],
{ map_size(X_max, Y_max),
natnum(X), natnum(Y),
X #< X_max, Y #< Y_max,
maplist(dif(X-Y), Visited) },
list_all_cells([X-Y|Visited]).
However, it doesn't generate a list and outputs only 4 pairs.
A possible query to the DCG looks like list_all_cells([])
which is supposed to list all cells on the grid. For example, it's gonna be [0-0, 1-0, 1-1, 0-1]
for a 2x2 grid (order doesn't matter).
In fact, I need this predicate to build another one called available_steps/2
that would generate a list of all possible moves for a given position. Having available_steps(CurrentPos, Visited)
, I will be able to brute-force Hunt the Wumpus
game and find all possible routes to gold.
list_all_cells(Cells) :-
bagof(C,cell(C),Cells).
cell(X-Y) :-
between(0,4,X),
between(0,4,Y).
Example run:
?- list_all_cells(Cells); true.
Cells= [0-0, 0-1, 0-2, 0-3, 0-4, 1-0, 1-1, 1-2, ... - ...|...] [write] % The letter w was pressed.
Cells= [0-0, 0-1, 0-2, 0-3, 0-4, 1-0, 1-1, 1-2, 1-3, 1-4, 2-0, 2-1, 2-2, 2-3, 2-4, 3-0, 3-1, 3-2, 3-3, 3-4, 4-0, 4-1, 4-2, 4-3, 4-4] ;
true.