How to get person to move by checking adjacent squares instead of manually inputting them in prolog?

125 views Asked by At

I am trying to write a Prolog application were an archer will move into an adjacent square that is safe on a grid that is in a 4 x 4 system, this should be done by archer itself, for example if the square above the archer is safe he will move into it, if it is not safe he will check the square to the right and if it is safe he then will move into it.

The archer starts in the cell 4,1 and there is currently a breeze square above and to the right of the archer. A breeze, gold, glitter is what is in each square so archer is in square 4,1 breeze is in 3,1 and 4,2. So far I have the user types in the co-ordinates that they want to go to, e.g. if I type in safe_square(4,1,3,1). the archer will move from 4,1 to 3,1 because the cell is safe.

But this is not a good way of doing this because the archer himself should check all adjacent options and then choose for itself what cell to go into. What I have done is ordered the safe_square() in a way that the archer will check for the gold first, the glitter second, empty next and then safe last. This is what it should do but because the users themselves have to enter in where they want to go the code is irrelevant .

Here is how I am doing it at the moment:

safe_square(Xa,Ya,X,Y,N) :-
maxBound(N),
adjacent(Xa,Ya,X,Y,N),
safe(X,Y),
      move(Xa,Ya,X,Y),
retract(show(L)),
append([[X,Y]],L,NewL),
assert(show(NewL)).

move(Xa,Ya,X,Y) :-
retract(square(Xa,Ya,ListFrom)),
delete(ListFrom,a,Existing),
retract(square(X,Y,ListTo)),
append([a],ListTo,NextPositionList),
assert(square(Xa,Ya,Existing)),
assert(square(X,Y,NextPositionList)),
!.

at(X1,Y1,E) :-
square(X1,Y1,L), member(E,L).

safe(X,Y) :-
square(X,Y,L),
\+ member(p,L),
\+ member(w,L).

adjacent(R,C,Ar,Ac,_) :- Ar is R -1,  Ar >= 1,  Ac is C.
adjacent(R,C,Ar,Ac,N) :-  Ar is R + 1,  Ar =< N,  Ac is C.
adjacent(R,C,Ar,Ac,_) :-  Ac is C - 1,  Ac >= 1,  Ar is R.
adjacent(R,C,Ar,Ac,N) :-  Ac is C + 1,  Ac =< N, Ar is R.

How can I do this without the users input?

I tried this but it didn't work:

safe_square(X,Y,L) :-
maxBound(N),
adjacent(Xa,Ya,X,Y,N),
gold(X,Y),
    move(Xa,Ya,X,Y),
retract(show(L)),
append([[X,Y]],L,NewL),
assert(show(NewL)).

And I tried this too:

at(X,Y,E) :-
square(X,Y,L), member(E,L).
0

There are 0 answers