void Start() { // string st = ""; // for (int i = 0; i < 52; i++) { // PhomCard pCard = new PhomCard (i); // st += " " + pCard.ToString (); // } // Debug.Log (st); // // PhomCard pc = new PhomCard ("TC"); // Debug.Log (pc.ToIndex ()); // PhomCombination comb = new PhomCombination ("AS AH AD 2D 3D 7H 9D TD JD QD KS KH KC"); // Debug.Log (comb.GetTypeName ()); PhomCard c = new PhomCard("TC"); PhomCard c2 = new PhomCard("4C"); Debug.Log(c.GetRank() + " " + c2.GetRank()); PhomHand hand = new PhomHand(); hand.AddCards("AS AH 2C 2D 3C KS QH KH KC KD"); // hand.FindCombinations (); // hand.Arrange (); Debug.Log("Best:" + hand.Best()); }
bool CheckBestScore(PhomHand hand, ref int bestScore, bool shouldCheckBest) { int tmpScore = shouldCheckBest? hand.Best() : hand.ComputeScore(false); if (tmpScore <= bestScore) { bestScore = tmpScore; } return(bestScore == 0); }
bool BestSubHandWithNoCardInTwoSet(CardSet subCardSet, ref int bestScore) { PhomHand hand = new PhomHand(SubSetList(0, numCards)); foreach (Card c in subCardSet.cards) { hand.RemoveCard(c); } hand.FindCombinations(); return(CheckBestScore(hand, ref bestScore, false)); // int tmpScore = hand.ComputeScore (false); // if (tmpScore < bestScore) { // bestScore = tmpScore; // } // // return bestScore == 0; }
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); }