Class: Pacman::Arcade::Maze
- Inherits:
-
Object
- Object
- Pacman::Arcade::Maze
- Defined in:
- lib/pacman/arcade/maze.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #ghost_starts ⇒ Object
-
#initialize(layout) ⇒ Maze
constructor
A new instance of Maze.
- #open_neighbors(position) ⇒ Object
- #pellet_positions ⇒ Object
- #player_start ⇒ Object
- #power_positions ⇒ Object
- #wall?(position) ⇒ Boolean
- #wrap(position) ⇒ Object
Constructor Details
#initialize(layout) ⇒ Maze
Returns a new instance of Maze.
8 9 10 11 12 |
# File 'lib/pacman/arcade/maze.rb', line 8 def initialize(layout) @rows = pad_rows(layout.lines.map(&:chomp)) @height = @rows.length @width = @rows.first.length end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
6 7 8 |
# File 'lib/pacman/arcade/maze.rb', line 6 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
6 7 8 |
# File 'lib/pacman/arcade/maze.rb', line 6 def width @width end |
Instance Method Details
#ghost_starts ⇒ Object
31 32 33 |
# File 'lib/pacman/arcade/maze.rb', line 31 def ghost_starts positions_of("G") end |
#open_neighbors(position) ⇒ Object
22 23 24 25 |
# File 'lib/pacman/arcade/maze.rb', line 22 def open_neighbors(position) Direction.all.map { |direction| wrap(position.step(direction)) } .reject { |neighbor| wall?(neighbor) } end |
#pellet_positions ⇒ Object
14 15 16 |
# File 'lib/pacman/arcade/maze.rb', line 14 def pellet_positions positions_of(".") end |
#player_start ⇒ Object
27 28 29 |
# File 'lib/pacman/arcade/maze.rb', line 27 def player_start positions_of("P").first end |
#power_positions ⇒ Object
18 19 20 |
# File 'lib/pacman/arcade/maze.rb', line 18 def power_positions positions_of("o") end |
#wall?(position) ⇒ Boolean
39 40 41 |
# File 'lib/pacman/arcade/maze.rb', line 39 def wall?(position) cell(position) == "#" end |
#wrap(position) ⇒ Object
35 36 37 |
# File 'lib/pacman/arcade/maze.rb', line 35 def wrap(position) Position.new(row: position.row % height, col: position.col % width) end |