Question

I'm a beginner at prolog and I'm trying to make pacman move by itself using netlogo and prolog. So this is part of my code:

walkfront(_,_,_,_,_,_,Pacman,DirP,Lab,Ghost,_,Pellet,_,_,Decision) :- findall(Dir, ( member(Dir,[0,90,180,270]), \+ ( member((G,false),Ghost), dangerous(Pacman,G,2,Dir,_) ) ), L), findall(Dir,(member(Dir,[0,90,180,270]),(member(P,Pellet))),T), chooseNotDangerous(L,Pacman,DirP,Lab,Dir,T)

walkfront(_,_,_,_,_,_,Pacman,DirP,Lab,Ghost,_,Pellet,_,_,Decision) this line has all the lists of information I'm getting from netlogo, Pacman has the position of pacman (x,y), DirP is the direction pacman is facing, Lab is the free spaces in the maze, Ghost is the position of the ghosts (x,y,eaten?), Pellet is a list of all the positions of the pellets (x,y), Decision is the output choosen by pacman.
The first findall is supposed to give me all the directions (Dir) that don't have ghosts and that aren't dangerous and save them in a list called L.
The second findall, I wanted it to give me all the directions that have pellets and save them in a list called T.
My question is if this findall's are correct because my code isn't working for some reason and I think it might be beacause of the second findall.
Thank you for helping me :).

Was it helpful?

Solution

Technically, findall/3 never fails, as it will complete with an empty result list if none of the calls succeed (well, exceptions apart, if your Prolog implements them).

Of course, it's impossible to answer your question, without all the code. And probably, even with all the code available, you will get little - if any - help, because the structure of your program seems more complex than what could be advisable.

Prolog is a language with a relational data model, and such data model is better employed when it's possible to keep the relations clean, best if normalized. Now you have a predicate with 16 arguments. How are you going to ensure all of them play correctly together ?

I would say - not change now the structure of your program, if you succeed debugging it ok. But the next program - if any - use another style, and the facilities that your Prolog offers to implement data hiding.

Plain old Prolog 'only' had compound terms: you code should likely be

  packman(CurrPackManState, CurrGhostsState, NextPackManState, NextGhostsState) :-
    ...

where CurrGhostsState should be a list of CurrGhostState, and each element of this list should unify with an appropriate structure, hiding information about position, color, shape, etc...

SWI-Prolog now has dicts, and any Prolog will let you use DCGs to reduce the complexity of the code. See this page from Markus Triska, look for 'Implicitly passing states around'.

Also, you can always choice to put some less frequently updated info - like for instance the maze structure - in global database, with assert/retract.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top