class Codebreaker::Game
Constants
- AMOUNT_DIGITS
- DIFFICULTIES
- GUESS_CODE
- INCREMENT
- NEGATIVE_DIGIT
- POSITIVE_DIGIT
- RANGE_OF_DIGITS
Attributes
attempts_left[R]
attempts_total[R]
attempts_used[R]
date[R]
difficulty[R]
errors[RW]
got_hints[R]
have_hints[R]
hints_total[R]
hints_used[R]
name[R]
secret_code[R]
winner[R]
Public Instance Methods
attempt(input)
click to toggle source
# File lib/entities/game.rb, line 28 def attempt(input) @errors = [] return use_hint if hint?(input) converted = convert_to_array(input) return guessing(converted) if valid_input?(input, AMOUNT_DIGITS) miss_input && return end
game_options(user_difficulty:, player:)
click to toggle source
# File lib/entities/game.rb, line 20 def game_options(user_difficulty:, player:) @got_hints = '' @hints_used = 0 @attempts_used = 0 @name = player.name assign_difficulty(DIFFICULTIES[user_difficulty.downcase.to_sym]) end
miss_input()
click to toggle source
# File lib/entities/game.rb, line 37 def miss_input @errors << I18n.t(:when_incorrect_guess) && return end
remove_instance_helpers()
click to toggle source
# File lib/entities/game.rb, line 45 def remove_instance_helpers remove_instance_variable(:@winner) if @winner remove_instance_variable(:@errors) if @errors remove_instance_variable(:@hints_array) if @hints_array remove_instance_variable(:@have_hints) if @have_hints end
valid_difficulties?(input)
click to toggle source
# File lib/entities/game.rb, line 41 def valid_difficulties?(input) DIFFICULTIES.key?(input.to_sym) end
Private Instance Methods
assign_difficulty(difficulty_of_variables)
click to toggle source
# File lib/entities/game.rb, line 67 def assign_difficulty(difficulty_of_variables) @attempts_total = difficulty_of_variables[:attempts] @hints_total = difficulty_of_variables[:hints] @have_hints = difficulty_of_variables[:hints] @attempts_left = @attempts_total @difficulty = difficulty_of_variables[:difficulty] end
compare_with_right_code(user_code)
click to toggle source
# File lib/entities/game.rb, line 102 def compare_with_right_code(user_code) user_code == secret_code end
convert_to_array(input)
click to toggle source
# File lib/entities/game.rb, line 63 def convert_to_array(input) input.chars.map(&:to_i) end
count_attempt()
click to toggle source
# File lib/entities/game.rb, line 83 def count_attempt @attempts_left -= INCREMENT @attempts_used += INCREMENT end
count_tip()
click to toggle source
# File lib/entities/game.rb, line 93 def count_tip @have_hints -= INCREMENT @hints_used += INCREMENT @hints_array ||= secret_code.clone.shuffle hint = @hints_array.pop.to_s @got_hints += hint hint end
guessing(user_code)
click to toggle source
# File lib/entities/game.rb, line 106 def guessing(user_code) count_attempt @date = Time.new and return @winner = true if compare_with_right_code(user_code) pin = [] clone_secret_code = secret_code.clone complex_code = user_code.zip(secret_code) complex_code.map do |user_digit, secret_digit| next unless user_digit == secret_digit pin << POSITIVE_DIGIT user_code.delete_at(user_code.index(user_digit)) clone_secret_code.delete_at(clone_secret_code.index(secret_digit)) end clone_secret_code.each do |digit| next unless user_code.include? digit pin << NEGATIVE_DIGIT user_code.delete_at(user_code.index(digit)) end pin.sort.join('') end
hint?(input)
click to toggle source
# File lib/entities/game.rb, line 59 def hint?(input) input == GUESS_CODE[:hint] end
use_hint()
click to toggle source
# File lib/entities/game.rb, line 88 def use_hint @errors << I18n.t(:when_no_hints) && return unless @have_hints.positive? count_tip end
valid_input?(entity, length)
click to toggle source
# File lib/entities/game.rb, line 75 def valid_input?(entity, length) return unless validate_presence?(entity) return unless validate_length(entity, length) return unless validate_match(entity) valid_digits?(entity, RANGE_OF_DIGITS) end