// if it qualifies, this will re-arrange the cards in the hand in the order that // the cards should be compared in the case of a tie void CheckIfQualifies(HandWrapper handWrapper) { // if all suits match (or if it's wild) handWrapper.WorkingCards.Sort(); if (handWrapper.WorkingCards.All(x => x.Suit == handWrapper.WorkingCards.First().Suit || x.Suit == Card.Suits.Wild)) { handWrapper.WinningCards = handWrapper.WorkingCards; handWrapper.WinningCards.Sort(); handWrapper.Result = Ranker.CompareResult.Win; } return; }
// if it qualifies, this will re-arrange the cards in the hand in the order that // the cards should be compared in the case of a tie void CheckIfQualifies(HandWrapper handWrapper) { // sort non-joker cards low-to-high, and count joker cards List <Card> nonJokerCards = handWrapper.WorkingCards.Where(x => x.Number != Card.Numbers.Joker).ToList(); nonJokerCards.Sort(); int numJokers = handWrapper.WorkingCards.Count(x => x.Number == Card.Numbers.Joker); for (int i = 0; i < nonJokerCards.Count - 1; i++) { Card c1 = nonJokerCards[i]; Card c2 = nonJokerCards[i + 1]; // get value difference between two cards int valueDifference = (int)c2.Number - (int)c1.Number; // if there's duplicate values, fail if (valueDifference == 0) { handWrapper.Result = Ranker.CompareResult.Lose; return; } // see if hand has joker(s) to fill the gap by adding jacks until it runs out int posToInsertJack = 1; while (valueDifference > 1 && numJokers >= valueDifference - 1) { valueDifference--; numJokers--; // insert a card of the correct value where the joker is being used (suit doesn't matter) nonJokerCards.Insert(i + posToInsertJack, new Card((Card.Numbers)((int)nonJokerCards[i].Number + posToInsertJack), Card.Suits.Club)); posToInsertJack++; } // if the difference between cards is still larger than 1, fail if (valueDifference > 1) { handWrapper.Result = Ranker.CompareResult.Lose; return; } } handWrapper.WinningCards = handWrapper.WorkingCards; handWrapper.RemainingCards = nonJokerCards; // put resolved cards (jokers replaced with numbers) into remaining cards list to be used for tie breaking handWrapper.Result = Ranker.CompareResult.Win; return; }
// if it qualifies, this will set the hand's "Result" to "Win", and will also // re-arrange the cards in the hand in the order that the cards should be compared in the case of a tie void CheckIfQualifies(HandWrapper handWrapper, int numRequirement) { // first, check special case for all jokers (succeed) if (handWrapper.WorkingCards.All(x => x.Number == Card.Numbers.Joker)) { handWrapper.Result = Ranker.CompareResult.Win; handWrapper.WinningCards = handWrapper.WorkingCards; handWrapper.RemainingCards = new List <Card>(); return; } // sort highest first handWrapper.WorkingCards.Sort(); handWrapper.WorkingCards.Reverse(); foreach (var card in handWrapper.WorkingCards) { // don't start with a joker // (or else everything will say it's equivalent and you'll get a false-positive) if (card.Number == Card.Numbers.Joker) { continue; } // if there's a match of the required number of cards List <Card> qualifyingCards = handWrapper.WorkingCards.Where(x => x == card).ToList(); if (qualifyingCards.Count() >= numRequirement) { handWrapper.Result = Ranker.CompareResult.Win; // organize qualifying hand to check for tie later handWrapper.WinningCards = qualifyingCards; handWrapper.RemainingCards = handWrapper.WorkingCards.Where(x => x != card).ToList(); return; } } return; }
public List <Hand> Compare(List <Hand> handsToCompare) { Logger.Log($"Entering criteria check for {WinName} with {handsToCompare.Count} hands."); // create the hand wrappers from the given hands List <HandWrapper> handWrappers = new List <HandWrapper>(); handsToCompare.ForEach(x => handWrappers.Add(new HandWrapper(x))); // first check to see if each hand passes the match criteria for (int i = 0; i < handWrappers.Count; i++) { handWrappers[i].Result = Ranker.CompareResult.None; CheckIfQualifies(handWrappers[i], numberRequirement); } // next check if any met criteria handWrappers.RemoveAll(x => x.Result != Ranker.CompareResult.Win); if (!handWrappers.Any()) // if there are no winners, return null { Logger.Log($"Exiting criteria check for {WinName} with no winner."); return(null); } else if (numberRequirementSecond > 0) // if needs to look for a 2nd match in remaining cards... { Logger.Log($"Checking for secondary match of {numberRequirementSecond} cards."); // copy remaining cards from the already winning hands to a new temporary working card list List <HandWrapper> localCopyOfHands = new List <HandWrapper>(); foreach (var winningHand in handWrappers) { localCopyOfHands.Add(new HandWrapper(new Hand(winningHand.Hand.Name, winningHand.RemainingCards))); } // check to see if each one qualifies for the 2nd match for (int i = 0; i < localCopyOfHands.Count; i++) { CheckIfQualifies(localCopyOfHands[i], numberRequirementSecond); } // remove any that didn't meet criteria localCopyOfHands.RemoveAll(x => x.Result != Ranker.CompareResult.Win); if (!localCopyOfHands.Any()) { Logger.Log($"Exiting criteria check for {WinName} with no winner."); return(null); } // remove any winningHandWrappers that don't have a matching localCopyOfHands handWrappers.RemoveAll(x => !localCopyOfHands.Any(y => y.Hand.Name == x.Hand.Name)); // for each of the hands with a primary match foreach (var winningHandWrapper in handWrappers) { HandWrapper secondaryHandWrapper = localCopyOfHands.Single(x => x.Hand.Name == winningHandWrapper.Hand.Name); // put in its respective winning/remaining lists to be compared as a final tie-breaker winningHandWrapper.WinningCards = winningHandWrapper.WinningCards.Where(x => x.Number != Card.Numbers.Joker).ToList(); winningHandWrapper.RemainingCards = secondaryHandWrapper.WinningCards; } } else if (handWrappers.Count() == 1) // or if there's a single winner, return results { Logger.Log($"Exiting criteria check for {WinName} with a winner (immediately)."); return(handWrappers.Select(x => x.Hand).ToList()); } // try to break the tie with best winning cards List <HandWrapper> winningTiedHandWrappers = DetermineHighestWinners(handWrappers); if (winningTiedHandWrappers.Count == 1) { Logger.Log($"Exiting criteria check for {WinName} with a winner (tie breaker with higher match)."); return(winningTiedHandWrappers.Select(x => x.Hand).ToList()); } // break the tie with remaining cards List <HandWrapper> winningRemainingTiedHandWrappers = WinCriteriaHelpers.FindHighestHandByRemainingCards(winningTiedHandWrappers); if (winningRemainingTiedHandWrappers.Count > 0) { Logger.Log($"Exiting criteria check for {WinName} with winner(s) (tie breaker with higher kickers)."); return(winningRemainingTiedHandWrappers.Select(x => x.Hand).ToList()); } Logger.Log($"Exiting criteria check for {WinName} with no winner."); return(null); }