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

- old
+ new

@@ -1,19 +1,27 @@ require 'colorize' require_relative 'exceptions' module Stroop class Set + COLORS = %w{ black white red green blue yellow }.freeze - COLORS = %w{ black white red green blue yellow } - NEUTRAL = :neutral CONGRUENT = :congruent INCONGRUENT = :incongruent MODES = [NEUTRAL, CONGRUENT, INCONGRUENT].freeze + BOX = { + vertical: "┊", + horizontal: "┄", + top_left: "┌", + top_right: "┐", + bottom_left: "└", + bottom_right: "┘" + }.transform_values(&:light_black).freeze + attr_reader :rows, :columns, :mode def initialize(rows:, columns:, mode:) raise SetModeNotAvailable.new unless MODES.include?(mode) @@ -21,17 +29,21 @@ @columns = columns.to_i.abs @mode = mode.to_sym end def to_s - [empty_line, *lines, empty_line].join("\n") + [empty_line(:top), *lines, empty_line(:bottom)].join("\n") end private - def empty_line - wrap(space * (total_word_width * columns)) + def empty_line(location) + corner_left = BOX[:"#{location}_left"] + line = BOX[:horizontal] * (total_word_width * columns) + corner_right = BOX[:"#{location}_right"] + + corner_left + line + corner_right end def lines (1..rows).map { line } end @@ -40,16 +52,16 @@ line = (1..columns).map { random_word }.join wrap(line) end def wrap(line) - space + line + space + BOX[:vertical] + line + BOX[:vertical] end def random_word word, color = word_color_pair - word.center(total_word_width).send(color).bold.on_light_black + word.center(total_word_width).send(color).bold end def word_color_pair case mode when :neutral @@ -75,14 +87,12 @@ [first, second] end def random_color - COLORS.sample - end - - def space - " ".on_light_black + 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 end