So I was doing BlackJack project recently and I'm kinda stuck on Ace value procedure right now.
According to the rules player can assume that Ace can be 1 or 11, peace of cake if you have 2 cards, problem with 3 cards.
I have an array of 3 cards with 2 aces, it can be - [A, A, 9], [A, 9, A], [9, A, A]
How do I say to ruby that one of the Aces should have value 11 and another one 1? Elements of array are objects of class Card with value attribute.
UPDATE
I took one of the answers and edited it a little bit, so if I got no aces and score more than 21 I get not nil value
def score_array
aces, non_aces = current_cards.partition(&:ace?)
base_value = non_aces.sum(&:value) + aces.size
return base_value unless aces?
score_array = Array.new(aces.size + 1) { |high_aces| base_value + 10 * high_aces }
ace_value = score_array.select { |score| score <= 21 }.max
end
UPDATE 2
I've refactored method into 2 lines using one of the comments idea:
def count_score
@score = current_cards.sum(&:value)
@score += 10 if score < 12 && aces?
end
It is misleading to say that there is any player choice involved in the value of aces. The value of a blackjack hand is in fact fixed strictly by rule, which is quite simple:
That's it. No need to mess with individual card values, counting aces, or any other nonsense. Just write a simple function to return the value of the hand using the above rules.