2013-11-26 53 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来定义-

使用下划线而不是短划线。