Sha256: 671baff9bef7108e98c68bf2e9df8f0e49564e184011cf8bc560ce8b880f90b4
Contents?: true
Size: 1.85 KB
Versions: 3
Compression:
Stored size: 1.85 KB
Contents
# frozen_string_literal: true 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 def find_next_point(point, direction) dig(point.x + direction[0], point.y + direction[1]) end class << self def make_from_file(file, should_catalog: true) 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, should_catalog: should_catalog) end private def make_word_search(x_len, string, should_catalog: true) 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), should_catalog ) end end plane end def add_to_catalog(plane, point, should_catalog = true) return unless should_catalog plane.catalog[point.letter] ||= [] plane.catalog[point.letter] << point end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
word_search-1.3.0 | lib/word_search/two_dimensional/plane.rb |
word_search-1.2.1 | lib/word_search/two_dimensional/plane.rb |
word_search-1.2.0 | lib/word_search/two_dimensional/plane.rb |