class Chadet::Guess
Attributes
chars_set[RW]
check if guess has redundant characters
guess[RW]
check if guess has redundant characters
guess_num[RW]
check if guess has redundant characters
Public Class Methods
new(chars_set)
click to toggle source
# File lib/chadet.rb, line 215 def initialize chars_set @guess = "" @guess_num = 0 @chars_set = chars_set end
Public Instance Methods
handle_redundancy()
click to toggle source
# File lib/chadet.rb, line 229 def handle_redundancy char_freq = {} @guess.each_char do |char| char_freq[char] ? char_freq[char] += 1 : char_freq[char] = 1 end redundant_char = char_freq.select {|k, v| v > 1} sorted_red_char = redundant_char.sort_by {|k, v| v} sorted_red_char.reverse! unless sorted_red_char[1].nil? || sorted_red_char[0][1] == sorted_red_char[1][1] redundant = sorted_red_char[0][0] frequency = sorted_red_char[0][1] if frequency == 2 freq_string = "twice" else freq_string = "#{frequency} times" end "Redundant: you typed \"#{redundant}\" #{freq_string}.".yellow.flash end
is_redundant?()
click to toggle source
# File lib/chadet.rb, line 221 def is_redundant? redundant = false a = @guess.split("").to_set.length b = @guess.length redundant = true if a < b return redundant end
wrong_input?()
click to toggle source
Check if wrong character is input
# File lib/chadet.rb, line 248 def wrong_input? wrong_char = false error_num = 0 @guess.each_char do |char| error_num += 1 unless chars_set.include? char end wrong_char = true if error_num > 0 return wrong_char end