Class: Pacman::Arcade::Maze

Inherits:
Object
  • Object
show all
Defined in:
lib/pacman/arcade/maze.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/pacman/arcade/maze.rb', line 6

def height
  @height
end

#widthObject (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_startsObject



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_positionsObject



14
15
16
# File 'lib/pacman/arcade/maze.rb', line 14

def pellet_positions
  positions_of(".")
end

#player_startObject



27
28
29
# File 'lib/pacman/arcade/maze.rb', line 27

def player_start
  positions_of("P").first
end

#power_positionsObject



18
19
20
# File 'lib/pacman/arcade/maze.rb', line 18

def power_positions
  positions_of("o")
end

#wall?(position) ⇒ Boolean

Returns:

  • (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