private static void CheckForFlush(Player player) { var allCards = player.Hand.Concat(deck.BoardCards).ToList();//Combine the board & the players cards. List<int> flushCardsRanks = new List<int>(); for (int a = 0; a < 4; a++) { foreach (Card card in allCards) { if (card.Suit == a) { flushCardsRanks.Add(card.Rank); } } if (flushCardsRanks.Count > 4) { CheckForStraight(player, flushCardsRanks, true); //Sends all cards with same suit for straight check. if (player.HandRank < 5) //No straight/royal flush setted by CheckfotStraight(), setting normal flush. { flushCardsRanks.Sort(); flushCardsRanks.RemoveRange(0, flushCardsRanks.Count - 5); //Leaving top 5 ranks from the flush. flushCardsRanks.Reverse(); List<int> kickers = new List<int>(); kickers.Add(0); //No kickers in Flush. player.SetRank(5, flushCardsRanks, kickers); } break; //Max one flush in a hand. Breaking. } flushCardsRanks.Clear(); } }
private static void CheckForPairs(Player player) { var allCards = player.Hand.Concat(deck.BoardCards).ToList(); List<int> ranks = (from rank in allCards select rank.Rank).ToList(); List<int[]> pairs = new List<int[]>(); //new int array [0] - card rank of the pair, [1] - the lenght of the pair. for (int rank = 2; rank < 15; rank++) { int count = 0; foreach (int otherRank in ranks) if (rank == otherRank) count++; if (count > 1) pairs.Add(new int[2] { rank, count }); //New pair with rank = cardRank & lenght = count. count = 0; } int pairsNumber = pairs.Count; // The number of the existing pairs. if (pairsNumber == 0) { if (player.HandRank < 1) { List<int> cardRanks = (from rank in allCards select rank.Rank).ToList(); cardRanks.Sort(); cardRanks.RemoveRange(0, 2); // Leaving top 5 cards sorted by rank. cardRanks.Reverse(); List<int> cardSum = new List<int>(); cardSum.Add(0); //No cards involved in pairs. player.SetRank(0, cardSum, cardRanks); } } else { if (pairsNumber != 0) { for (int a = 0; a < pairsNumber; a++) { List<int> cardRanks = (from rank in allCards select rank.Rank).ToList(); List<int> cardSum = new List<int>(); //Storing the cards sum ranks. cardRanks.Clear(); cardRanks = (from rank in allCards select rank.Rank).ToList(); int pairRank = pairs[a][0]; int pairLenght = pairs[a][1]; for (int b = 0; b < pairLenght; b++) //Adding the pair card ranks from 2 to 4 times (the pair lenght) cardSum.Add(pairRank); cardRanks.RemoveAll(rank => rank == pairRank); //Removing all cards involved in the pair. int handRank = 0; if (pairLenght == 4) handRank = 7; //Four of a kind. if (pairLenght == 3) handRank = 3; //Three of a kind. if (pairLenght == 2) handRank = 1; //Pair. if (player.HandRank < handRank) { int kickersNumber = 5 - pairLenght; cardRanks.Sort(); cardRanks.RemoveRange(0, cardRanks.Count - kickersNumber);//Leaving the top 3, 2 or 1 kickers (kickersNumber). cardRanks.Reverse(); player.SetRank(handRank, cardSum, cardRanks); } } } if (pairsNumber > 1 && player.HandRank < 7) { List<int> cardRanks = (from rank in allCards select rank.Rank).ToList(); List<int> cardSum = new List<int>(); //Storing the cards sum ranks. List<int> pairsRanks = new List<int>(); List<int> pairsLenght = new List<int>(); for (int a = 0; a < pairsNumber; a++) //Store all pairs and their lenghts. { pairsRanks.Add(pairs[a][0]); pairsLenght.Add(pairs[a][1]); } int highestLenght = pairsLenght.Max(); if (highestLenght == 3) //Full House { int index = pairsLenght.IndexOf(highestLenght); for (int i = 0; i < 3; i++) cardSum.Add(pairsRanks[index]); //Adds the rank ot the pair with lenght = 3. pairsRanks.RemoveAt(index); //Removes this pair. for (int i = 0; i < 2; i++) cardSum.Add(pairsRanks.Max()); //Adds two more ranks from the second highest pair. if (player.HandRank < 6) { List<int> kickers = new List<int>(); kickers.Add(0); // No kickers in Full House. player.SetRank(6, cardSum, kickers); } } if (highestLenght == 2) //Two pairs { for (int a = 0; a < 2; a++) { int highestRank = pairsRanks.Max();//Gets the highest ranks from the pairs. for (int b = 0; b < 2; b++) { cardSum.Add(highestRank); //Adds the ranks. } cardRanks.RemoveAll(rank => rank == highestRank);//Removing the ranks. pairsRanks.Remove(highestRank);//Removing the pair. } if (player.HandRank < 2) { cardRanks.Sort(); cardRanks.RemoveRange(0, 2); //Leaving one kicker. player.SetRank(2, cardSum, cardRanks); } } } } }
private static void CheckForStraight(Player player, List<int> cardRanks, bool couldBeRoyal) { List<int> straightRanks = new List<int>(); if (cardRanks == null) // If null - get board & player hand. { var allCards = player.Hand.Concat(deck.BoardCards).ToList(); cardRanks = (from rank in allCards select rank.Rank).ToList(); } if (cardRanks.IndexOf(14) != -1) //Ace could be the lowest and the highest rank in straight. cardRanks.Add(1); //Adding 1 as the lowest rank if there is an Ace. for (int a = 0; a < cardRanks.Count; a++) { straightRanks.Add(cardRanks[a]); for (int b = 1; b < 7; b++) { if (cardRanks.IndexOf(cardRanks[a] + b) != -1) straightRanks.Add(cardRanks[a] + b); else break; } if (straightRanks.Count > 4) { List<int> kickers = new List<int>(); kickers.Add(0); //No kickers in straight. if (couldBeRoyal) //If method called by CheckForFlush, then set handrank 8 (Royal Flush). { straightRanks.RemoveRange(0, straightRanks.Count - 5); //Leaving top 5 ranks. straightRanks.Reverse(); player.SetRank(8, straightRanks, kickers); } else //Normal straight. { straightRanks.RemoveRange(0, straightRanks.Count - 5);//Leaving top 5 ranks. straightRanks.Reverse(); player.SetRank(4, straightRanks, kickers); } break; //Max one straight. Breaking. } straightRanks.Clear(); } }