2013-11-26 55 views
0

我做這個序言功能的吃豆子游戲:序言 - 遞歸運動

% I want this to return 0, 1, 2 or 3 to make a move. 
other-moves([[Xpacman,Ypacman]], Listpellets, Listwall, Movepacman) :- 
    count_pellets_above(Listpellets,A), 
    count_pellets_bellow(Listpellets,B), 
    A > B, 
    repeat, 
    choose(4,2,Movepacman), 
    iswall(Xpacman,Ypacman,Movepacman,Listwall), 
    !. 
other-moves([[Xpacman,Ypacman]], Listpellets, Listwall, Movepacman) :- 
    count_pellets_above(Listpellets,C), 
    count_pellets_bellow(Listpellets,D), 
    C =< D, 
    repeat, 
    choose(4,3,Movepacman), 
    iswall(Xpacman,Ypacman,Movepacman,Listwall), 
    !. 

% verifies if the coordinate is a wall. 
iswall(Xpacman, Ypacman, Random,Listwall) :- 
    Random==0, 
    X1 is Xpacman-1, 
    (member([X1,Ypacman], Listwall)), 
    !. 
iswall(Xpacman, Ypacman, Random,Listwall) :- 
    Random==1, 
    X1 is Xpacman+1, 
    (member([X1,Ypacman],Listwall)), 
    !. 
iswall(Xpacman, Ypacman, Random,Listwall) :- 
    Random==2, 
    Y1 is Ypacman-1, 
    (member([Xpacman,Y1],Listwall)), 
    !. 
iswall(Xpacman, Ypacman, Random,Listwall) :- 
    Random==3, 
    Y1 is Ypacman+1, 
    (member([Xpacman,Y1],Listwall)), 
    !. 

% gives a random number 
choose(A, C, B) :- 
    repeat, 
    B is random(A), 
    B \= C, 
    !. 

%count the number of pellets above the coordinate (0,0). 
count_pellets_above([],0). 
count_pellets_above([[_,Y]|T],N) :- 
    Y>=0, 
    count_pellets_above(T,M), 
    N is M+1, 
    !. 
count_pellets_above([[_,Y]|T],N) :- 
    Y<0, 
    count_pellets_above(T,M), 
    N is M, 
    !. 

% count the number of pellets bellow the coordinate (0,0). 
count_pellets_bellow([],0). 
count_pellets_bellow([[_,Y]|T],N) :- 
    Y=<0, 
    count_pellets_bellow(T,M), 
    N is M+1, 
    !. 
count_pellets_bellow([[_,Y]|T],N) :- 
    Y>0, 
    count_pellets_bellow(T,M), 
    N is M, 
    !. 

我希望其他行駛至返回從一招一堵牆不同的數字。我不知道爲什麼其他移動是在我發出這個命令時返回false而不是數字:

other-moves([[1,2]],[[]],[[1,3]],C). 

謝謝。

回答

1

other-moves不是有效的Prolog標識符。它解析爲

other - moves([[Xpacman,Ypacman]], Listpellets, Listwall, Movepacman) 

所以你有效地對原子other和某些moves/4來定義-

使用下劃線而不是短劃線。