lib/stroop/set.rb in stroop-1.0.0 vs lib/stroop/set.rb in stroop-1.1.0

- old
+ new

@@ -1,12 +1,11 @@ require 'colorize' require_relative 'exceptions' +require_relative 'color_generator' module Stroop class Set - COLORS = %w{ black white red green blue yellow }.freeze - NEUTRAL = :neutral CONGRUENT = :congruent INCONGRUENT = :incongruent MODES = [NEUTRAL, CONGRUENT, INCONGRUENT].freeze @@ -18,18 +17,20 @@ top_right: "┐", bottom_left: "└", bottom_right: "┘" }.transform_values(&:light_black).freeze - attr_reader :rows, :columns, :mode + attr_reader :rows, :columns, :mode, :seed - def initialize(rows:, columns:, mode:) + def initialize(rows:, columns:, mode:, seed: nil) raise SetModeNotAvailable.new unless MODES.include?(mode) - @rows = rows.to_i.abs + @rows = rows.to_i.abs @columns = columns.to_i.abs - @mode = mode.to_sym + @mode = mode.to_sym + @generator = ColorGenerator.new(seed: seed) + @seed = @generator.seed end def to_s [empty_line(:top), *lines, empty_line(:bottom)].join("\n") end @@ -63,40 +64,23 @@ end def word_color_pair case mode when :neutral - word = random_color + word = @generator.generate color = :black when :congruent - word = random_color + word = @generator.generate color = word when :incongruent - word, color = random_different_colors + word, color = @generator.generate_pair end [word, color] end - def random_different_colors - first = random_color - second = random_color - - while first == second - second = random_color - end - - [first, second] - end - - def random_color - color = COLORS.sample - color = random_color if color == @latest_random_color - @latest_random_color = color - end - def max_word_length - @max_word_length ||= COLORS.map { |color| color.chars.count }.max + @max_word_length ||= ColorGenerator::COLORS.map { |color| color.chars.count }.max end def total_word_width max_word_length + 2 end