class GemCodebreakerAmidasd::Game
Attributes
count_attempt[R]
count_hints[R]
count_minus[R]
count_plus[R]
difficulty[R]
difficulty_hash[R]
error[R]
hint[R]
length_code[R]
max_num[R]
min_num[R]
secret_code[R]
secret_code_rand[R]
status[R]
total_count_attempt[R]
total_count_hints[R]
Public Class Methods
new(other_difficulty: {})
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 20 def initialize(other_difficulty: {}) @difficulty_hash = DIFFICULTY_HASH.merge(other_difficulty) @total_count_attempt = 0 @total_count_hints = 0 @count_plus = 0 @count_minus = 0 @length_code = 4 @min_num = 1 @max_num = 6 @count_attempt = 0 @count_hints = 0 @status = STATUS[:process_game] end
Public Instance Methods
difficulty_set(difficulty)
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 38 def difficulty_set(difficulty) return unless @difficulty_hash.key? difficulty @difficulty = difficulty @total_count_attempt = @difficulty_hash[difficulty][:total_count_attempt] @total_count_hints = @difficulty_hash[difficulty][:total_count_hints] end
gets_hint()
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 46 def gets_hint empty_result return add_error(ERRORS[:HintsEnd]) unless @total_count_hints > @count_hints return add_error(ERRORS[:NoCluesAvailable]) unless secret_code_rand @count_hints += 1 @hint = secret_code_rand.pop end
guess_code(code_attempt)
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 63 def guess_code(code_attempt) empty_result input_code = to_array(code_attempt) return @status = STATUS[:win] if input_code == secret_code return add_error(ERRORS[:WrongCode]) unless valid_code? input_code check_results(input_code) @count_attempt += 1 @status = STATUS[:lose] if @total_count_attempt <= @count_attempt end
string_secretcode()
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 34 def string_secretcode secret_code.join('') end
Private Instance Methods
add_error(error)
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 97 def add_error(error) (@error = error) && nil end
check_results(input_code)
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 77 def check_results(input_code) secret_code_temp = secret_code.map.with_index do |value,key| next input_code[key] = nil if value == input_code[key]; value end @count_plus = secret_code_temp.count(nil) secret_code_temp = secret_code_temp.map.with_index do |value, | next nil if value && input_code.include?(value) value end @count_minus = secret_code_temp.count(nil) - @count_plus end
empty_result()
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 90 def empty_result @error = nil @count_plus = 0 @count_minus = 0 @hint = 0 end
to_array(str)
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 105 def to_array(str) str.chars.map(&:to_i) end
valid_code?(input)
click to toggle source
# File lib/gem_codebreaker_amidasd/game.rb, line 101 def valid_code?(input) input.size == length_code && input.all? { |number| number.between? min_num, max_num } end