//确定谁的牌最大,支持部分玩家出了牌 public static string GetWinnerWithoutAllShowedCards(CurrentTrickState trickState) { var cp = new CurrentPoker[4]; Suit trump = trickState.Trump; var trumpInt = (int)trickState.Trump; int rank = trickState.Rank; cp[0] = new CurrentPoker(trickState.ShowedCards[trickState.Learder], trumpInt, rank); string nextPlayer1 = trickState.NextPlayer(trickState.Learder); cp[1] = new CurrentPoker(trickState.ShowedCards[nextPlayer1], trumpInt, rank); string nextPlayer2 = trickState.NextPlayer(nextPlayer1); cp[2] = new CurrentPoker(trickState.ShowedCards[nextPlayer2], trumpInt, rank); string nextPlayer3 = trickState.NextPlayer(nextPlayer2); cp[3] = new CurrentPoker(trickState.ShowedCards[nextPlayer3], trumpInt, rank); //cp[0].Sort(); //cp[1].Sort(); //cp[2].Sort(); //cp[3].Sort(); int leadingCardsCount = trickState.ShowedCards[trickState.Learder].Count; int winderNumber = 0; var leadingSuit = (int)trickState.LeadingSuit; List <int> leadingTractor = cp[0].GetTractorOfAnySuit(); //甩牌 拖拉机 if ((leadingTractor.Count > 1) && (cp[0].Count > leadingTractor.Count * 2)) //甩拖拉机 { int maxCard = leadingTractor[0]; if (trickState.ShowedCards[nextPlayer1].Count > 0) { List <int> tractor1 = cp[1].GetTractor(trump); if (tractor1.Count >= leadingTractor.Count && (!cp[1].IsMixed() && cp[1].GetPairs().Count >= cp[0].GetPairs().Count)) { int tmpMax = tractor1[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 1; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer2].Count > 0) { List <int> tractor2 = cp[2].GetTractor(trump); if (tractor2.Count >= leadingTractor.Count && (!cp[2].IsMixed() && cp[2].GetPairs().Count >= cp[0].GetPairs().Count)) { int tmpMax = tractor2[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 2; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer3].Count > 0) { List <int> tractor3 = cp[3].GetTractor(trump); if (tractor3.Count >= leadingTractor.Count && (!cp[3].IsMixed() && cp[3].GetPairs().Count >= cp[0].GetPairs().Count)) { int tmpMax = tractor3[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 3; } } } } //甩牌 对 else if ((2 < leadingCardsCount) && (cp[0].GetPairs().Count > 0) && leadingTractor.Count < 2) { int maxCard = CommonMethods.GetMaxCard(cp[0].GetPairs(), trump, rank); if (trickState.ShowedCards[nextPlayer1].Count > 0) { if (cp[1].GetPairs().Count >= cp[0].GetPairs().Count&& (!cp[1].IsMixed())) { int tmpMax = CommonMethods.GetMaxCard(cp[1].GetPairs(), trump, rank); if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 1; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer2].Count > 0) { if (cp[2].GetPairs().Count >= cp[0].GetPairs().Count&& (!cp[2].IsMixed())) { int tmpMax = CommonMethods.GetMaxCard(cp[2].GetPairs(), trump, rank); if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 2; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer3].Count > 0) { if (cp[3].GetPairs().Count >= cp[0].GetPairs().Count&& (!cp[3].IsMixed())) { int tmpMax = CommonMethods.GetMaxCard(cp[3].GetPairs(), trump, rank); if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 3; } } } } //甩多个单张牌 else if ((leadingCardsCount > 1) && (cp[0].GetPairs().Count == 0)) { int maxCard = CommonMethods.GetMaxCard(trickState.ShowedCards[trickState.Learder], trump, rank); int tmpMax = 0; if (trickState.ShowedCards[nextPlayer1].Count > 0) { tmpMax = CommonMethods.GetMaxCard(trickState.ShowedCards[nextPlayer1], trump, rank); if (cp[1].GetSuitCardsWithJokerAndRank(trumpInt).Count() == trickState.ShowedCards[trickState.Learder].Count) { if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 1; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer2].Count > 0) { if (cp[2].GetSuitCardsWithJokerAndRank(trumpInt).Count() == trickState.ShowedCards[trickState.Learder].Count) { tmpMax = CommonMethods.GetMaxCard(trickState.ShowedCards[nextPlayer2], trump, rank); if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 2; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer3].Count > 0) { if (cp[3].GetSuitCardsWithJokerAndRank(trumpInt).Count() == trickState.ShowedCards[trickState.Learder].Count) { tmpMax = CommonMethods.GetMaxCard(trickState.ShowedCards[nextPlayer3], trump, rank); if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 3; } } } } //拖拉机 else if (leadingTractor.Count > 1) { //如果有拖拉机 List <int> tractor0 = cp[0].GetTractorOfAnySuit(); List <int> tractor1 = null; List <int> tractor2 = null; List <int> tractor3 = null; if (trickState.ShowedCards[nextPlayer1].Count > 0) { tractor1 = cp[1].GetTractorOfAnySuit(); } if (trickState.ShowedCards[nextPlayer2].Count > 0) { tractor2 = cp[2].GetTractorOfAnySuit(); } if (trickState.ShowedCards[nextPlayer3].Count > 0) { tractor3 = cp[3].GetTractorOfAnySuit(); } int maxCard = tractor0[0]; if (trickState.ShowedCards[nextPlayer1].Count > 0) { if (tractor1.Count >= tractor0.Count) { int tmpMax = tractor1[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 1; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer2].Count > 0) { if (tractor2.Count >= tractor0.Count) { int tmpMax = tractor2[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 2; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer3].Count > 0) { if (tractor3.Count >= tractor0.Count) { int tmpMax = tractor3[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 3; } } } } else if (cp[0].GetPairs().Count == 1 && (leadingCardsCount == 2)) //如果有一个对 { var maxCard = (int)cp[0].GetPairs()[0]; if (trickState.ShowedCards[nextPlayer1].Count > 0) { if (cp[1].GetPairs().Count > 0) { var tmpMax = (int)cp[1].GetPairs()[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 1; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer2].Count > 0) { if (cp[2].GetPairs().Count > 0) { var tmpMax = (int)cp[2].GetPairs()[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 2; maxCard = tmpMax; } } } if (trickState.ShowedCards[nextPlayer3].Count > 0) { if (cp[3].GetPairs().Count > 0) { var tmpMax = (int)cp[3].GetPairs()[0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 3; } } } } else if (leadingCardsCount == 1) //如果是单张牌 { int maxCard = trickState.ShowedCards[trickState.Learder][0]; int tmpMax = 0; if (trickState.ShowedCards[nextPlayer1].Count > 0) { tmpMax = trickState.ShowedCards[nextPlayer1][0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 1; maxCard = tmpMax; } } if (trickState.ShowedCards[nextPlayer2].Count > 0) { tmpMax = trickState.ShowedCards[nextPlayer2][0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 2; maxCard = tmpMax; } } if (trickState.ShowedCards[nextPlayer3].Count > 0) { tmpMax = trickState.ShowedCards[nextPlayer3][0]; if (!CommonMethods.CompareTo(maxCard, tmpMax, trumpInt, rank, leadingSuit)) { winderNumber = 3; } } } string winner = ""; switch (winderNumber) { case 0: winner = trickState.Learder; break; case 1: winner = nextPlayer1; break; case 2: winner = nextPlayer2; break; case 3: winner = nextPlayer3; break; } return(winner); }
/// <summary> /// check if the player's selected cards valid /// 检查出牌或者甩牌合理 /// </summary> /// <param name="playerHoldingCards">所有玩家当前手里的牌</param> /// <param name="selectedCards"></param> /// <param name="player">出牌的玩家</param> /// <returns></returns> public static ShowingCardsValidationResult IsLeadingCardsValid( Dictionary <string, CurrentPoker> playerHoldingCards, List <int> selectedCards, string player) { var mustShowCardsForDumpingFail = new List <int>(); CurrentPoker holdingCardsCp = playerHoldingCards.Values.FirstOrDefault(); if (holdingCardsCp == null) { return new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.Unknown } } ; var selectedCardsCp = new CurrentPoker(selectedCards, holdingCardsCp.TrumpInt, holdingCardsCp.Rank); int selectedCardSuit = CommonMethods.GetSuit(selectedCards[0], selectedCardsCp.TrumpInt, selectedCardsCp.Rank); int trump = selectedCardsCp.TrumpInt; int rank = selectedCardsCp.Rank; List <int> tractor = selectedCardsCp.GetTractorOfAnySuit(); if (selectedCards.Count == 1) //如果是单张牌 { return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.Valid }); } if (selectedCards.Count == 2 && (selectedCardsCp.GetPairs().Count == 1)) //如果是一对 { return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.Valid }); } if (tractor.Count > 1 && selectedCards.Count == tractor.Count * 2) //如果是拖拉机 { return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.Valid }); } if (selectedCardsCp.IsMixed()) { return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.Invalid }); } while (tractor.Count > 1) { int myMax = tractor[0]; foreach (int card in tractor) { selectedCardsCp.RemoveCard(card); selectedCardsCp.RemoveCard(card); } foreach (var keyValue in playerHoldingCards) { if (keyValue.Key != player) { List <int> tractor1 = keyValue.Value.GetTractor((Suit)selectedCardSuit); if (tractor1.Count < tractor.Count) { continue; } int max = tractor1[0]; if (!CommonMethods.CompareTo(myMax, max, trump, rank, selectedCardSuit)) { foreach (int card in tractor) { mustShowCardsForDumpingFail.Add(card); mustShowCardsForDumpingFail.Add(card); } return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.DumpingFail, MustShowCardsForDumpingFail = mustShowCardsForDumpingFail, CardsToShow = selectedCards }); } } } tractor = selectedCardsCp.GetTractorOfAnySuit(); } if (selectedCardsCp.GetPairs().Count > 0) { ArrayList list0 = selectedCardsCp.GetPairs(); for (int i = 0; i < list0.Count; i++) { var myMax = (int)list0[i]; selectedCardsCp.RemoveCard(myMax); selectedCardsCp.RemoveCard(myMax); foreach (var keyValue in playerHoldingCards) { if (keyValue.Key != player) { ArrayList list = keyValue.Value.GetPairs(selectedCardSuit); if (list.Count == 0) { continue; } var max = (int)list[list.Count - 1]; if (!CommonMethods.CompareTo(myMax, max, trump, rank, selectedCardSuit) && max > -1) { mustShowCardsForDumpingFail.Add(myMax); mustShowCardsForDumpingFail.Add(myMax); return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.DumpingFail, MustShowCardsForDumpingFail = mustShowCardsForDumpingFail, CardsToShow = selectedCards }); } } } } } int[] cards = selectedCardsCp.GetCards(); foreach (var keyValue in playerHoldingCards) { if (keyValue.Key != player) { int max = keyValue.Value.GetMaxCard(selectedCardSuit); for (int i = 0; i < 54; i++) { if (cards[i] == 1) { if (!CommonMethods.CompareTo(i, max, trump, rank, selectedCardSuit)) { mustShowCardsForDumpingFail.Add(i); return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.DumpingFail, MustShowCardsForDumpingFail = mustShowCardsForDumpingFail, CardsToShow = selectedCards }); } } } } } return(new ShowingCardsValidationResult { ResultType = ShowingCardsValidationResultType.DumpingSuccess }); }