class CodebreakerGem::Game
Constants
- DELIMITER
- ELEMENT_VALUE_RANGE
- EXACT_MATCH
- NUMBER_MATCH
- SECRET_CODE_LENGTH
Attributes
difficulty[R]
secret_code[R]
total_attempts[R]
total_hints[R]
used_attempts[R]
used_hints[R]
Public Class Methods
new(difficulty)
click to toggle source
# File lib/app/entities/game.rb, line 14 def initialize(difficulty) @difficulty = difficulty[:level] @total_attempts = difficulty[:attempts] @used_attempts = 0 @total_hints = difficulty[:hints] @used_hints = 0 @secret_code = generate @shuffled_code = @secret_code.shuffle end
Public Instance Methods
hints_available?()
click to toggle source
# File lib/app/entities/game.rb, line 29 def hints_available? @used_hints >= @total_hints end
increment_used_attempts()
click to toggle source
# File lib/app/entities/game.rb, line 41 def increment_used_attempts @used_attempts += 1 end
loss?()
click to toggle source
# File lib/app/entities/game.rb, line 37 def loss? @used_attempts == @total_attempts end
mark_guess(guess_code)
click to toggle source
# File lib/app/entities/game.rb, line 45 def mark_guess(guess_code) @converted_input = convert_to_digit_array(guess_code) @cloned_code = @secret_code.clone convert_to_string(exact_match.compact + number_match.compact) end
use_hint()
click to toggle source
# File lib/app/entities/game.rb, line 24 def use_hint increment_used_hints @shuffled_code.shift end
win?(guess_code)
click to toggle source
# File lib/app/entities/game.rb, line 33 def win?(guess_code) guess_code == convert_to_string(@secret_code) end
Private Instance Methods
convert_marked_guess()
click to toggle source
# File lib/app/entities/game.rb, line 88 def convert_marked_guess convert_to_string(@cloned_code.grep(String).sort) end
convert_to_digit_array(guess_code)
click to toggle source
# File lib/app/entities/game.rb, line 65 def convert_to_digit_array(guess_code) guess_code.split(DELIMITER).map(&:to_i) end
convert_to_string(argument)
click to toggle source
# File lib/app/entities/game.rb, line 61 def convert_to_string(argument) argument.join end
exact_match()
click to toggle source
# File lib/app/entities/game.rb, line 69 def exact_match @converted_input.map.with_index do |digit, index| next unless @cloned_code[index] == digit @converted_input[index] = nil @cloned_code[index] = nil EXACT_MATCH end end
generate()
click to toggle source
# File lib/app/entities/game.rb, line 53 def generate Array.new(SECRET_CODE_LENGTH) { rand(ELEMENT_VALUE_RANGE) } end
increment_used_hints()
click to toggle source
# File lib/app/entities/game.rb, line 57 def increment_used_hints @used_hints += 1 end
number_match()
click to toggle source
# File lib/app/entities/game.rb, line 79 def number_match @converted_input.compact.map do |digit| next unless @cloned_code.include?(digit) @cloned_code.delete_at(@cloned_code.index(digit)) NUMBER_MATCH end end