3

The following code results in an infinite loop with an eventual "Out of Local Stack" error Basically I am decrementing the value of GX until it is the same as MX. Sample input [[m,g,b],[w,w,w]],Path

wallBlock('w').
wallBlock('b').
item('f').
item('p').
item('m').
item('u').
item('6').
item('r').
item('g').
anyCell(Cell) :- 
    wallBlock(Cell).
anyCell(Cell) :-
    item(Cell).


ghostPathing(Maps, Path) :-
    append(Maps, NewMap), 
    length(Maps, N), 
    findGhost(NewMap, N, GX, GY), 
    findPacman(NewMap, N, MX, MY),
    moveGhost(NewMap, N, MX, MY, GX, GY, Path).

/*FINDS THE COORDINATES OF THE GHOST W=row X=column*/
findGhost(NewMap, N, X, Y) :-
    findGhostSpot(NewMap, S),
    X is floor(S / N) + 1,
    Y is (S mod N) +1 .

findGhostSpot(['g'|_], 0) :-
    print('Found Ghost. ').
findGhostSpot(['r'|_], 0) :-
    print('Found Ghost. ').
findGhostSpot(['6'|_], 0) :-
    print('Found Ghost. ').
findGhostSpot([_|Tail], S) :-
    findGhostSpot(Tail, S1),
    S is S1+1 .

/*FINDS THE COORDINATES OF THE GHOST W=row X=column*/
findPacman(NewMap, N, X, Y) :-
    findPacmanSpot(NewMap, S),
    X is floor(S / N) + 1,
    Y is (S mod N) + 1.

findPacmanSpot(['m'|_], 0) :-
    print('Found Pacman. ').
findPacmanSpot([_|Tail], S) :-
    findPacmanSpot(Tail, S1),
    S is S1+1 .

/* Base Case, Ghost is on the Pacman*/
moveGhost(_, _, X, Y, X, Y, []).

/*IF PACMAN AND THE GHOST ARE IN THE SAME COLUMN*/
moveGhost(NewMap, N, MX, Y, GX, Y, ['u'|Rest]) :-
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, MX, Y, X, Y, Rest),
    GX is X + 1,
    MX < GX,
    CN is ((X * N) + Y).
moveGhost(NewMap, N, MX, Y, GX, Y, ['d'|Rest]) :-
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, MX, Y, X, Y, Rest),
    GX is X - 1,
    MX > GX,
    CN is ((X * N) + Y).

/*IF PACMAN AND THE GHOST ARE IN THE SAME ROW*/
moveGhost(NewMap, N, X, MY, X, GY, ['l'|Rest]) :-
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, X, MY, X, Y, Rest),
    GY is Y + 1,
    MY < GY,
    CN is ((X * N) + Y).
moveGhost(NewMap, N, X, MY, X, GY, ['r'|Rest]) :-
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, X, MY, X, Y, Rest),
    GY is Y - 1,
    MY > GY,
    CN is ((X * N) + Y).

itemNext([Cell|_], 0, Cell) :-
    item(Cell). 
itemNext([First|Rest], CN, Cell) :-
    anyCell(First),
    itemNext(Rest, N, Cell),
    CN is N + 1.

because it is a 2d array the append turns it into 1d and the arithmetic in the NextCell takes the lenght of a row and finds the coordinates of the adjacent cell and returns the value of that cell. The ghost cannot move in that direction if the cell is a 'w' or 'b'. you can assume Maps is SQUARE!

false
  • 10,264
  • 13
  • 101
  • 209
theB3RV
  • 894
  • 4
  • 13
  • 33
  • If GX is a free variable, you receive "< /2: Arguments are not sufficiently instantiated." so GX is X+1 must be written before MX < GX. – joel76 Dec 03 '12 at 08:21
  • @joel76 when i do this, i get an infinite loop with an eventual local stack overflow. – theB3RV Dec 03 '12 at 08:33
  • for example, MX=3, Y=3, GX=4, Y=3 – theB3RV Dec 03 '12 at 08:35
  • OK, there is a design problem. You should post more codes. – joel76 Dec 03 '12 at 08:36
  • @joel76 this is the entirety of the code that i have so far. it only works if the pacman and ghost are in the same row/colum when it is finished, but would like it to work wherever the ghost is. – theB3RV Dec 03 '12 at 08:46

