ajout application sokoban article univ
This commit is contained in:
parent
e8ad0d3364
commit
1c7e355c29
4 changed files with 121 additions and 0 deletions
32
TP1/Prolog/sokoban/board.pl
Normal file
32
TP1/Prolog/sokoban/board.pl
Normal file
|
@ -0,0 +1,32 @@
|
|||
next_location(Start, End, up) :- top(Start, End).
|
||||
next_location(Start, End, down) :- top(End, Start).
|
||||
next_location(Start, End, right) :- top(Start, End).
|
||||
next_location(Start, End, left) :- top(End, Start).
|
||||
|
||||
% explication nécessaire
|
||||
corner(X) :- \+ noncorner(X).
|
||||
noncorner(X) :- top(_, X), top(X, _).
|
||||
noncorner(X) :- right(_, X), right(X, _).
|
||||
|
||||
% explication nécessaire
|
||||
stuck(X, Y) :-
|
||||
(right(X, Y) ; right(Y, X)),
|
||||
(\+ storage(X); \+ storage(Y)),
|
||||
(\+ top(X,_), \+ top(Y,_); \+ top(_,X), \+ top(_,Y)),
|
||||
!.
|
||||
stuck(X,Y):-
|
||||
(top(X,Y);top(Y,X)),
|
||||
(\+ storage(X); \+ storage(Y)),
|
||||
(\+ right(X,_), \+ right(Y,_); \+ right(_,X), \+ right(_,Y)),
|
||||
!.
|
||||
|
||||
insert_list(X, [], [X]).
|
||||
insert_list(X, [Y|Ysuite], [X, Y|Ysuite]) :-
|
||||
X @=<Y,!.
|
||||
insert_list(X, [Y|Ysuite], [Y|Ordered]) :-
|
||||
insert_list(X, Ysuite, Ordered).
|
||||
|
||||
end([]).
|
||||
end([Pos|Suite]) :-
|
||||
storage(Pos),
|
||||
end(Suite).
|
5
TP1/Prolog/sokoban/main.pl
Normal file
5
TP1/Prolog/sokoban/main.pl
Normal file
|
@ -0,0 +1,5 @@
|
|||
:- include(screen0).
|
||||
:- include(board).
|
||||
:- include(solver).
|
||||
|
||||
% solve(c4r6,[c5r4,c5r5,c6r3],Plan,Len).
|
38
TP1/Prolog/sokoban/screen0.pl
Normal file
38
TP1/Prolog/sokoban/screen0.pl
Normal file
|
@ -0,0 +1,38 @@
|
|||
top(c2r5,c2r4).
|
||||
top(c2r6,c2r5).
|
||||
top(c3r3,c3r2).
|
||||
top(c3r4,c3r3).
|
||||
top(c3r5,c3r4).
|
||||
top(c3r6,c3r5).
|
||||
top(c5r5,c5r4).
|
||||
top(c5r6,c5r5).
|
||||
top(c6r3,c6r2).
|
||||
top(c6r4,c6r3).
|
||||
top(c6r5,c6r4).
|
||||
top(c7r4,c7r3).
|
||||
top(c7r5,c7r4).
|
||||
|
||||
right(c3r2,c4r2).
|
||||
right(c4r2,c5r2).
|
||||
right(c5r2,c6r2).
|
||||
right(c6r3,c7r3).
|
||||
right(c2r4,c3r4).
|
||||
right(c3r4,c4r4).
|
||||
right(c4r4,c5r4).
|
||||
right(c5r4,c6r4).
|
||||
right(c6r4,c7r4).
|
||||
right(c2r5,c3r5).
|
||||
right(c5r5,c6r5).
|
||||
right(c6r5,c7r5).
|
||||
right(c2r6,c3r6).
|
||||
right(c3r6,c4r6).
|
||||
right(c4r6,c5r6).
|
||||
|
||||
box(c6r3).
|
||||
box(c5r4).
|
||||
box(c5r5).
|
||||
|
||||
storage(c3r3).
|
||||
storage(c3r4).
|
||||
storage(c4r4).
|
||||
sokoban(c4r6).
|
46
TP1/Prolog/sokoban/solver.pl
Normal file
46
TP1/Prolog/sokoban/solver.pl
Normal file
|
@ -0,0 +1,46 @@
|
|||
|
||||
% explications nécessaires
|
||||
solve(_SokobanPos, BoxsPos, Plan, Len) :-
|
||||
end(BoxsPos),
|
||||
!,
|
||||
Plan = [],
|
||||
Len = 0.
|
||||
|
||||
solve(SokobanPos, BoxsPos, [push(BoxPos, Dir, Destination) | Plan], Len) :-
|
||||
select(BoxPos, BoxsPos, NewBoxsPos),
|
||||
next_location(PrevNextLoc, BoxPos, Dir),
|
||||
\+ member(PrevNextLoc, NewBoxsPos),
|
||||
next_location(BoxPos, NextNextLoc, Dir),
|
||||
possible_dest(NextNextLoc, BoxPos1),
|
||||
reachable(SokobanPos, PrevNextLoc, BoxsPos),
|
||||
next_dest(BoxPos, NextNextLoc, Dir, Destination, NewSokobanPos, BoxPos1),
|
||||
insert_list(Destination, NewBoxsPos, NexBoxsPos),
|
||||
solve(NewSokobanPos, NexBoxsPos, Plan, Len1),
|
||||
Len is Len1 + 1.
|
||||
|
||||
% explication nécessaires
|
||||
reachable(Pos, Pos, BoxsPos).
|
||||
reachable(X, Y, BoxsPos) :-
|
||||
next_location(X, Z, _),
|
||||
\+ member(Z, BoxsPos),
|
||||
reachable(Z, Y, BoxsPos).
|
||||
|
||||
% good_dest(Loc,BoxLocs):-
|
||||
% \+ member(Loc,BoxLocs),
|
||||
% (corner(Loc)->storage(Loc);true),
|
||||
% foreach(BoxLoc in BoxLocs, \+ stuck(BoxLoc,Loc)).
|
||||
|
||||
possible_dest(Pos, []).
|
||||
possible_dest(Pos, [BoxPos|BoxPosSuite]) :-
|
||||
\+ Pos = BoxPos,
|
||||
(corner(Pos)->storage(Pos);true),
|
||||
\+ stuck(BoxPos, Pos),
|
||||
possible_dest(Pos, BoxPosSuite).
|
||||
|
||||
next_dest(Pos, NextPos, _Dir, Destination, NewSokobanPos, _BoxsPos) :-
|
||||
Destination = NextPos,
|
||||
NewSokobanPos = BoxPos.
|
||||
next_dest(Pos, NextPos, Dir, Destination, NewSokobanPos, BoxsPos) :-
|
||||
next_location(NextPos, NextNextPos, Dir),
|
||||
possible_dest(NextNextPos, BoxsPos),
|
||||
next_dest(NextPos, NextNextPos, Dir, Destination, NewSokobanPos, BoxsPos).
|
Reference in a new issue