Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 3.15 KB | None | 0 0
  1. class
  2.     MAZE
  3.  
  4. inherit
  5.  
  6.     ARRAY2 [CHARACTER]
  7.         redefine
  8.             out
  9.         end
  10.  
  11. create
  12.     make
  13.  
  14. feature -- Map characters
  15.  
  16.     free_path: CHARACTER = '-'
  17.             -- Character for empty fields.
  18.  
  19.     an_exit: CHARACTER = '#'
  20.             -- Character for an exit field.
  21.  
  22.     a_wall: CHARACTER = '*'
  23.             -- Character for a wall field.
  24.  
  25.     Visited_char: CHARACTER = 'x'
  26.             -- Character for a field that has been visited by `find_path'.
  27.  
  28. feature -- Element change
  29.  
  30.     set_empty (r, c: INTEGER)
  31.             -- Set field with row `r' and column `c' to empty.
  32.         require
  33.             r_valid: r >= 1 and r <= height
  34.             c_valid: c >= 1 and c <= width
  35.         do
  36.             put (free_path, r, c)
  37.         ensure
  38.             field_set: item (r, c) = free_path
  39.         end
  40.  
  41.     set_exit (r, c: INTEGER)
  42.             -- Set field with row `r' and column `c' to exit.
  43.         require
  44.             r_valid: r >= 1 and r <= height
  45.             c_valid: c >= 1 and c <= width
  46.         do
  47.             put (an_exit, r, c)
  48.         ensure
  49.             field_set: item (r, c) = an_exit
  50.         end
  51.  
  52.     set_wall (r, c: INTEGER)
  53.             -- Set field with row `r' and column `c' to wall.
  54.         require
  55.             r_valid: r >= 1 and r <= height
  56.             c_valid: c >= 1 and c <= width
  57.         do
  58.             put (a_wall, r, c)
  59.         ensure
  60.             field_set: item (r, c) = a_wall
  61.         end
  62.  
  63.     set_visited (r, c: INTEGER)
  64.             -- Set field with row `r' and column `c' to visited.
  65.         require
  66.             r_valid: r >= 1 and r <= height
  67.             c_valid: c >= 1 and c <= width
  68.         do
  69.             put (Visited_char, r, c)
  70.         ensure
  71.             field_set: item (r, c) = Visited_char
  72.         end
  73.  
  74. feature -- Status report
  75.  
  76.     is_valid (c: CHARACTER): BOOLEAN
  77.             -- Is `c' a valid map character?
  78.         do
  79.             Result := c = free_path or c = a_wall or c = an_exit
  80.         end
  81.  
  82. feature -- Path finding
  83.  
  84.     path: STRING
  85.             -- Sequence of instructions to find the way out of the maze.
  86.  
  87.     find_path (r, c: INTEGER)
  88.             -- Find the path starting at row `r' and column `c'.
  89.         do
  90.             if go(r, c) then
  91.                 path := path.substring (4, path.count)
  92.             end
  93.         end
  94.  
  95.     go (r, c: INTEGER): BOOLEAN
  96.         do
  97.             Result := false
  98.             if not can_go (r, c) then
  99.                 Result := false
  100.             else
  101.                 if item (r, c) ~ an_exit then
  102.                     path := " > You're free!"
  103.                     Result := true
  104.                 else
  105.                     set_visited (r, c)
  106.                     if go (r - 1, c) then
  107.                         path := " > N" + path
  108.                         Result := true
  109.                     elseif go (r + 1, c) then
  110.                         path := " > S" + path
  111.                         Result := true
  112.                     elseif go (r, c - 1) then
  113.                         path := " > W" + path
  114.                         Result := true
  115.                     elseif go (r, c + 1) then
  116.                         path := " > E" + path
  117.                         Result := true
  118.                     end
  119.                 end
  120.             end
  121.         end
  122.  
  123.     can_go (r, c: INTEGER): BOOLEAN
  124.         do
  125.             Result := inbound (r, c) and passable_cell (r, c)
  126.         end
  127.  
  128.     inbound (r, c: INTEGER): BOOLEAN
  129.         do
  130.             Result := r >= 1 and r <= height and c >= 1 and c <= width
  131.         end
  132.  
  133.     passable_cell (r, c: INTEGER): BOOLEAN
  134.         do
  135.             Result := item(r, c) ~ free_path or item(r, c) ~ an_exit
  136.         end
  137.  
  138. feature -- Output
  139.  
  140.     out: STRING
  141.             -- Maze map.
  142.         local
  143.             i, j: INTEGER
  144.         do
  145.             from
  146.                 i := 1
  147.                 j := 1
  148.                 Result := ""
  149.             until
  150.                 i > height
  151.             loop
  152.                 from
  153.                     j := 1
  154.                 until
  155.                     j > width
  156.                 loop
  157.                     Result.append_character (item (i, j))
  158.                     j := j + 1
  159.                 end
  160.                 i := i + 1
  161.                 Result := Result + "%N"
  162.             end
  163.         end
  164.  
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement