Sha256: 02712c66584bbfa4a680182743012a481cadf79a55c3225b308dd0e7eedf73f1

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

module AlexCodebreaker
  class Game
    include AlexCodebreaker::Modules::ArgumentsValidation

    STATUS = { in_game: :in_game, win: :win, lose: :lose }.freeze

    attr_reader :secret_code, :session, :game_id, :status, :used_hints, :current_comparison

    def initialize
      @secret_code = AlexCodebreaker::SecretCodeGenerator.new.secret_code
      @secret_code_for_hint = @secret_code.clone.uniq
      @session = Session.new
      @used_hints = []
      @status = STATUS[:in_game]
    end

    def hint
      return unless @session.check_hints

      process_hint
    end

    def guess(user_input)
      return unless guess_validation(user_input)

      compare_codes(user_input)
      check_win_lose
    end

    private

    def process_hint
      hint = if @secret_code_for_hint.one?
               @secret_code_for_hint.first
             else
               @secret_code_for_hint.delete(@secret_code_for_hint.sample)
             end
      @used_hints << hint
    end

    def compare_codes(user_input)
      user_input = format_user_input(user_input)
      comparison = Comparison.new(user_input, @secret_code.clone)
      @current_comparison = comparison.response
    end

    def format_user_input(user_input)
      user_input.chars.map(&:to_i)
    end

    def check_win_lose
      checking_result = @session.check_attempts
      return @status = STATUS[:win] if @current_comparison == AlexCodebreaker::Modules::Settings::WIN_COMPARISON

      @status = STATUS[:lose] unless checking_result
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alex_codebreaker-0.2.3 lib/alex_codebreaker/game.rb
alex_codebreaker-0.2.2 lib/alex_codebreaker/game.rb
alex_codebreaker-0.2.1 lib/alex_codebreaker/game.rb