Sha256: 84da0a8cc35e6b6bd21584ce157fd9614e0ed98fd76d40fbbcbc49a41146aa45

Contents?: true

Size: 799 Bytes

Versions: 5

Compression:

Stored size: 799 Bytes

Contents

module CardsLib
  module Standard
    module Evaluators
      class BlackjackEvaluator
        def initialize(cards)
          @cards = cards
          @reduced_ace_count = 0
        end
        
        def worth
          @worth ||= @cards.map(&:value).inject(:+)
          if @worth > 21
            attempt = reduce_by_ace
            until @worth < 22 || !attempt
              attempt = reduce_by_ace
            end
          end 
          @worth
        end

        private
        def reduce_by_ace
          if count_aces > @reduced_ace_count
            @worth -= 10
            @reduced_ace_count += 1
            true
          else
            false
          end
        end

        def count_aces
          @cards.count {|c| c.rank == "A"}
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cards_lib-0.2.4 lib/cards_lib/standard/evaluators/blackjack_evaluator.rb
cards_lib-0.2.3 lib/cards_lib/standard/evaluators/blackjack_evaluator.rb
cards_lib-0.2.2 lib/cards_lib/standard/evaluators/blackjack_evaluator.rb
cards_lib-0.2.1 lib/cards_lib/standard/evaluators/blackjack_evaluator.rb
cards_lib-0.2.0 lib/cards_lib/standard/evaluators/blackjack_evaluator.rb