Sha256: 2916e65943db3930c665552217685a75a929655dbaaca477a259d2902bd6068b
Contents?: true
Size: 1.79 KB
Versions: 2
Compression:
Stored size: 1.79 KB
Contents
# frozen_string_literal: true require_relative 'config' module CodebreakerDiz class Game attr_reader :tries_count, :hints_count def initialize(difficulty: :kid) validate_difficulty(difficulty) @difficulty = difficulty @hint_indexes = (0...CODE_LENGTH).to_a @secret = Array.new(CODE_LENGTH) { rand(MIN_CODE_NUMBER..MAX_CODE_NUMBER) } @tries_count = DIFFICULTIES[@difficulty][:tries] @hints_count = DIFFICULTIES[@difficulty][:hints] @matches = '' end def validate_difficulty(difficulty) raise ArgumentError, I18n.t(:INVALID_DIFFICULTY) unless DIFFICULTIES.include? difficulty.to_sym end def check_guess(input) input = to_array(input) return I18n.t(:WRONG_FORMAT) unless valid? input @tries_count -= 1 @matches = Matcher.new(@secret, input).matches end def generate_hint return I18n.t(:NO_HINTS_LEFT) if @hints_count.zero? index = @hint_indexes.sample hint = @secret[index] @hint_indexes.delete index @hints_count -= 1 hint end def win? @matches == Array.new(CODE_LENGTH, EXACT_MATCH_SIGN).join end def lose? @tries_count.zero? end def data { difficulty: DIFFICULTIES.keys.index(@difficulty), secret: @secret, tries_total: DIFFICULTIES[@difficulty][:tries], hints_total: DIFFICULTIES[@difficulty][:hints], tries_used: DIFFICULTIES[@difficulty][:tries] - @tries_count, hints_used: DIFFICULTIES[@difficulty][:hints] - @hints_count } end private def to_array(input) input.to_i.digits.reverse end def valid?(input) input.size == CODE_LENGTH && input.all? { |number| number.between? MIN_CODE_NUMBER, MAX_CODE_NUMBER } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
codebreaker_diz-0.2.5 | lib/codebreaker_diz/game.rb |
codebreaker_diz-0.2.4 | lib/codebreaker_diz/game.rb |