public bool CanFit(Card card, out Card Joker) { Joker = null; if (card.IsJoker()) { return(card.Color == Color && Cards.Count < 14); } var jokers = Cards.Where(c => c.IsJoker()); if (card.Suit != Suit || (Cards.Count == 14 && !jokers.Any())) { return(false); } // Check whether the new card replaces a joker foreach (var joker in jokers) { var jokerRank = CardUtil.GetJokerRank(Cards, Cards.IndexOf(joker)); if (jokerRank == card.Rank) { Joker = joker; return(true); } } return((HighestRank != Card.CardRank.ACE && card.Rank == HighestRank + 1) || (LowestRank != Card.CardRank.ACE && card.Rank == LowestRank - 1) || (LowestRank == Card.CardRank.TWO && card.Rank == Card.CardRank.ACE)); }
private void CalculateValue() { int value = 0; for (int i = 0; i < Count; i++) { var rank = Cards[i].Rank; if (rank == Card.CardRank.JOKER) { if (i == 0 && Cards.Count == 1) { // The joker is the first and only card in the run, // which will have value zero for now break; } var jokerRank = CardUtil.GetJokerRank(Cards, i); // If the next card is a TWO, jokerRank is RANK(2-1)=Rank(1)=JOKER if (jokerRank == Card.CardRank.JOKER) { // JOKER is ACE, ACE counts 1 in ACE-2-3 value += 1; } else { value += Card.CardValues[jokerRank]; } } else if (rank == Card.CardRank.ACE && i == 0) { // ACE counts 1 in ACE-2-3 value += 1; } else { value += Cards[i].Value; } } Value = value; }
public override void AddCard(Card card) { // By default, add the new card at the end int idx = Objects.Count; // If the run is empty or only contains joker cards, also add the new card at the end // otherwise if (Type == SpotType.RUN && Objects.Count(c => !c.IsJoker()) > 0) { // Find out the rank of the last card in the run int highestNonJokerIdx = Objects.GetFirstCardIndex(Objects.Count - 1, false); var highestNonJokerRank = Objects[highestNonJokerIdx].Rank; var highestRank = highestNonJokerRank + (Objects.Count - 1 - highestNonJokerIdx); if (highestNonJokerIdx == 0 && highestNonJokerRank == Card.CardRank.ACE) { highestRank = (Card.CardRank)Objects.Count; } // If adding ACE after King is not possible, add ACE at beginning if (card.Rank == Card.CardRank.ACE && (highestRank < Card.CardRank.KING || (highestRank == Card.CardRank.ACE && !Objects[Objects.Count - 1].IsJoker()))) { idx = 0; } else if (card.IsJoker()) // Joker will be added at the end, if possible { if (highestRank == Card.CardRank.ACE) { idx = 0; } else { idx = Objects.Count; } } else // Any other case, the card will be sorted by rank { for (int i = 0; i < Objects.Count; i++) { var rank = Objects[i].Rank; if (Objects[i].IsJoker()) // Figure out the rank of the card which the joker is replacing { if (i == 0 && Objects.Count == 1) { // Joker is the only card in the run and the next card comes after the joker idx = 1; break; } rank = CardUtil.GetJokerRank(Objects, i); } else if (i == 0 && rank == Card.CardRank.ACE) { rank = (Card.CardRank) 1; // Although it's not actually a Joker } if (rank > card.Rank) { idx = i; break; } } } } AddCard(card, idx); }