
Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовая работа по прологу / climbers
.pl del(X, [X | L], L).
del(X, [Y | L], [Y | R]) :- del(X, L, R).
off:- add(1, 100, 100, 1, 11, 6), add(2, 100, 100, 1, 11, 6), add(3, 100, 100, 1, 11, 6), add(4, 10, 100, 1, 5, 5), add(5, 100, 100, 1, 5, 5), add(6, 3, -1, 1, 3, 3), add(7, 100, 100, 1, 3, 3), add(8, 100, 100, 1, 13, 3), add(9, 10, 100, 1, 21, 5), add(10, 10, 5, 1, 22, 3).
prepare:- assertz(clteam([e, e, e, e, e])), assertz(climber(e, e, e, e, e, e)), assertz(tmallA([e])), assertz(tmallB([e])).
length([], 0).
length([X|L], N) :- length(L, M), N is M+1.
not(Goal):-Goal,!,fail;true.
conc([], L, L).
conc([Head | Tail], L, [Head | NewTail]) :- conc(Tail, L, NewTail).
member(X, [X|T]).
member(X, [H|T]) :- member(X, T).
pr(Elem, Elem).
add(N, STR, FD, H, X, Y) :- assertz(climber(N, STR, FD, H, X, Y)).
gotm:- readytm(TmList), tmGo(TmList).
gocl:- readycl(ClList), clGo(ClList).
start(N):- (N>0, analiz, gocl, gotm, N1 is N-1, start(N1); nl).
start:- analiz, gocl, gotm, start.
readytm(TmList):- pr(TmList, [e]).
tmGo(TmList) :- (
tmallA(E), climber(N, STR, FD, H, X, Y), member(N, E), not(member(N, TmList)),
conc([N], TmList, TmList1), STR>=Y*2+9, goT(N, STR, FD, H, X, Y), tmGo(TmList1);
tmallA(E), climber(N, STR, FD, H, X, Y), member(N, E), not(member(N, TmList)),
conc([N], TmList, TmList1), goT(N, STR, FD, H, X, Y), tmGo(TmList1);
nl).
readycl(ClList):- pr(ClList, [e]).
clGo(ClList) :- (
climber(N, STR, FD, H, X, Y), not(member(N, ClList)),
tmallA(E), not(member(N, E)),
goA(N, STR, FD, H, X, Y), conc([N], ClList, ClList1), clGo(ClList1);
nl).
clcl:- tmallA(E), climber(N1, STR1, FD1, H1, X1, Y1), climber(N2, STR2, FD2, H2, X2, Y2), not(member(N1, E)), not(member(N2, E)), N1\=N2, X1==X2, Y1==Y2, H1==H2,
pr(TmListAN, [N1]), conc([N2], TmListAN, TmListANN), asserta(clteam(TmListANN)), conc(E, TmListANN, E1), retract(tmallA(E)), assertz(tmallA(E1)), write('New team!').
tmcl:- tmallA(E), climber(N1, STR1, FD1, H1, X1, Y1), clteam([Head | Tail]),
not(member(N1, E)), climber(Head, STR2, FD2, H2, X2, Y2),
X1==X2, Y1==Y2, H1==H2, conc([N1], [Head | Tail], TmListA), retract(clteam([Head | Tail])), asserta(clteam(TmListA)), conc(E, [N1], E1), retract(tmallA(E)), asserta(tmallA(E1)),
write('New member of team -'), write(N1).
tmtm:- clteam([Head1 | Tail1]), clteam([Head2 | Tail2]), Head1\=Head2,
climber(Head1, STR1, FD1, H1, X1, Y1), climber(Head2, STR2, FD2, H2, X2, Y2), X1==X2, Y1==Y2, H1==H2,
conc([Head1 | Tail1], [Head2 | Tail2], TmListA),
retract(clteam([Head1 | Tail1])), retract(clteam([Head2 | Tail2])), asserta(clteam(TmListA)),
write('Consolidation of teams').
analiz :- (
clcl, nl, analiz;
tmcl, nl, analiz;
tmtm, nl, analiz;
clteam(TmListA), length(TmListA, P), P<2, pr(TmListA, [Head | Tail]), tmallA(E), del(Head, E, E1), retract(tmallA(E)), asserta(tmallA(E1)), retract(clteam(TmListA)), nl, analiz;
nl).
goA(N, STR, FD, H, X, Y) :-
(FD<0, write('climber - '), write(N), nl, write('No food, no strage.'), nl, unluck(N, STR, FD, H, X, Y);
STR<0, write('climber - '), write(N), nl, write('No strange.'), nl, unluck(N, STR, FD, H, X, Y);
STR<Y*2+9, write('climber - '), write(N), nl, write('Eating...'), nl, eat(N, STR, FD, H, X, Y);
STR>=Y*2+9, write('climber - '), write(N), nl, write('Step'), nl, nextA(N, STR, FD, H, X, Y)).
something(N, STR, FD, H, X, Y) :- random(E), Z is E*100,
(Z=<2, write('Fall!'), nl, fall(N, STR, FD, H, X, Y);
Z>2, Z=<50, write('Hard impediment.'), nl, impediment(N, STR, FD, H, X, Y);
Z>50).
unluck(N, STR, FD, H, X, Y) :- retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, H1 is 1, asserta(climber(N, STR1, FD1, H1, X1, Y1)).
impediment(N, STR, FD, H, X, Y) :- STR1 is STR-3, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y)).
eat(N, STR, FD, H, X, Y) :- FD1 is FD-5, STR1 is STR+35, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD1, H, X, Y)).
nextA(N, STR, FD, H, X, Y) :-
(Y==13, change(N, STR, FD, H, X, Y);
Y==0, change(N, STR, FD, H, X, Y);
H==1, Y\=13, Y\=0, nextCH(N, STR, FD, H, X, Y);
H==0, Y\=13, Y\=0, nextCH(N, STR, FD, H, X, Y)).
change(N, STR, FD, H, X, Y) :- Y==13, H is 1, write('On the top!'), nl, H1 is 0, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H1, X, Y)), nextCH(N, STR, FD, H1, X, Y).
change(N, STR, FD, H, X, Y) :- Y==0, H is 0, write('Down of the hill.'), nl, H1 is 1, STR1 is 100, FD1 is 100, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD1, H1, X, Y)), nextCH(N, STR1, FD1, H1, X, Y).
change(N, STR, FD, H, X, Y) :- nextCH(N, STR, FD, H, X, Y).
nextCH(N, STR, FD, H, X, Y) :- (H==1, Y+1=<X, Y+1=<26-X, nextUA(N, STR, FD, H, X, Y);
H==1, Y+1>X, nextUR(N, STR, FD, H, X, Y);
H==1, Y+1>26-X, nextUL(N, STR, FD, H, X, Y);
H==0, nextD(N, STR, FD, H, X, Y)).
nextUA(N, STR, FD, H, X, Y) :- STR1 is STR-(Y*2+9), Y1 is Y+1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y1)), something(N, STR1, FD, H, X, Y1).
nextUR(N, STR, FD, H, X, Y) :- X1 is X+1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)), write('Deflection to the right.'), nl.
nextUL(N, STR, FD, H, X, Y) :- X1 is X-1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)), write('Deflection to the left.'), nl.
nextD(N, STR, FD, H, X, Y) :- random(E), Z is E*100, (Z=<30, Y\=13, write('Deflection to the right.'), nl, downR(N, STR, FD, H, X, Y);
Z>30, Y\=13, Z=<60, write('Deflection to the left.'), nl, downL(N, STR, FD, H, X, Y);
write('Nothing.'), nl, down(N, STR, FD, H, X, Y)).
down(N, STR, FD, H, X, Y) :- STR1 is STR-(Y*2+9), Y1 is Y-1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y1)), something(N, STR1, FD, H, X, Y1).
downL(N, STR, FD, H, X, Y) :- X1 is X-1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)).
downR(N, STR, FD, H, X, Y) :- X1 is X+1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)).
fall(N, STR, FD, H, X, Y) :- (
clteam(TmListA), member(N, TmListA), del(N, TmListA, TmList1), retract(clteam(TmListA)), asserta(clteam(TmList1)), tmallA(E), del(N, E, E1), retract(tmallA(E)), asserta(tmallA(E1)),
retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, asserta(climber(N, STR1, FD1, H1, X1, Y1));
retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, asserta(climber(N, STR1, FD1, H1, X1, Y1))).
goT(N, STR, FD, H, X, Y) :-
(FD<0, write('climber - '), write(N), nl, write('No food, no strage in team.'), nl, unluckT(N, STR, FD, H, X, Y);
STR<0, write('climber - '), write(N), nl, write('No strange in team.'), nl, unluckT(N, STR, FD, H, X, Y);
STR>=Y*2+9, clteam(TmList), member(N,TmList), climber(N3, STR3, FD3, H3, X3, Y3), member(N3, TmList), STR3<Y*2+9, write('climber - '), write(N), nl, write('Waiting... in team'), nl, wait(N, STR, FD, H, X, Y);
STR<Y*2+9, write('climber - '), write(N), nl, write('Eating... in team'), nl, eat(N, STR, FD, H, X, Y);
STR>=Y*2+9, write('climber - '), write(N), nl, write('Step in team'), nl, nextA(N, STR, FD, H, X, Y)).
unluckT(N, STR, FD, H, X, Y) :- clteam(TmList), del(N, TmList, TmList1), retract(clteam(TmList)), asserta(clteam(TmList1)), retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, H1 is 1,
asserta(climber(N, STR1, FD1, H1, X1, Y1)), tmallA(E), del(N, E, E1), retract(tmallA(E)), asserta(tmallA(E1)).
wait(N, STR, FD, H, X, Y) :- STR1 is STR+3, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y)).
del(X, [Y | L], [Y | R]) :- del(X, L, R).
off:- add(1, 100, 100, 1, 11, 6), add(2, 100, 100, 1, 11, 6), add(3, 100, 100, 1, 11, 6), add(4, 10, 100, 1, 5, 5), add(5, 100, 100, 1, 5, 5), add(6, 3, -1, 1, 3, 3), add(7, 100, 100, 1, 3, 3), add(8, 100, 100, 1, 13, 3), add(9, 10, 100, 1, 21, 5), add(10, 10, 5, 1, 22, 3).
prepare:- assertz(clteam([e, e, e, e, e])), assertz(climber(e, e, e, e, e, e)), assertz(tmallA([e])), assertz(tmallB([e])).
length([], 0).
length([X|L], N) :- length(L, M), N is M+1.
not(Goal):-Goal,!,fail;true.
conc([], L, L).
conc([Head | Tail], L, [Head | NewTail]) :- conc(Tail, L, NewTail).
member(X, [X|T]).
member(X, [H|T]) :- member(X, T).
pr(Elem, Elem).
add(N, STR, FD, H, X, Y) :- assertz(climber(N, STR, FD, H, X, Y)).
gotm:- readytm(TmList), tmGo(TmList).
gocl:- readycl(ClList), clGo(ClList).
start(N):- (N>0, analiz, gocl, gotm, N1 is N-1, start(N1); nl).
start:- analiz, gocl, gotm, start.
readytm(TmList):- pr(TmList, [e]).
tmGo(TmList) :- (
tmallA(E), climber(N, STR, FD, H, X, Y), member(N, E), not(member(N, TmList)),
conc([N], TmList, TmList1), STR>=Y*2+9, goT(N, STR, FD, H, X, Y), tmGo(TmList1);
tmallA(E), climber(N, STR, FD, H, X, Y), member(N, E), not(member(N, TmList)),
conc([N], TmList, TmList1), goT(N, STR, FD, H, X, Y), tmGo(TmList1);
nl).
readycl(ClList):- pr(ClList, [e]).
clGo(ClList) :- (
climber(N, STR, FD, H, X, Y), not(member(N, ClList)),
tmallA(E), not(member(N, E)),
goA(N, STR, FD, H, X, Y), conc([N], ClList, ClList1), clGo(ClList1);
nl).
clcl:- tmallA(E), climber(N1, STR1, FD1, H1, X1, Y1), climber(N2, STR2, FD2, H2, X2, Y2), not(member(N1, E)), not(member(N2, E)), N1\=N2, X1==X2, Y1==Y2, H1==H2,
pr(TmListAN, [N1]), conc([N2], TmListAN, TmListANN), asserta(clteam(TmListANN)), conc(E, TmListANN, E1), retract(tmallA(E)), assertz(tmallA(E1)), write('New team!').
tmcl:- tmallA(E), climber(N1, STR1, FD1, H1, X1, Y1), clteam([Head | Tail]),
not(member(N1, E)), climber(Head, STR2, FD2, H2, X2, Y2),
X1==X2, Y1==Y2, H1==H2, conc([N1], [Head | Tail], TmListA), retract(clteam([Head | Tail])), asserta(clteam(TmListA)), conc(E, [N1], E1), retract(tmallA(E)), asserta(tmallA(E1)),
write('New member of team -'), write(N1).
tmtm:- clteam([Head1 | Tail1]), clteam([Head2 | Tail2]), Head1\=Head2,
climber(Head1, STR1, FD1, H1, X1, Y1), climber(Head2, STR2, FD2, H2, X2, Y2), X1==X2, Y1==Y2, H1==H2,
conc([Head1 | Tail1], [Head2 | Tail2], TmListA),
retract(clteam([Head1 | Tail1])), retract(clteam([Head2 | Tail2])), asserta(clteam(TmListA)),
write('Consolidation of teams').
analiz :- (
clcl, nl, analiz;
tmcl, nl, analiz;
tmtm, nl, analiz;
clteam(TmListA), length(TmListA, P), P<2, pr(TmListA, [Head | Tail]), tmallA(E), del(Head, E, E1), retract(tmallA(E)), asserta(tmallA(E1)), retract(clteam(TmListA)), nl, analiz;
nl).
goA(N, STR, FD, H, X, Y) :-
(FD<0, write('climber - '), write(N), nl, write('No food, no strage.'), nl, unluck(N, STR, FD, H, X, Y);
STR<0, write('climber - '), write(N), nl, write('No strange.'), nl, unluck(N, STR, FD, H, X, Y);
STR<Y*2+9, write('climber - '), write(N), nl, write('Eating...'), nl, eat(N, STR, FD, H, X, Y);
STR>=Y*2+9, write('climber - '), write(N), nl, write('Step'), nl, nextA(N, STR, FD, H, X, Y)).
something(N, STR, FD, H, X, Y) :- random(E), Z is E*100,
(Z=<2, write('Fall!'), nl, fall(N, STR, FD, H, X, Y);
Z>2, Z=<50, write('Hard impediment.'), nl, impediment(N, STR, FD, H, X, Y);
Z>50).
unluck(N, STR, FD, H, X, Y) :- retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, H1 is 1, asserta(climber(N, STR1, FD1, H1, X1, Y1)).
impediment(N, STR, FD, H, X, Y) :- STR1 is STR-3, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y)).
eat(N, STR, FD, H, X, Y) :- FD1 is FD-5, STR1 is STR+35, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD1, H, X, Y)).
nextA(N, STR, FD, H, X, Y) :-
(Y==13, change(N, STR, FD, H, X, Y);
Y==0, change(N, STR, FD, H, X, Y);
H==1, Y\=13, Y\=0, nextCH(N, STR, FD, H, X, Y);
H==0, Y\=13, Y\=0, nextCH(N, STR, FD, H, X, Y)).
change(N, STR, FD, H, X, Y) :- Y==13, H is 1, write('On the top!'), nl, H1 is 0, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H1, X, Y)), nextCH(N, STR, FD, H1, X, Y).
change(N, STR, FD, H, X, Y) :- Y==0, H is 0, write('Down of the hill.'), nl, H1 is 1, STR1 is 100, FD1 is 100, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD1, H1, X, Y)), nextCH(N, STR1, FD1, H1, X, Y).
change(N, STR, FD, H, X, Y) :- nextCH(N, STR, FD, H, X, Y).
nextCH(N, STR, FD, H, X, Y) :- (H==1, Y+1=<X, Y+1=<26-X, nextUA(N, STR, FD, H, X, Y);
H==1, Y+1>X, nextUR(N, STR, FD, H, X, Y);
H==1, Y+1>26-X, nextUL(N, STR, FD, H, X, Y);
H==0, nextD(N, STR, FD, H, X, Y)).
nextUA(N, STR, FD, H, X, Y) :- STR1 is STR-(Y*2+9), Y1 is Y+1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y1)), something(N, STR1, FD, H, X, Y1).
nextUR(N, STR, FD, H, X, Y) :- X1 is X+1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)), write('Deflection to the right.'), nl.
nextUL(N, STR, FD, H, X, Y) :- X1 is X-1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)), write('Deflection to the left.'), nl.
nextD(N, STR, FD, H, X, Y) :- random(E), Z is E*100, (Z=<30, Y\=13, write('Deflection to the right.'), nl, downR(N, STR, FD, H, X, Y);
Z>30, Y\=13, Z=<60, write('Deflection to the left.'), nl, downL(N, STR, FD, H, X, Y);
write('Nothing.'), nl, down(N, STR, FD, H, X, Y)).
down(N, STR, FD, H, X, Y) :- STR1 is STR-(Y*2+9), Y1 is Y-1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y1)), something(N, STR1, FD, H, X, Y1).
downL(N, STR, FD, H, X, Y) :- X1 is X-1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)).
downR(N, STR, FD, H, X, Y) :- X1 is X+1, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR, FD, H, X1, Y)).
fall(N, STR, FD, H, X, Y) :- (
clteam(TmListA), member(N, TmListA), del(N, TmListA, TmList1), retract(clteam(TmListA)), asserta(clteam(TmList1)), tmallA(E), del(N, E, E1), retract(tmallA(E)), asserta(tmallA(E1)),
retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, asserta(climber(N, STR1, FD1, H1, X1, Y1));
retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, asserta(climber(N, STR1, FD1, H1, X1, Y1))).
goT(N, STR, FD, H, X, Y) :-
(FD<0, write('climber - '), write(N), nl, write('No food, no strage in team.'), nl, unluckT(N, STR, FD, H, X, Y);
STR<0, write('climber - '), write(N), nl, write('No strange in team.'), nl, unluckT(N, STR, FD, H, X, Y);
STR>=Y*2+9, clteam(TmList), member(N,TmList), climber(N3, STR3, FD3, H3, X3, Y3), member(N3, TmList), STR3<Y*2+9, write('climber - '), write(N), nl, write('Waiting... in team'), nl, wait(N, STR, FD, H, X, Y);
STR<Y*2+9, write('climber - '), write(N), nl, write('Eating... in team'), nl, eat(N, STR, FD, H, X, Y);
STR>=Y*2+9, write('climber - '), write(N), nl, write('Step in team'), nl, nextA(N, STR, FD, H, X, Y)).
unluckT(N, STR, FD, H, X, Y) :- clteam(TmList), del(N, TmList, TmList1), retract(clteam(TmList)), asserta(clteam(TmList1)), retract(climber(N, STR, FD, H, X, Y)), H1 is 1, STR1 is 100, FD1 is 100, X1 is 0, Y1 is 0, H1 is 1,
asserta(climber(N, STR1, FD1, H1, X1, Y1)), tmallA(E), del(N, E, E1), retract(tmallA(E)), asserta(tmallA(E1)).
wait(N, STR, FD, H, X, Y) :- STR1 is STR+3, retract(climber(N, STR, FD, H, X, Y)), asserta(climber(N, STR1, FD, H, X, Y)).
Соседние файлы в папке Курсовая работа по прологу