Class: Chadet::Guess
- Inherits:
-
Object
- Object
- Chadet::Guess
- Defined in:
- lib/chadet.rb
Instance Attribute Summary collapse
-
#chars_set ⇒ Object
check if guess has redundant characters.
-
#guess ⇒ Object
check if guess has redundant characters.
-
#guess_num ⇒ Object
check if guess has redundant characters.
Instance Method Summary collapse
- #handle_redundancy ⇒ Object
-
#initialize(chars_set) ⇒ Guess
constructor
A new instance of Guess.
- #is_redundant? ⇒ Boolean
-
#wrong_input? ⇒ Boolean
Check if wrong character is input.
Constructor Details
#initialize(chars_set) ⇒ Guess
Returns a new instance of Guess.
215 216 217 218 219 |
# File 'lib/chadet.rb', line 215 def initialize chars_set @guess = "" @guess_num = 0 @chars_set = chars_set end |
Instance Attribute Details
#chars_set ⇒ Object
check if guess has redundant characters
214 215 216 |
# File 'lib/chadet.rb', line 214 def chars_set @chars_set end |
#guess ⇒ Object
check if guess has redundant characters
214 215 216 |
# File 'lib/chadet.rb', line 214 def guess @guess end |
#guess_num ⇒ Object
check if guess has redundant characters
214 215 216 |
# File 'lib/chadet.rb', line 214 def guess_num @guess_num end |
Instance Method Details
#handle_redundancy ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# 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? ⇒ Boolean
221 222 223 224 225 226 227 |
# 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? ⇒ Boolean
Check if wrong character is input
248 249 250 251 252 253 254 255 256 |
# 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 |