Example #1
0
        public bool Fold(string name)
        {
            var player = GetPlayer(name);

            VerifyIsPlayersTurn(player);
            BetQueue.RemoveFirst();
            player.Folded  = true;
            player.Hand[0] = null;
            player.Hand[1] = null;
            Pots.ForEach(x => x.RemovePlayer(player));

            if (GetPlayersStillInGame().Count() == 1)
            {
                BetQueue.Clear();
                State = State.DeterminingWinner;
                NextStage();
                return(false);
            }
            else
            {
                if (BetQueue.Count == 0)
                {
                    NextStage();
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Example #2
0
        public void NextStage()
        {
            //todo: verify enough players to continue play.

            if (State == State.Start)
            {
                SetPot(currentPotIndex);
                RequeuePlayers();
                Table[0] = NextCardInDeck();
                Table[1] = NextCardInDeck();
                Table[2] = NextCardInDeck();
                State    = State.Flop;
                if (BetQueue.Count() <= 1)
                {
                    BetQueue.Clear();
                }
            }
            else if (State == State.Flop)
            {
                SetPot(currentPotIndex);
                RequeuePlayers();
                Table[3] = NextCardInDeck();
                State    = State.Turn;
                if (BetQueue.Count() == 1)
                {
                    BetQueue.Clear();
                }
            }
            else if (State == State.Turn)
            {
                SetPot(currentPotIndex);
                RequeuePlayers();
                Table[4] = NextCardInDeck();
                State    = State.River;
                if (BetQueue.Count() == 1)
                {
                    BetQueue.Clear();
                }
            }
            else if (State == State.River)
            {
                SetPot(currentPotIndex);
                PlayerHands     = new Dictionary <string, Hand>();
                ShowPlayerCards = new List <Player>();
                foreach (var player in GetPlayersStillInGame())
                {
                    PlayerHands.Add(player.Name, new Hand(GetPlayerCards(player.Name).ToArray(), Table));
                }
                BetQueue.Clear();

                State = State.DeterminingWinner;
                var playerThatNeedToHideShow = new List <Player>();

                foreach (var pot in Pots)
                {
                    var  testPlayerShowCards = Dealer;
                    var  firstPlayerIn       = false;
                    Hand bestHand            = null;
                    do
                    {
                        testPlayerShowCards = PlayerAfter(testPlayerShowCards.Position);
                        if (!testPlayerShowCards.Folded && testPlayerShowCards.Chips != 0 && testPlayerShowCards.Hand[0] != null)
                        {
                            if (!playerThatNeedToHideShow.Contains(testPlayerShowCards))
                            {
                                if (!firstPlayerIn)
                                {
                                    firstPlayerIn = true;
                                    bestHand      = PlayerHands[testPlayerShowCards.Name];
                                    ShowPlayerCards.Add(testPlayerShowCards);
                                }
                                else
                                {
                                    if (PlayerHands[testPlayerShowCards.Name] < bestHand)
                                    {
                                        playerThatNeedToHideShow.Add(testPlayerShowCards);
                                    }
                                    else
                                    {
                                        ShowPlayerCards.Add(testPlayerShowCards);
                                        if (PlayerHands[testPlayerShowCards.Name] > bestHand)
                                        {
                                            bestHand = PlayerHands[testPlayerShowCards.Name];
                                        }
                                    }
                                }
                            }
                        }
                    } while (testPlayerShowCards != Dealer);
                }
                var requeuePlayer = Dealer;
                do
                {
                    requeuePlayer = PlayerAfter(requeuePlayer.Position);
                    if (playerThatNeedToHideShow.Contains(requeuePlayer))
                    {
                        BetQueue.AddLast(requeuePlayer);
                    }
                } while (requeuePlayer != Dealer);
            }
            else if (State == State.DeterminingWinner)
            {
                SetPot(currentPotIndex);
                State = State.DistributingPot;

                foreach (var pot in Pots)
                {
                    double share       = 0;
                    Player winner      = null;
                    Hand   winningHand = null;
                    var    winners     = new List <Player>();

                    if (pot.EligiblePlayers.Count() == 1)
                    {
                        winners.Add(pot.EligiblePlayers[0]);
                    }
                    else
                    {
                        foreach (var p in pot.EligiblePlayers)
                        {
                            var hand = PlayerHands[p.Name];
                            if (winner == null ||
                                hand > winningHand)
                            {
                                winners.Clear();
                                winningHand = hand;
                                winner      = p;
                                winners.Add(winner);
                            }
                            else if (hand == winningHand)
                            {
                                winners.Add(p);
                            }
                        }
                    }
                    share = pot.PotAmount / winners.Count;
                    foreach (var w in winners)
                    {
                        w.CurrentBet += share;
                    }
                }
                var allWinners = Players.Where(x => x.CurrentBet > 0);
                if (GetPlayersStillInGame().Count() == 1)
                {
                    allWinners.ElementAt(0).Hand[0] = null;
                    allWinners.ElementAt(0).Hand[1] = null;
                    var newHandHistory = new HandHistory();
                    newHandHistory.PlayerName = allWinners.ElementAt(0).Name;
                    newHandHistory.MoneyWon   = allWinners.ElementAt(0).CurrentBet;
                    if (HandHistory.Count == 50)
                    {
                        HandHistory.RemoveAt(0);
                    }
                    HandHistory.Add(newHandHistory);
                }
                else
                {
                    foreach (var handWinner in allWinners)
                    {
                        var newHandHistory = new HandHistory();
                        newHandHistory.PlayerName = handWinner.Name;
                        newHandHistory.MoneyWon   = handWinner.CurrentBet;
                        newHandHistory.Hand       = PlayerHands[newHandHistory.PlayerName];
                        HandHistory.Add(newHandHistory);
                    }
                }
                Pots.Clear();
            }
            else if (State == State.DistributingPot)
            {
                foreach (var p in Players)
                {
                    p.Chips  += p.CurrentBet;
                    p.Hand[0] = null;
                    p.Hand[1] = null;
                }
                for (int i = 0; i < Table.Length; i++)
                {
                    Table[i] = null;
                }
                State = State.GameOver;
            }
            else
            {
                Start();
            }

            if (State != State.DistributingPot && State != State.Start)
            {
                foreach (var p in Players)
                {
                    p.CurrentBet = 0;
                }
                MinBet = 0; // Allows checks.
            }
        }