Re: hi,
- From: "Advait" <advait_raut@xxxxxxxxxxxxxx>
- Date: 29 Aug 2006 08:45:33 -0700
Hi
kiran007.r@xxxxxxxxxxxxxx wrote:
can some one provide me code for the following program in sicstus
prolog code please.
1. Exploring a Maze
A maze is represented as a fact with four arguments (maze/4): the first
its name, the
second how many cells wide it is, the third how many cells high it is,
the fourth the maze
itself represented as a list of lists. The cells are either variables,
which the explorer can
move into, or '*'s which are walls. For example:
maze(mymaze, 3, 3, [[_, *, _],
[_, _, _],
[*, *, _]]).
The explorer can move up/down/left/right. Write code that will find a
way through the
maze from the top left (1-1) cell to the bottom right (width-height, in
this case 3-3) cell.
Note that detecting that the explorer has escaped the maze is the only
reason the
height/width arguments above are needed. The code should work for any
maze of any
dimensions.In addition, the path through the
maze should be displayed to the terminal, with each cell visited
numbered in turn. E.g.:
|1|*| |
|2|3|4|
|*|*|5|
or
|1|*| |*|
|2|*| | |
|3|4|5|6|
|*| |*|7|
You are advised to approach the problem as follows: treat the maze as a
logical object
becoming instantiated as the depth first search progresses. To do this,
follow these steps:
a) [view a cell] write a predicate called get_y/3 where the first
argument is the maze,
the second is a positive integer and the third is the row of the maze
as given by the
integer. Similarly write get_x/3; using the two allows you to view a
cell.
b) [make a move] write a predicate called move/6, where the arguments
are the maze,
the current position as co-ordinates, the next position as co-ordinates
and the current
move count. This should have four cases, each attempting to perform a
move in one of
the four possible directions and failing if this is not possible. Once
a successful, the cell
moved to should be unified with the move count.
c) [control the search] write a predicate called search/6. This should
include a case
that captures success.
d) [display a solution] write a predicate that will display a maze to
the terminal (see, for
example, above).
e) [run the code] write a predicate that will ask the user for the name
of a maze, will get
that maze from the maze facts (assuming there is a corresponding maze),
find a route
through it and display it to terminal.
SNIPPED..................
Take the code implemented for you.
I have made it in SWI,
Now its your duty to convert it in SICSTUS code
also replace the *write* call by some apropriate
functions to get better output.
I have not tested the code properly.
Try it n enjoy.
%%%%%%%%%%%%% START %%%%%%%%%
maze(Row,Col,Maze):-
move(pos(1,1),Row,Col,1,Maze),
write(Maze).
move(pos(R,C),Rmax,Cmax,Count,Puzz):-
R>0, C>0, R=<Rmax, C=<Cmax,
nth1(R,Puzz,L1), nth1(C,L1,Element), var(Element),
(
(
R=Rmax,C=Cmax,Element=Count);
Element=Count,
Count2 is Count+1,
(
C2 is C+1, R2=R ;
C2 is C-1, R2=R ;
R2 is R+1, C2=C ;
R2 is R-1, C2=C
),
move(pos(R2,C2),Rmax,Cmax,Count2,Puzz)
).
%%%%%%%%%%%%%% END %%%%%%%%
sample query.
?- maze(3,3,[[_, *, _],[_, _, _],[*, *, _]]).
[[1, *, _G447], [2, 3, 4], [*, *, 5]]
Suggestions are welcome
yours
Advait
.
- References:
- hi,
- From: kiran007.r@xxxxxxxxxxxxxx
- hi,
- Prev by Date: Re: The n-knights problem
- Next by Date: Prolog programming job?
- Previous by thread: Re: hi,
- Next by thread: Prolog programming job?
- Index(es):
Relevant Pages
|
|