class CodebreakerRuban::Game
Constants
- DIFFICULTY
- LENGTH_GUESS
- RANGE_SECRET_CODE
Attributes
attempts_total[R]
attempts_used[RW]
datetime[R]
difficulty[R]
errors[RW]
guess_code[RW]
hints_showed[R]
hints_total[R]
hints_used[RW]
secret_code[R]
user[R]
Public Class Methods
new(difficulty, user)
click to toggle source
# File lib/app/entities/game.rb, line 17 def initialize(difficulty, user) @user = user @difficulty = difficulty[:difficulty] @secret_code = generator_secret_code @attempts_total = difficulty[:attempts_total] @hints_total = difficulty[:hints_total] @hint = secret_code.clone.shuffle @hints_used = 0 @attempts_used = 0 @hints_showed = [] @datetime = Time.now @guess_code = nil @errors = [] end
Public Instance Methods
check_attemps()
click to toggle source
# File lib/app/entities/game.rb, line 38 def check_attemps @attempts_used += 1 @attempts_used != @attempts_total end
check_user_input(input)
click to toggle source
# File lib/app/entities/game.rb, line 43 def check_user_input(input) input = transform_in_array(input) result = [] secret_arr = secret_code.clone user_arr = input.clone check_plus(user_arr, secret_arr, result).compact! check_minus(user_arr, secret_arr, result) [result].join('') end
hint_use()
click to toggle source
# File lib/app/entities/game.rb, line 53 def hint_use if @hints_used < @hints_total @hints_used += 1 @hints_showed << @hint.pop else clear_errors errors << Message.error_guess_code end end
to_h()
click to toggle source
# File lib/app/entities/game.rb, line 63 def to_h { name: @user.name.capitalize, difficulty: @difficulty, attempts_used: @attempts_used, attempts_total: @attempts_total, hints_total: @hints_total, hints_used: @hints_used } end
validate_guess_input(input)
click to toggle source
# File lib/app/entities/game.rb, line 32 def validate_guess_input(input) clear_errors return errors << Message.error_guess_code unless validate_range_lenght(input, LENGTH_GUESS) return errors << Message.error_guess_code unless check_number(transform_in_array(input), RANGE_SECRET_CODE) end
Private Instance Methods
check_minus(user_arr, secret_arr, minus)
click to toggle source
# File lib/app/entities/game.rb, line 86 def check_minus(user_arr, secret_arr, minus) user_arr.each_with_index do |number, index| next unless secret_arr.include? number minus << '-' index_s = secret_arr.find_index number user_arr[index] = nil secret_arr[index_s] = nil end end
check_plus(user_arr, secret_arr, plus)
click to toggle source
# File lib/app/entities/game.rb, line 76 def check_plus(user_arr, secret_arr, plus) user_arr.each_index do |index| next unless user_arr[index] == secret_arr[index] plus << '+' user_arr[index] = nil secret_arr[index] = nil end end
clear_errors()
click to toggle source
# File lib/app/entities/game.rb, line 101 def clear_errors errors.clear end
generator_secret_code()
click to toggle source
# File lib/app/entities/game.rb, line 105 def generator_secret_code Array.new(LENGTH_GUESS) { Random.rand(RANGE_SECRET_CODE) } end
transform_in_array(arg)
click to toggle source
# File lib/app/entities/game.rb, line 97 def transform_in_array(arg) arg.split('').map(&:to_i) end