public int ComputeScore(bool auto = true) { int score = 0; if (auto) { return(Best()); } // No single cards => U. if (singleCards.Length() == 0) { return(0); } // No combinations => Mom. else if (singleCards.Length() == Length() && Length() >= 9) { return(1000); } Debug.Log("Compute:" + ToCardString() + " ====" + singleCards.ToCardString()); foreach (PhomCard card in singleCards.cards) { score += card.GetRank() + 1; } return(score); }
public CardSet FindRankSetCombination(PhomCard card) { CardSet combination = new CardSet(); foreach (PhomCard c in cards) { if (card.HasSameRank(c)) { combination.AddCard(c); } } if (combination.Length() <= 2) { combination.Reset(); } else { combination.Sort(Order.ASC); } return(combination); }
public CardSet FindStraightCombination(PhomCard card) { CardSet combination = new CardSet(); int topRank = card.GetRank(); int bottomRank = topRank; int index = cards.IndexOf(card); for (int i = index - 1; i >= 0; i--) { if (card.HasSameSuit(cards [i]) && cards [i].GetRank() == bottomRank - 1) { combination.AddCard(cards [i]); bottomRank--; } } combination.AddCard(card); for (int i = index + 1; i < cards.Count; i++) { if (card.HasSameSuit(cards [i]) && cards [i].GetRank() == topRank + 1) { combination.AddCard(cards [i]); topRank++; } } if (combination.Length() <= 2) { combination.Reset(); } else { combination.Sort(Order.ASC); } return(combination); }
bool BestSubHand(CardSet subCardSet, int exceptIndex, ref int bestScore) { PhomHand hand = new PhomHand(SubSetList(0, numCards)); if (exceptIndex == -1) { foreach (Card c in subCardSet.cards) { hand.RemoveCard(c); } } else { for (int n = 0; n < subCardSet.Length(); n++) { if (n != exceptIndex) { hand.RemoveCard(subCardSet.At(n)); } } } return(CheckBestScore(hand, ref bestScore, true)); }
public int Best() { Sort(Order.ASC); CardSet oneSetCards = new CardSet(); CardSet twoSetsCards = new CardSet(); ClassifyCards(ref oneSetCards, ref twoSetsCards); int bestScore = 1000; // There is at least one card which is in two sets. if (twoSetsCards.Length() > 0) { int tmpScore = 0; for (int i = 0; i < twoSetsCards.Length(); i++) { // Debug.Log ("Rank set:" + ToCardString ()); // First check same rank set. CardSet rankSet = FindRankSetCombination((PhomCard)twoSetsCards.At(i)); List <int> overlappedIndice = new List <int> (); if (rankSet.Length() == 4) { for (int j = 0; j < twoSetsCards.Length(); j++) { if (i != j && rankSet.Contains(twoSetsCards.At(j))) { overlappedIndice.Add(j); break; } } } // Two cards in the same rank set. Quad set. if (overlappedIndice.Count > 0) { for (int m = 0; m < overlappedIndice.Count; m++) { // Keep all in the rank set. if (BestSubHand(rankSet, -1, ref bestScore)) { return(0); } // Remove the overlapped out of rank set. if (BestSubHand(rankSet, overlappedIndice [m], ref bestScore)) { return(0); } } } else { if (BestSubHand(rankSet, -1, ref bestScore)) { return(0); } } // Check straight set. // Debug.Log ("Straight set:" + ToCardString ()); CardSet straightSet = FindStraightCombination((PhomCard)twoSetsCards.At(i)); // Debug.Log ("Straight set2:" + straightSet.ToCardString ()); int mainIndex = straightSet.cards.IndexOf(twoSetsCards.At(i)); overlappedIndice.Clear(); for (int j = 0; j < twoSetsCards.Length(); j++) { if (i != j && straightSet.Contains(twoSetsCards.At(j))) { overlappedIndice.Add(j); break; } } // Two cards in the same straight. if (overlappedIndice.Count > 0) { for (int m = 0; m < overlappedIndice.Count; m++) { // Keep the overlapped card in the straight hand <=> remove it from the remaining. if (BestSubHand(straightSet, -1, ref bestScore)) { return(0); } // Remove the overlapped card in the set <=> not remove it from the remaining. if ((int)Mathf.Abs(mainIndex - overlappedIndice [m]) > 2) { PhomHand straightHand = new PhomHand(SubSetList(0, numCards)); if (mainIndex < overlappedIndice [m]) { for (int k = overlappedIndice [m]; k < straightSet.Length(); k++) { straightHand.RemoveCard(straightSet.At(k)); } } else { for (int k = 0; k <= overlappedIndice [m]; k++) { straightHand.RemoveCard(straightSet.At(k)); } } if (CheckBestScore(straightHand, ref bestScore, true)) { return(0); } } } } // else { if (BestSubHand(straightSet, -1, ref bestScore)) { return(0); } } } } // No card in two sets. else if (oneSetCards.Length() > 0) { int tmpScore = 0; // Debug.Log ("++++++++:" + oneSetCards.ToCardString () + " " + oneSetCards.Length ()); for (int i = 0; i < oneSetCards.Length(); i++) { // First check same rank set. CardSet rankSet = FindRankSetCombination((PhomCard)oneSetCards.At(i)); // Debug.Log ("++++++++2:" + rankSet.ToCardString ()); if (rankSet.Length() > 0 && BestSubHandWithNoCardInTwoSet(rankSet, ref bestScore)) { return(0); } // Check straight set. CardSet straightSet = FindStraightCombination((PhomCard)oneSetCards.At(i)); // Debug.Log ("Set one: straight set:" + straightSet.ToCardString ()); if (straightSet.Length() > 0 && BestSubHandWithNoCardInTwoSet(straightSet, ref bestScore)) { return(0); } } } else { // Debug.Log ("no set found:" + ToCardString ()); singleCards.cards.AddRange(cards); if (CheckBestScore(this, ref bestScore, false)) { return(0); } } // Debug.Log ("bestScore:" + bestScore); return(bestScore); }