•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Legacy and Other Languages section within the Software Development category of DaniWeb, a massive community of 391,771 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,301 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Legacy and Other Languages advertiser:
Views: 372 | Replies: 0
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Legacy and Other Languages Marketplace
- Previous Thread: .Bat file - use of "&" in iexplore cmd??...
- Next Thread: Q in scheme programming


Linear Mode