Hello,
I'm having some trouble in my prolog homework for some class, and needed some assistance.
I'm kind of newbie in prolog but I've been trying to read some tutorials to learn the "mysteries" behind the recursive functions and the backtracking.
My project its kind of simple, i have to make an agent (a) move randomly through a room in 4 directions (up,down,left,right) and he has to find the goal (o), but there are some walls (x), so he can only move to the spots that are free (-) or to the goal.
To accomplish this I'm using 1 fact that's a list of all the "world", the list has 16 members and it's printed has an array of 4x4.
To move the agent, I'm adding or subtracting to his index an amount of 1 (if its to the right or left), or a amount of 4 (if its to up or down).
The problem begins here, i need to make sure that the agent doesn't move to some values (A1>0 and A1<17, since the values A1=2,A1=10,A1=11 are walls he can't move there too), for that i'm trying to build up a recursive function that tests the values before the agent move (function test), if the eventual move of the agent its not valid the function gets a new value to the Random move (moves up,down,left,right) and re-tests it until its a valid value. I've first built up the function in a .pl document, then inserted it in the project, but there is some problem in the recursive testing.
lanca(A,Z,Nestado):-
R is random(4),
teste(A,R),
move(A,Z,Nestado,R).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
teste(A,R):-
R=0,
A1 is A-4,
valores(A1,R).
teste(A,R):-
R=1,
A1 is A-4,
valores(A1,R).
teste(A,R):-
R=2,
A1 is A-4,
valores(A1,R).
teste(A,R):-
R=3,
A1 is A-4,
valores(A1,R).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
valores(A1,R):-
A1<1,!,
R is random(4),
teste(A1,R).
valores(A1,R):-
A1>16,!,
R is random(4),
teste(A1,R).
valores(A1,R):-
A1=2,!,
R is random(4),
teste(A1,R).
valores(A1,R):-
A1=10,!,
R is random(4),
teste(A1,R).
valores(A1,R):-
A1=11,!,
R is random(4),
teste(A1,R).
Thanks for the help,
Tiago
Prolog - testing values of a variable
Started by TiagoISMT, May 31 2008 12:59 PM
3 replies to this topic
#1
Posted 31 May 2008 - 12:59 PM
#2
Posted 31 May 2008 - 05:50 PM
Sorry, this is not a forum for homework questions. If you are having trouble you should talk to your professor, TA, or classmates.
reedbeta.com - developer blog, OpenGL demos, and other projects
#3
Posted 31 May 2008 - 06:09 PM
My professor wants us to use this kind of forum, so there is no problem. My classmates have already gave up on this project!
#4
Posted 02 June 2008 - 05:17 PM
Hello
Your code can't work :
You first unify R with a random value.
This means that R will keep this value all along the programm with nop possibility of backtrack.
Next, you call teste(A,R).
You must change the design of your programm.
Your code can't work :
lanca(A,Z,Nestado):- R is random(4), teste(A,R), ...I Think you start with lanca(A,Z,Nestado).
You first unify R with a random value.
This means that R will keep this value all along the programm with nop possibility of backtrack.
Next, you call teste(A,R).
teste(A,R):- R=0, A1 is A-4, valores(A1,R).If we suppose that R is 0, you compute and unify A1 with A-4 thne you call valores(A1, 0).
valores(A1,R):- A1<1,!, R is random(4), teste(A1,R).R is 0 and after you unify R with random(4). It succeed one time on four, and fail three times.
You must change the design of your programm.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











