Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class
- MAZE
- inherit
- ARRAY2 [CHARACTER]
- redefine
- out
- end
- create
- make
- feature -- Map characters
- free_path: CHARACTER = '-'
- -- Character for empty fields.
- an_exit: CHARACTER = '#'
- -- Character for an exit field.
- a_wall: CHARACTER = '*'
- -- Character for a wall field.
- Visited_char: CHARACTER = 'x'
- -- Character for a field that has been visited by `find_path'.
- feature -- Element change
- set_empty (r, c: INTEGER)
- -- Set field with row `r' and column `c' to empty.
- require
- r_valid: r >= 1 and r <= height
- c_valid: c >= 1 and c <= width
- do
- put (free_path, r, c)
- ensure
- field_set: item (r, c) = free_path
- end
- set_exit (r, c: INTEGER)
- -- Set field with row `r' and column `c' to exit.
- require
- r_valid: r >= 1 and r <= height
- c_valid: c >= 1 and c <= width
- do
- put (an_exit, r, c)
- ensure
- field_set: item (r, c) = an_exit
- end
- set_wall (r, c: INTEGER)
- -- Set field with row `r' and column `c' to wall.
- require
- r_valid: r >= 1 and r <= height
- c_valid: c >= 1 and c <= width
- do
- put (a_wall, r, c)
- ensure
- field_set: item (r, c) = a_wall
- end
- set_visited (r, c: INTEGER)
- -- Set field with row `r' and column `c' to visited.
- require
- r_valid: r >= 1 and r <= height
- c_valid: c >= 1 and c <= width
- do
- put (Visited_char, r, c)
- ensure
- field_set: item (r, c) = Visited_char
- end
- feature -- Status report
- is_valid (c: CHARACTER): BOOLEAN
- -- Is `c' a valid map character?
- do
- Result := c = free_path or c = a_wall or c = an_exit
- end
- feature -- Path finding
- path: STRING
- -- Sequence of instructions to find the way out of the maze.
- find_path (r, c: INTEGER)
- -- Find the path starting at row `r' and column `c'.
- do
- if go(r, c) then
- path := path.substring (4, path.count)
- end
- end
- go (r, c: INTEGER): BOOLEAN
- do
- Result := false
- if not can_go (r, c) then
- Result := false
- else
- if item (r, c) ~ an_exit then
- path := " > You're free!"
- Result := true
- else
- set_visited (r, c)
- if go (r - 1, c) then
- path := " > N" + path
- Result := true
- elseif go (r + 1, c) then
- path := " > S" + path
- Result := true
- elseif go (r, c - 1) then
- path := " > W" + path
- Result := true
- elseif go (r, c + 1) then
- path := " > E" + path
- Result := true
- end
- end
- end
- end
- can_go (r, c: INTEGER): BOOLEAN
- do
- Result := inbound (r, c) and passable_cell (r, c)
- end
- inbound (r, c: INTEGER): BOOLEAN
- do
- Result := r >= 1 and r <= height and c >= 1 and c <= width
- end
- passable_cell (r, c: INTEGER): BOOLEAN
- do
- Result := item(r, c) ~ free_path or item(r, c) ~ an_exit
- end
- feature -- Output
- out: STRING
- -- Maze map.
- local
- i, j: INTEGER
- do
- from
- i := 1
- j := 1
- Result := ""
- until
- i > height
- loop
- from
- j := 1
- until
- j > width
- loop
- Result.append_character (item (i, j))
- j := j + 1
- end
- i := i + 1
- Result := Result + "%N"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement