private Result <int, ApplicationError> CountFifteens(Card[] cards) => Result.Try(() => Enumerable.Range(0, 1 << cards.Length) .Select(o => Enumerable.Range(0, cards.Length) .Where(p => (o & (1 << p)) != 0) .Select(q => cards[q])) .Where(s => s.Sum(r => r.Face.GetFaceValue()) == 15).Count()) .MapOnFailure(ex => GameLogicError.Create($"Error counting 15s: {ex.Message}", ex, ErrorCodes.HandErrorCode.Create("15s")));
private Result <Card[], ApplicationError> CreateCards() => Result.Try(() => { return((from suits in EnumHelper.EnumerateEnum <Suits>() from faces in EnumHelper.EnumerateEnum <Faces>() select Card.Create(suits, faces)).ToArray()); }) .MapOnFailure(ex => GameLogicError.Create($"Error creating deck of cards: {ex.Message}", ex, ErrorCodes.DeckErrorCode.Create(ex.Message)));
private Result <int, ApplicationError> CalculateHandFlush(Card[] hand, Card cutCard) => Result.Try(() => hand.All(c => c.Suit == hand[0].Suit) ? hand .Append(cutCard) .All(c => c.Suit == hand[0].Suit) ? hand.Length + 1 : hand.Length : 0) .MapOnFailure(ex => GameLogicError.Create($"Error calculating a flush in the hand", ex, ErrorCodes.HandErrorCode.Create("Flush")));
private Result <Card[], ApplicationError> ShuffleCards(Card[] cards) => Result.Try(() => { for (int n = cards.Length - 1; n >= 0; --n) { var k = Random.Next(n + 1); var temp = cards[n]; cards[n] = cards[k]; cards[k] = temp; } return(cards); }) .MapOnFailure(ex => GameLogicError.Create($"Error shuffling deck of cards: {ex.Message}", ex, ErrorCodes.DeckErrorCode.Create(ex.Message)));
private Result <int[], ApplicationError> CreateArrayOfDuplicates(Card[] cards) => Result.Try(() => { var duplicates = new int[14]; for (var i = 0; i < duplicates.Length; ++i) { duplicates[i] = 0; } foreach (var card in cards) { duplicates[(int)card.Face]++; } return(duplicates); }) .MapOnFailure(ex => GameLogicError.Create($"Error creating array of duplicates: {ex.Message}", ex, ErrorCodes.HandErrorCode.Create("Pairs")));
private Result <Card[], ApplicationError> CombineCards(Card[] hand, Card cutCard) => Result.Try(() => hand.Append(cutCard).ToArray()) .MapOnFailure(ex => GameLogicError.Create($"Error combining hand and cut cards: {ex.Message}", ex, ErrorCodes.HandErrorCode.Create(ex.Message)));
private Result <int, ApplicationError> SumPairs(int[] duplicates) => Result.Try(() => duplicates.Sum(d => d * (d - 1))) .MapOnFailure(ex => GameLogicError.Create($"Error summing points for pairs: {ex.Message}", ex, ErrorCodes.HandErrorCode.Create("Pairs")));
public Result <int, ApplicationError> Calculate(Card[] hand, Card cutCard) => Result.Try(() => hand.Where(c => c.Face == Faces.Jack && c.Suit == cutCard.Suit).Count()) .MapOnFailure(ex => GameLogicError.Create($"Error calculating Nobs points: {ex.Message}", ex, ErrorCodes.HandErrorCode.Create("Nobs")));
private Result <int, ApplicationError> CalculateCribFlush(Card[] cards) => Result.Try(() => cards.All(c => c.Suit == cards[0].Suit) ? cards.Length : 0) .MapOnFailure(ex => GameLogicError.Create($"Error calculating a flush in the crib", ex, ErrorCodes.HandErrorCode.Create("Flush")));