0

I'm making a "game" in Prolog, from an initial position in an array, the game must return all possible paths, where the whole array is -1 and the current position is set to 0. Must write all possible routes in a .txt file

Editing by sublime, compiling using SWI-Prolog.

Initial function receiving initial position X, Y in matrix

inicia(X,Y,Matriz,Caminho) :- jogar(Matriz,X,Y,Caminho), escreveArq(Caminho). 

Prints matrix

imprimeMatriz([]).
imprimeMatriz([H|T]) :- imprimeLista(H),nl, imprimeMatriz(T).
imprimeLista([]).
imprimeLista([H|T]) :- write(H),  write(", "), imprimeLista(T).

Write on file

escreveArq([]).
escreveArq(Rota) :- open('caminho.txt', write, ID), writeq(ID,Rota), write(ID,'\n'), close(ID).

recover element X Y

recuperaElemento(X, Y, Elemento,Matriz) :- achaElemento(X,0,Matriz,ListaX), achaElemento(Y,0,ListaX,Elemento).
achaElemento(_,_,[],_) :- false.
achaElemento(Elemento,Aux,[H|_],H) :- Elemento == Aux.
achaElemento(Elemento,Aux,[_|HT],R) :- Aux1 is Aux + 1, achaElemento(Elemento,Aux1,HT,R).

search neighbor element

elementoSuperior(X,Y,Elemento,Matriz) :- N is X-1, recuperaElemento(N,Y,Elemento, Matriz).
elementoSuperior(_,_,-5,_).
elementoInferior(X,Y,Elemento,Matriz) :- N is X+1, recuperaElemento(N,Y,Elemento,Matriz).
elementoInferior(_,_,-5,_).
elementoDireita(X,Y,Elemento,Matriz) :- N is Y+1, recuperaElemento(X,N,Elemento,Matriz).
elementoDireita(_,_,-5,_).
elementoEsquerda(X,Y,Elemento,Matriz) :- N is Y-1, recuperaElemento(X,N,Elemento,Matriz).
elementoEsquerda(_,_,-5,_).

check game end

fimDeJogo(Matriz) :- verificaFim(Matriz,R), R == 1.
    verificaFim([],X) :- X is 0.
    verificaFim([H|T],X) :- verificaFimColuna(H,X1), verificaFim(T,X2), X is X1 + X2.
    verificaFimColuna([],X) :- X is 0, !.
    verificaFimColuna([H|T],R) :- H == -1, verificaFimColuna(T, R).
    verificaFimColuna([H|T],X) :- H == 0, verificaFimColuna(T, R), X is R+1.
impossivelJogar(Matriz, X,Y) :- elementosVizinhos(X,Y,Resp,Matriz), verifica(Resp).
    verifica([]).
    verifica([H|T]) :- H < 0, verifica(T).

Play the game adds S as it rises in array adds And when it goes to the left add D when going right add I when it goes down

jogar(Matriz, _,_,_) :- fimDeJogo(Matriz).
jogar(Matriz, X,Y,["S"|Rota]) :- elementoSuperior(X,Y,Z,Matriz),Z > -1,decrementa(X,Y,Matriz,NMAtriz),
                                                                                        NX is X-1,jogar(NMAtriz, NX, Y,Rota).
jogar(Matriz, X,Y,["I"|Rota]) :- elementoInferior(X,Y,Z,Matriz),Z > -1,decrementa(X,Y,Matriz,NMAtriz),
                                                                                        NX is X+1,jogar(NMAtriz, NX, Y,Rota).
jogar(Matriz, X,Y,["E"|Rota]) :- elementoEsquerda(X,Y,Z,Matriz),Z > -1,decrementa(X,Y,Matriz,NMAtriz),
                                                                                        NY is Y-1,jogar(NMAtriz, X, NY,Rota).
jogar(Matriz, X,Y,["D"|Rota]) :- elementoDireita(X,Y,Z,Matriz) ,Z > -1,decrementa(X,Y,Matriz,NMAtriz),
                                                                                        NY is Y+1,jogar(NMAtriz, X, NY,Rota).

reduces X value in the matrix

decrementa(X,Y,Matriz, MatrizAtualizada) :- reduzX(X,Y,0,0,Matriz,MatrizAtualizada).
    reduzX(_,_,    _,    _,    [],         []) .
    reduzX(X,Y,    X, AuxY, [H|T], [LAlt|Mat]) :- NX is X+1,reduzY(X,Y, AuxY,  AuxY, H, LAlt), reduzX(X,Y,NX, AuxY, T, Mat).
    reduzX(X,Y, AuxX, AuxY, [H|T], [   H|Mat]) :- NAuxX is AuxX+1, reduzX(X,Y,NAuxX, AuxY, T,  Mat).
        reduzY(_,_, _,    _,    [],       []) .
        reduzY(_,Y, _,    Y, [H|T], [NH|MAT]) :- NH is H-1, NY is Y+1,   reduzY(_,Y, _, NY, T, MAT).
        reduzY(_,Y, _, AuxY, [H|T], [ H|MAT]) :-           NY is AuxY+1, reduzY(_,Y, _, NY, T, MAT).

for some matrices, example [[1,1,1], [1,1,1], [1,1,1] generates a wrong path and only presents a path.

I believe the error responsible for giving me wrong directions, is in this code snippet:

jogar(Matriz, _,_, []) :- fimDeJogo(Matriz).
jogar(Matriz, X,Y, _) :- impossivelJogar(Matriz, X,Y).
jogar(Matriz, X,Y,["S"|Rota]) :- elementoSuperior(X,Y,Z,Matriz),Z > -1,decrementa(X,Y,Matriz,NMatriz),
                                                                                        NX is X-1,jogar(NMatriz, NX, Y,Rota).
jogar(Matriz, X,Y,["I"|Rota]) :- elementoInferior(X,Y,Z,Matriz),Z > -1,decrementa(X,Y,Matriz,NMatriz),
                                                                                        NX is X+1,jogar(NMatriz, NX, Y,Rota).
jogar(Matriz, X,Y,["E"|Rota]) :- elementoEsquerda(X,Y,Z,Matriz),Z > -1,decrementa(X,Y,Matriz,NMatriz),
                                                                                        NY is Y-1,jogar(NMatriz, X, NY,Rota).
jogar(Matriz, X,Y,["D"|Rota]) :- elementoDireita(X,Y,Z,Matriz) ,Z > -1,decrementa(X,Y,Matriz,NMatriz),
                                                                                        NY is Y+1,jogar(NMatriz, X, NY,Rota).

When the game does not end and it is impossible to play, the program should return and try another path, instead it checks the next options and "forces" a path. How can it not continue and return to try a new path to see that there is no longer a possible option?

  • 1
    This is a good start. Already a much better question than the average. This gives me hope you will take advice, namely: please try to construct a *minimal*, reproducible example, as described here: https://stackoverflow.com/help/minimal-reproducible-example. The important word here is **minimal**. What you have showed is too much code; to answer it, I'd have to "debug" it. So why don't you debug it instead? This will help you make a minimal example. – User9213 May 29 '19 at 06:00
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas May 29 '19 at 06:12
  • I'm going to try to do this, I'm really already doing trace in SWI for two days and several tests, including doing it now, but it's really not working, I'll try to do and post here again !! – Gabriel Augusto May 29 '19 at 06:28
  • Hello friend, I just edited, because I believe you have found the location of the error, and I do not know how to correct it, can you help me? It is at the end of everything. – Gabriel Augusto May 29 '19 at 07:49

0 Answers0