Sha256: 615a535fc745e6ce74f37b427adc316ffe65ff656b3df8a7563d7a507fd2b584

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module WordSearch
  module TwoDimensional
    class Plane < Plane::Base
      def initialize(x, y)
        @catalog = Catalog.new
        @x, @y = x, y

        initialize_plane do |x_point, y_point|
          self[x_point][y_point] = Point.new(x_point, y_point)
        end
      end

      def to_s
        values.map do |row|
          row.values.map(&:letter)
        end.transpose.reverse.map(&:join).join("\n")
      end

      def add_letters
        super do |x_point, y_point|
          self[x_point][y_point].letter ||= random_letter
        end
      end

      def total_points
        x * y
      end

      def max
        [x, y].max
      end

      def directions
        @directions ||= WordSearch::TwoDimensional::Direction
      end

      class << self
        def make_from_file(file)
          string = File.read(file).split("\n").reverse

          return false if (x_len = string.collect(&:length).uniq).count > 1 ||
                          x_len.blank?

          make_word_search(x_len, string)
        end

        private

        def make_word_search(x_len, string)
          plane = new(x_len.first, string.count)

          string.each_with_index do |row, y|
            row.split('').each_with_index do |letter, x|
              add_to_catalog(plane, plane[x][y] = Point.new(x, y, letter))
            end
          end

          plane
        end

        def add_to_catalog(plane, point)
          plane.catalog[point.letter] ||= []
          plane.catalog[point.letter] << point
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
word_search-0.5.1 lib/word_search/two_dimensional/plane.rb