1
I am making a "game" in Prolog, from a starting 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. You must write all possible routes in a file . txt
I’m editing by sublime, compiling using SWI-Prolog.
inicia(X,Y,Matriz,Caminho) :- jogar(Matriz,X,Y,Caminho), escreveArq(Caminho).
Prints the current game matrix
imprimeMatriz([]).
imprimeMatriz([H|T]) :- imprimeLista(H),nl, imprimeMatriz(T).
imprimeLista([]).
imprimeLista([H|T]) :- write(H), write(", "), imprimeLista(T).
Writes in the archive
escreveArq([]).
escreveArq(Rota) :- open('caminho.txt', write, ID), writeq(ID,Rota), write(ID,'\n'), close(ID).
Retrieves element from the list
recuperaElemento(X, Y, Elemento,Matriz) :- achaElemento(X,0,Matriz,ListaX), achaElemento(Y,0,ListaX,Elemento).
Finds an element.
achaElemento(_,_,[],_) :- false.
achaElemento(Elemento,Aux,[H|_],H) :- Elemento == Aux.
achaElemento(Elemento,Aux,[_|HT],R) :- Aux1 is Aux + 1, achaElemento(Elemento,Aux1,HT,R).
Find neighbors of (X,Y)
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 if game has come to an 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.
Game itself
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).
Change number in (X,Y)
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).
My problem: for some matrices, example [[1,1,1], [1,1,1], [1,1,1]] generates a wrong path and presents only one path.