On Oct 22, 2005, at 9:14 PM, David Vincelli wrote: > I'm writing a little BlackJack program for fun and I'm at the point > where I have to decide which point value to assign to a hand. As you > know, an Ace may be worth 1 or 11 in this game - whatever is closer to > 21 without going over. > > Considering this, I know I have to come up with an algorithm that > first determines what the different point values may be (the extreme > case may be that a player got all four aces, now I'd have to determine > all combinations of the players cards). > > This demonstrates some of the code I wrote: > > pl = Player.new > pl.hand.each { |card| p pl.card.value } > > might produce this output: > [1, 11] > [5] > [10] > > For the above example, so what I'm looking for is a method that will > calculate all possible point totals. For this example I'd get the > results [16, 26] > > I'm thinking I might do this by first building a binary tree, creating > unique branches that would add up to the results when I applied some > recursive algorithm to it. But I'm curious to know how other people > might do this? (I'm especially curious to see if anyone has some > solution that does not require building a tree and recursing..). Well, two aces counting as 11 total to 22 which is bigger than 21 and so you can never have more than one ace count as 11. I'd just keep an extra bit of information: do we have an ace? If we do then we may add 10 (just once) to the total when the total <= 11 (and where the total counts each ace as 1). This won't give you *all* totals just the best legal one. > > Thanks, > > -- > David Vincelli > > ---- Bob Hutchison -- blogs at <http://www.recursive.ca/hutch/> Recursive Design Inc. -- <http://www.recursive.ca/> Raconteur -- <http://www.raconteur.info/>