Sha256: 11e1212b44d82e1e18686674cbb2805ce4c3e2cc32df8eb4242d3cf50a520ded

Contents?: true

Size: 852 Bytes

Versions: 3

Compression:

Stored size: 852 Bytes

Contents

module WordSearch
  module TwoDimensional
    class Generator < Generator::Base
      def directions
        Direction.values.shuffle.map do |direction|
          Point.new(direction.first, direction.second)
        end
      end

      private

      def place_word(word)
        placed = false
        until placed || used_coordinates.uniq.count == plane.total_points
          placed = position_word(word).present?
        end

        word_bank.errors.add(word, 'cannot be placed') if placed.blank?
        placed
      end

      def position_word(word)
        used_coordinates << (coordinate = random_point)

        directions.find do |direction|
          PositionWord.new(plane, word, direction, coordinate).perform
        end
      end

      def random_point
        Point.new(random(plane.x), random(plane.y))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
word_search-0.5.1 lib/word_search/two_dimensional/generator.rb
word_search-0.5.0 lib/word_search/two_dimensional/generator.rb
word_search-0.1.0 lib/word_search/two_dimensional/generator.rb