public static double HandValue(Deck deck) { int val1 = deck.Sum(c => c.Value); double aces = deck.Count(c => c.ID == "A"); double val2 = aces > 0 ? val1 + (10 * aces) : val1; return(new double[] { val1, val2 } .Select(handVal => new { handVal, weight = Math.Abs(handVal - 21) + (handVal > 21 ? 100 : 0) }) .OrderBy(n => n.weight) .First().handVal); }
// Deal two cards to each player public void DealHands() { if (!IsActionAllowed(Action.Deal)) { throw new InvalidOperationException(); } this.LastState = State.Unknown; // Build a new deck of cards before deal if less than 15 cards left in deck if ((this.deck == null) || (deck.Count() < GameParameters.DeckLowCardCount)) { this.deck = new Deck(); } this.DealNewHands(); // Deal hand to player and dealer this.PlayTheDeal(); }