1 Answers1

1

(I assume that your question is why your program loops.)

To localize the reason for the non-termination of your program, I have inserted goals false into your program. Since the remaining program (failure slice) does not terminate, also your original program does not terminate. You need to fix something in the remaining visible part. Or, to put it differently: As long as the current fragment remains unchanged, the problem will persist! And as a minor remark, better avoid print/1 goals in a pure program as yours is.

For more, see tag

?- ghostPathing([[m,g,b],[w,w,w]],Path), false.


item('f') :- false.
item('p') :- false.
item('m').
item('u') :- false.
item('6') :- false.
item('r') :- false.
item('g').

anyCell(Cell) :- false,
    wallBlock(Cell).
anyCell(Cell) :-
    item(Cell).

ghostPathing(Maps, Path) :-
    append(Maps, NewMap), 
    length(Maps, N), 
    findGhost(NewMap, N, GX, GY), 
    findPacman(NewMap, N, MX, MY),
    moveGhost(NewMap, N, MX, MY, GX, GY, Path), false.

/*FINDS THE COORDINATES OF THE GHOST W=row X=column*/
findGhost(NewMap, N, X, Y) :-
    findGhostSpot(NewMap, S),
    X is floor(S / N) + 1,
    Y is (S mod N) +1 .

findGhostSpot(['g'|_], 0) :-
    print('Found Ghost. ').
findGhostSpot(['r'|_], 0) :- false,
    print('Found Ghost. ').
findGhostSpot(['6'|_], 0) :- false,
    print('Found Ghost. ').
findGhostSpot([_|Tail], S) :-
    findGhostSpot(Tail, S1),
    S is S1+1 .

/*FINDS THE COORDINATES OF THE GHOST W=row X=column*/
findPacman(NewMap, N, X, Y) :-
    findPacmanSpot(NewMap, S),
    X is floor(S / N) + 1,
    Y is (S mod N) + 1.

findPacmanSpot(['m'|_], 0) :-
    print('Found Pacman. ').
findPacmanSpot([_|Tail], S) :- false,
    findPacmanSpot(Tail, S1),
    S is S1+1 .

/* Base Case, Ghost is on the Pacman*/
moveGhost(_, _, X, Y, X, Y, []) :- false.

/*IF PACMAN AND THE GHOST ARE IN THE SAME COLUMN*/
moveGhost(NewMap, N, MX, Y, GX, Y, ['u'|Rest]) :- false,
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, MX, Y, X, Y, Rest),
    GX is X + 1,
    MX < GX,
    CN is ((X * N) + Y).
moveGhost(NewMap, N, MX, Y, GX, Y, ['d'|Rest]) :-  false,
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, MX, Y, X, Y, Rest),
    GX is X - 1,
    MX > GX,
    CN is ((X * N) + Y).

/*IF PACMAN AND THE GHOST ARE IN THE SAME ROW*/
moveGhost(NewMap, N, X, MY, X, GY, ['l'|Rest]) :-
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, X, MY, X, Y, Rest), false,
    GY is Y + 1,
    MY < GY,
    CN is ((X * N) + Y).
moveGhost(NewMap, N, X, MY, X, GY, ['r'|Rest]) :- false,
    itemNext(NewMap, CN, Z),
    item(Z),
    moveGhost(NewMap, N, X, MY, X, Y, Rest),
    GY is Y - 1,
    MY > GY,
    CN is ((X * N) + Y).

itemNext([Cell|_], 0, Cell) :-
    item(Cell). 
itemNext([First|Rest], CN, Cell) :-
    anyCell(First),
    itemNext(Rest, N, Cell),
    CN is N + 1.
false
  • 10,264
  • 13
  • 101
  • 209