```On 6 Jan 2008, at 23:54, Denis Hennessy wrote:

> On 6 Jan 2008, at 22:40, James Gray wrote:
>
>> On Jan 6, 2008, at 3:03 PM, Denis Hennessy wrote:
>>
>>> The understanding I had was that a dealer could 're-value' their
>>> hand with each card dealt; that is - if they draw two aces they're
>>> obviously 11 and 1 and they have to hit again. If they then draw a
>>> card which would cause them to bust, they can treat both aces as 1.
>>
>> You have all of that right, but Eric was saying you don't properly
>> value hands with multiple aces in them.  For example, ace, ace, and
>> ten should value as 12, not 22.
>>
>> One way I've handled this in the pass was to sort the hand such
>> that aces are at the end, run through building up a total, and
>> count an ace as 11 when I passed it if doing so would keep the
>> running count less than or equal to 21 minus the cards left to count.
>>
>> Hope that makes sense.
>>
>> James Edward Gray II
>>
>
> Actually, I think my code is correct, but I might be snow-blind.
> Here's the relevant function. It values all the non-ace cards first
> and then values the aces.
>
> def hand_value(hand)

... original function ...
>
> end
>

Gah! There was a bug - it valued aces after other cards but didn't
take into account other aces. Here's the corrected function which
works similarly to Eric's code (value aces as 11 and later re-value as
1 as needed):

def hand_value(hand)
value = 0
# First calculate values counting aces as 11
hand.each do |c|
if c=='A'
value += 11
elsif 'JQK'.include? c
value += 10
else
value += c.to_i
end
end
# Then re-value aces as 1 as long as hand is bust
hand.each do |c|
if c=='A'
if value>21
value -= 10
end
end
end
value
end

```