Class: Chadet::Guess

Inherits:
Object
  • Object
show all
Defined in:
lib/chadet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chars_set) ⇒ Guess

Returns a new instance of Guess.



266
267
268
269
270
# File 'lib/chadet.rb', line 266

def initialize chars_set
  @guess = ""
  @guess_num = 0
  @chars_set = chars_set
end

Instance Attribute Details

#chars_setObject

check if guess has redundant characters



265
266
267
# File 'lib/chadet.rb', line 265

def chars_set
  @chars_set
end

#guessObject

check if guess has redundant characters



265
266
267
# File 'lib/chadet.rb', line 265

def guess
  @guess
end

#guess_numObject

check if guess has redundant characters



265
266
267
# File 'lib/chadet.rb', line 265

def guess_num
  @guess_num
end

Instance Method Details

#handle_redundancyObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/chadet.rb', line 280

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

Returns:

  • (Boolean)


272
273
274
275
276
277
278
# File 'lib/chadet.rb', line 272

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

Returns:

  • (Boolean)


299
300
301
302
303
304
305
306
307
# File 'lib/chadet.rb', line 299

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