//look for royal flush, removing pair using recursion public static bool isRoyalFlush(Hand hand) { hand.sortByRank(); Hand simplifiedhand1, simplifiedhand2; //to be set the same as hand - cards are removed from this hand to evaluate straights separately without the interference of pairs or three-of-a-kind for (int i = 0; i <= hand.Count() - 2; i++) { if (hand[i] == hand[i + 1]) { simplifiedhand1 = new Hand(hand); simplifiedhand1.Remove(i); simplifiedhand2 = new Hand(hand); simplifiedhand2.Remove(i + 1); if (HandCombination.isRoyalFlush(simplifiedhand1)) { return(true); } if (HandCombination.isRoyalFlush(simplifiedhand2)) { return(true); } } } int currentsuit = hand.getCard(0).getSuit(); if (hand.getCard(0).getRank() == 14 && hand.getCard(1).getRank() == 13 && hand.getCard(2).getRank() == 12 && hand.getCard(3).getRank() == 11 && hand.getCard(4).getRank() == 10 && hand.getCard(1).getSuit() == currentsuit && hand.getCard(2).getSuit() == currentsuit && hand.getCard(3).getSuit() == currentsuit && hand.getCard(4).getSuit() == currentsuit) { return(true); } else { return(false); } }
PlayerList QuickSortBestHand(PlayerList myPlayers) { Player pivot; Random ran = new Random(); if (myPlayers.Count() <= 1) { return(myPlayers); } pivot = myPlayers[ran.Next(myPlayers.Count())]; myPlayers.Remove(pivot); var less = new PlayerList(); var greater = new PlayerList(); // Assign values to less or greater list foreach (Player player in myPlayers) { if (HandCombination.getBestHand(new Hand(player.getHand())) > HandCombination.getBestHand(new Hand(pivot.getHand()))) { greater.Add(player); } else if (HandCombination.getBestHand(new Hand(player.getHand())) <= HandCombination.getBestHand(new Hand(pivot.getHand()))) { less.Add(player); } } // Recurse for less and greaterlists var list = new PlayerList(); list.AddRange(QuickSortBestHand(greater)); list.Add(pivot); list.AddRange(QuickSortBestHand(less)); return(list); }
//get royal flush using recursion public static Hand getRoyalFlush(Hand hand) { hand.sortByRank(); Hand straightflush = new Hand(HandCombination.getStraightFlush(hand)); straightflush.setValue(10); if (straightflush.getCard(0).getRank() == 14) { return(straightflush); } else { straightflush.Clear(); return(straightflush); } }
//determine the hole cards of the player and the community cards to be dealt //determines who will win the showdown at the end public void CalculateHandValueHard(Hand otherHand,Deck deck) { Hand knownCommunityCards = new Hand(); Hand remainingCommunityCards = new Hand(); //add known community cards for (int i = 2; i < myHand.Count(); i++) { knownCommunityCards.Add(myHand[i]); } //generate remaining community cards if (knownCommunityCards.Count() < 5) { remainingCommunityCards.Add(deck.Deal()); if (knownCommunityCards.Count() < 4) { remainingCommunityCards.Add(deck.Deal()); if (knownCommunityCards.Count() < 3) { remainingCommunityCards.Add(deck.Deal()); remainingCommunityCards.Add(deck.Deal()); remainingCommunityCards.Add(deck.Deal()); } } } //add the remaining cards this.AddToHand(remainingCommunityCards); otherHand += remainingCommunityCards; otherHand += knownCommunityCards; //compare hands if (HandCombination.getBestHandEfficiently(new Hand(myHand)) > HandCombination.getBestHandEfficiently(new Hand(otherHand))) handValue = 1; else if (HandCombination.getBestHandEfficiently(new Hand(myHand)) < HandCombination.getBestHandEfficiently(new Hand(otherHand))) handValue = 0; else handValue = 0.5; for (int i = 0; i < remainingCommunityCards.Count(); i++) { myHand.Remove(remainingCommunityCards[i]); } }
//use recursion to get rid of pairs, then evaluate straight flush public static bool isStraightFlush(Hand hand) { hand.sortByRank(); Hand simplifiedhand1, simplifiedhand2; //to be set the same as hand - cards are removed from this hand to evaluate straights separately without the interference of pairs or three-of-a-kind for (int i = 0; i <= hand.Count() - 2; i++) { if (hand.getCard(i) == hand.getCard(i + 1)) { simplifiedhand1 = new Hand(hand); simplifiedhand1.Remove(i); simplifiedhand2 = new Hand(hand); simplifiedhand2.Remove(i + 1); if (HandCombination.isStraightFlush(simplifiedhand1)) { return(true); } if (HandCombination.isStraightFlush(simplifiedhand2)) { return(true); } } } for (int i = 0; i <= hand.Count() - 5; i++) { int currentrank = hand.getCard(i).getRank(), currentsuit = hand.getCard(i).getSuit(); if (currentrank == hand.getCard(i + 1).getRank() + 1 && currentrank == hand.getCard(i + 2).getRank() + 2 && currentrank == hand.getCard(i + 3).getRank() + 3 && currentrank == hand.getCard(i + 4).getRank() + 4 && currentsuit == hand.getCard(i + 1).getSuit() && currentsuit == hand.getCard(i + 2).getSuit() && currentsuit == hand.getCard(i + 3).getSuit() && currentsuit == hand.getCard(i + 4).getSuit()) { return(true); } } for (int i = 0; i <= hand.Count() - 4; i++) { int currentrank = hand.getCard(i).getRank(), currentsuit = hand.getCard(i).getSuit(); if (currentrank == 5 && hand.getCard(i + 1).getRank() == 4 && hand.getCard(i + 2).getRank() == 3 && hand.getCard(i + 3).getRank() == 2 && hand.getCard(0).getRank() == 14 && currentsuit == hand.getCard(i + 1).getSuit() && currentsuit == hand.getCard(i + 2).getSuit() && currentsuit == hand.getCard(i + 3).getSuit() && currentsuit == hand.getCard(0).getSuit()) { return(true); } } return(false); }
//showdown code! public void ShowDown() { //creating sidepots if (CreateSidePots()) { mainPot.getPlayersInPot().Sort(); for (int i = 0; i < mainPot.getPlayersInPot().Count - 1; i++) { if (mainPot.getPlayersInPot()[i].AmountInPot != mainPot.getPlayersInPot()[i + 1].AmountInPot) { PlayerList tempPlayers = new PlayerList(); for (int j = mainPot.getPlayersInPot().Count - 1; j > i; j--) { tempPlayers.Add(mainPot.getPlayersInPot()[j]); } int potSize = (mainPot.getPlayersInPot()[i + 1].AmountInPot - mainPot.getPlayersInPot()[i].AmountInPot) * tempPlayers.Count; mainPot.Amount -= potSize; sidePots.Add(new Pot(potSize, tempPlayers)); } } } //awarding mainpot PlayerList bestHandList = new PlayerList(); List <int> Winners = new List <int>(); bestHandList = QuickSortBestHand(new PlayerList(mainPot.getPlayersInPot())); for (int i = 0; i < bestHandList.Count; i++) { for (int j = 0; j < this.getPlayers().Count; j++) { if (players[j] == bestHandList[i]) { Winners.Add(j); } } if (HandCombination.getBestHand(new Hand(bestHandList[i].getHand())) != HandCombination.getBestHand(new Hand(bestHandList[i + 1].getHand()))) { break; } } mainPot.Amount /= Winners.Count; if (Winners.Count > 1) { for (int i = 0; i < this.getPlayers().Count; i++) { if (Winners.Contains(i)) { currentIndex = i; players[i].CollectMoney(mainPot); winnermessage += players[i].Name + ", "; } } winnermessage += Environment.NewLine + " split the pot."; } else { currentIndex = Winners[0]; players[currentIndex].CollectMoney(mainPot); winnermessage = players[currentIndex].Message; } //awarding sidepots for (int i = 0; i < sidePots.Count; i++) { List <int> sidePotWinners = new List <int>(); for (int x = 0; x < bestHandList.Count; x++) { for (int j = 0; j < this.getPlayers().Count; j++) { if (players[j] == bestHandList[x] && sidePots[i].getPlayersInPot().Contains(bestHandList[x])) { sidePotWinners.Add(j); } } if (HandCombination.getBestHand(new Hand(bestHandList[x].getHand())) != HandCombination.getBestHand(new Hand(bestHandList[x + 1].getHand())) && sidePotWinners.Count != 0) { break; } } sidePots[i].Amount /= sidePotWinners.Count; for (int j = 0; j < this.getPlayers().Count; j++) { if (sidePotWinners.Contains(j)) { currentIndex = j; players[j].CollectMoney(sidePots[i]); } } } }
//using the monte carlo method, calculate a hand value for the AI's current hand //this can be very hard on the computer's memory and processor and can result in lag time public void CalculateHandValue(int count) { double score = 0; PlayerList playerList = new PlayerList(); for (int i = 0; i < count - 1; i++) { playerList.Add(new Player()); } Hand bestHand = new Hand(); int bestHandCount = 1; Deck deck = new Deck(); Hand knownCommunityCards = new Hand(); Hand remainingCommunityCards = new Hand(); Hand myHoleCards = new Hand(); //remove known cards from deck for (int i = 0; i < myHand.Count(); i++) { deck.Remove(myHand[i]); } //add known community cards for (int i = 2; i < myHand.Count(); i++) { knownCommunityCards.Add(myHand[i]); } myHoleCards.Add(this.getHand()[0]); myHoleCards.Add(this.getHand()[1]); //loop 100 times for (int i = 0; i < 100; i++) { //reset players and shuffle deck for (int j = 0; j < playerList.Count; j++) { playerList[j].isbusted = false; playerList[j].getHand().Clear(); } myHand.Clear(); remainingCommunityCards.Clear(); deck.Shuffle(); //generate remaining community cards if (knownCommunityCards.Count() < 5) { remainingCommunityCards.Add(deck.Deal()); if (knownCommunityCards.Count() < 4) { remainingCommunityCards.Add(deck.Deal()); if (knownCommunityCards.Count() < 3) { remainingCommunityCards.Add(deck.Deal()); remainingCommunityCards.Add(deck.Deal()); remainingCommunityCards.Add(deck.Deal()); } } } //add hole/community cards to the AI this.AddToHand(knownCommunityCards); this.AddToHand(remainingCommunityCards); this.AddToHand(myHoleCards); //add hole/community cards to other players for (int j = 0; j < playerList.Count; j++) { playerList[j].AddToHand(knownCommunityCards); if (remainingCommunityCards.Count() != 0) playerList[j].AddToHand(remainingCommunityCards); playerList[j].AddToHand(deck.Deal()); playerList[j].AddToHand(deck.Deal()); //if player is dealt hole cards of less than 5-5, and no pocket pairs the player drops out if (playerList[j].getHand()[playerList[j].getHand().Count() - 1].getRank() + playerList[j].getHand()[playerList[j].getHand().Count() - 2].getRank() <= 10 && playerList[j].getHand()[playerList[j].getHand().Count() - 1].getRank() != playerList[j].getHand()[playerList[j].getHand().Count() - 2].getRank()) { playerList[j].isbusted = true; } } //add cards back to deck for (int j = 0; j < remainingCommunityCards.Count(); j++) { deck.Add(remainingCommunityCards[j]); } for (int j = 0; j < playerList.Count; j++) { deck.Add(playerList[j].getHand()[playerList[j].getHand().Count() - 1]); deck.Add(playerList[j].getHand()[playerList[j].getHand().Count() - 2]); } //compare hands bestHandCount = 1; playerList.Add(this); bestHand = playerList[0].getHand(); for (int j = 0; j <playerList.Count-1; j++) { if (playerList[j].isbusted) continue; if (HandCombination.getBestHandEfficiently(new Hand(playerList[j+1].getHand())) > HandCombination.getBestHandEfficiently(new Hand(playerList[j].getHand()))) { bestHandCount = 1; bestHand = playerList[j+1].getHand(); } else if (HandCombination.getBestHandEfficiently(new Hand(playerList[j+1].getHand())) == HandCombination.getBestHandEfficiently(new Hand(playerList[j].getHand()))) bestHandCount++; } playerList.Remove(this); //if my hand is the best, increment score if (myHand.isEqual(bestHand)) score = score + (1 / bestHandCount); } //reconstruct original hand myHand.Clear(); this.AddToHand(myHoleCards); this.AddToHand(knownCommunityCards); //calculate hand value as a percentage of wins handValue = score / 100; }