Re: hi,



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

.



Relevant Pages

  • Re: Find The Way Out Problem, VB.NET 2003 - solution needed(
    ... If you build your maze like this then it is relatively easy to keep track of where the taveller is, which directions are valid for movement and whether or not the current cell has the exit gap. ... When you get to constructing your non-random maze it will be constructed as an array - in fact the only algorithm I know for a perfect maze requires that it be built cell by cell and then wall by wall for each cell. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Making a maze....
    ... # I just removed some unused functions -- Georgy Pruss 20031118-002204 ... import Maze ... # A maze cell is a vector of three items: ... # (for top and left walls refer to upper and left cells resp.) ...
    (comp.lang.python)
  • hi,
    ... Exploring a Maze ... The explorer can move up/down/left/right. ... this case 3-3) cell. ... write a predicate called search/6. ...
    (comp.lang.prolog)
  • Re: hugi compo #29
    ... but this is wrong because there are laws for what is a maze ... in modo che ciascuna casella all'interno sia collegata a qualsiasi altra casella ... [in the way each cell is connected to each other cell from a sole corridor ... If the current cell has any neighbours which have not been visited ...
    (alt.lang.asm)
  • Re: Design/Inheritance Question
    ... IsoscelesTriangle subclasses, but it's not clear to me what the best ... duplication is preferable to a misleading attempt to reuse the "shared" code. ... Presumably, for a maze, the /most/ important fact ... need different classes for each kind of cell. ...
    (comp.lang.java.programmer)