2012-12-03 91 views
3

下面的代碼導致了一個無限循環,最終出現「Out of Local Stack」錯誤基本上,我將GX的值遞減,直到它與MX相同。 樣品輸入[[米,G,B],[W,W,W],路徑Prolog中的列表移位

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. 

,因爲它是一個二維數組追加把它變成1d和在NextCell算術取lenght並找到相鄰單元格的座標並返回該單元格的值。如果單元格是'w'或'b',鬼魂不能朝那個方向移動。你可以假設地圖是SQUARE!

+0

如果GX是自由變量,您會收到 「 joel76

+0

@ joel76當我這樣做,我得到一個無限循環與最終本地堆棧溢出。 – theB3RV

+0

例如,MX = 3,Y = 3,GX = 4,Y = 3 – theB3RV

回答

1

(我假設你的問題是,爲什麼你的程序循環。)

要本地化你的程序的非終止的原因,我已插入目標false到你的程序。由於剩餘的程序(失敗片)沒有終止,您的原始程序也不會終止。你需要修復剩下的可見部分。或者,換句話說:只要當前片段保持不變,問題就會持續存在!作爲一個小小的評論,最好避免在純粹的程序中使用print/1目標。

如需更多信息,請參見標籤

 
?- 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.