Question: We can represent a maze on a rectangular grid as a list of paths. Each path consists of a pair of coordinates which are the

We can represent a maze on a rectangular grid as a list of paths. Each path consists of a pair of coordinates which are the ends of the path, where a coordinate is a pair of non-negative integers (x, y). All paths run either horizontally (same y-value for both ends) or vertically (same x-value for both ends). A path of length 1 is considered to be both horizontal and vertical. The order of the ends of a paths is not important; the path ((0,2),(2,2)) is equivalent to the path ((2,2),(0,2)). Similarly, the order of the paths in the maze is unimportant.
Write a function:
renderMaze :: [((Int, Int),(Int, Int))]->[String]
that renders the maze row-by-row as a list of strings. Each character in each string represents a coordinate within the maze. If a horizontal path is present at that coordinate, the character should be a hyphen symbol '-'. If a vertical path is present at that coordinate, the character should be a bar symbol '|'. If that coordinate lies on both a horizontal and vertical path, the character should be a plus symbol '+'.If a path is not present, the character should be a space.
For example, if we have defined:
maze =[((0,0),(0,3)),((0,2),(2,2)),((2,1),(4,1)),((4,0),(4,2)),((4,2),(5,2)),((2,1),(2,5)),((1,5),(4,5))]
then renderMaze maze should return:["||","|+-+","+-++-","||","|","-+--"]. Written an way that is easier to read, this is
["||",
"|+-+",
"+-++-",
"||",
"|",
"-+--"]
The grid in the rendering should run from (0,0) in the top left corner to (maxX,maxY) in the bottom right, where maxX and maxY are the largest x and y values to appear in any path in the maze.
Hint: you may find it useful to use list comprehensions to calculate all of the (x,y) points that appear in a maze.
For example:
TestResult
renderMaze []
[]
renderMaze [((0,0),(0,0))]- Singleton Path
["+"]
renderMaze [((1,1),(1,1))]- Singleton Path, maze size 2x2
["","+"]
renderMaze [((0,1),(1,1))]- Horizontal Path
["","--"]
renderMaze [((1,1),(0,1))]- Horizontal Path, switched endpoints
["","--"]
renderMaze [((1,0),(1,1))]- Vertical Path
["|","|"]
renderMaze [((1,1),(1,0))]- Vertical Path, switched endpoints
["|","|"]
renderMaze [((0,1),(1,1)),((1,0),(1,1))]- Intersecting Paths
["|","-+"]
renderMaze [((0,1),(3,1)),((2,1),(4,1))]- Overlapping Paths
["","-----"]
renderMaze [((0,0),(0,1)),((0,2),(3,2))]- Non-Intersecting Paths
["|","|","----"]
renderMaze [((2,0),(2,3)),((0,1),(4,1)),((1,3),(3,3)),((1,3),(1,5)),((3,3),(3,5))]- person-like shape
["|","--+--","|","+++","||","||"]
renderMaze [((0,0),(0,1)),((0,1),(1,1)),((1,1),(1,2)),((1,2),(2,2)),((2,2),(2,3)),((2,3),(3,3)),((3,3),(3,5)),((3,4),(4,4)),((2,5),(5,5)),((2,5),(2,6)),((1,6),(2,6)),((1,7),(1,6)),((0,7),(1,7)),((5,5),(5,6)),((5,6),(6,6)),((6,6),(6,7)),((6,7),(7,7))]- lambda shape
["|","++","++","++","+-","++-+","++++","-++-"]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!