class SnakesAndLadders::Grid
Constants
- CLASSIC_BOARD_MAPPINGS
Attributes
default_tile[R]
size[R]
tile_mappings[R]
Public Class Methods
classic()
click to toggle source
# File lib/snakes_and_ladders/grid.rb, line 27 def self.classic mappings = Marshal.load(Marshal.dump(CLASSIC_BOARD_MAPPINGS)) new(size: 100, tile_mappings: mappings).build end
new(size:, tile_mappings: [], default_tile: :Cell)
click to toggle source
# File lib/snakes_and_ladders/grid.rb, line 32 def initialize(size:, tile_mappings: [], default_tile: :Cell) @size = size @tile_mappings = tile_mappings @default_tile = default_tile raise ArgumentError, "Board size must be equal to or greater than its tiles." if size < tile_mappings.size end
Public Instance Methods
build()
click to toggle source
# File lib/snakes_and_ladders/grid.rb, line 40 def build (1..size).each_with_object({}) { |index, grid| grid[index] = tile_at(index) } end
Private Instance Methods
tile_at(index)
click to toggle source
# File lib/snakes_and_ladders/grid.rb, line 46 def tile_at(index) if mapping = tile_mappings.detect { |tile_map| tile_map[:location].equal?(index) } klass = mapping.delete(:class) SnakesAndLadders.const_get(klass).new(mapping) else SnakesAndLadders.const_get(default_tile).new(location: index) end end