Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with prolog programming.

Status
Not open for further replies.

fouduck

Programmer
Aug 8, 2012
1
0
0
GR
Hello everybody!
As a newbie, I'm having trouble with programming in prolog (for SWI-Prolog console). My excerise is about the well known broblem of crossing the bridge. Let me introduce you to it.
We have a group of 7 tourists is located around a bridge (I mean that they are not all of them on the same side), and they should cross the bridge with a torch. Our restrictions are that the bridge can bear from 1 to 3 of the tourists each time and also, no one of them can be alone on each side.Additionally, each time the program should let the user know in which state we are as far as the locations of the tourists and the time passed during the bridge-crossing. I'm asked to show all the actions needed (operators) without using any algorithms. I'v tried to write down some piece of code but it always compiles errors.
Here is the code I've already done:

initial_state (state (left(TL), right(TR), torch_left, time(P)).

max_pace (P1, P2, P3, P3) :- P1 =< P3, P2 =< P3
max_pace (P1, P2, P3, P2) :- P1 =< P2, P3 =< P2
max_pace (P1, P2, P3, P1) :- P1 > P2, P1 > P3
max_pace (P1, P2, P2) :- P1 =< P2
max_pace (P1, P2, P1) :- P1 > P2

left_operator (state (left(TL), right(TR), torch_left, time(P)),
state (left(NTL), right(NTR), torch_right, time(P)).

right_operator (state (left(TL), right(TR), torch_right, time(P)),
state (left(NTL), right(NTR), torch_left, time(P)).


%Bridge- crossing from left to right side.

move (T),
T =< TL,
NTL is TL-T,
NTR is TR+T,
valid (NTL,2),
valid(NTR,2)
time(P)
P is max_pace,
write('The bridge is crossed within P minutes'), nl.

%Bridge- crossing from right to left side.

move (T),
T =< TR,
NTR is TR-T,
NTL is TL+T,
valid (NTR,2),
valid(NTL,2)
time(P)
P is max_pace,
write('The bridge is crossed within P minutes'), nl.

I know that this isn's the best attempt but I'm still working on it. I hope you can help me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top