Beispiel #1
0
        public HandEngine(Seat[] players, ulong handNumber, uint button, double[] blinds)
        {
            //todo: change
            _seats = players;

            _history = new HandHistory(_seats, handNumber, button, blinds, 0, BettingStructure.NoLimit);

            _history.HoleCards  = new ulong[_seats.Length];
            _history.DealtCards = 0UL;
            _history.Flop       = 0UL;
            _history.Turn       = 0UL;
            _history.River      = 0UL;

            //Create a new map from player names to player chips for the BetManager
            Dictionary <string, double> namesToChips = new Dictionary <string, double>();

            //Create a new list of players for the PlayerManager
            _playerIndices      = new CircularList <int>();
            _playerIndices.Loop = true;

            for (int i = 0; i < _seats.Length; i++)
            {
                namesToChips[_seats[i].Name] = _seats[i].Chips;
                if (_seats[i].SeatNumber == _history.Button)
                {
                    _buttonIdx = i;
                    _utgIdx    = (i + 1) % _seats.Length;
                }
            }

            for (int i = (_buttonIdx + 1) % _seats.Length; _playerIndices.Count < _seats.Length;)
            {
                _playerIndices.Add(i);
                i = (i + 1) % _seats.Length;
            }

            _betManager = new BetManager(namesToChips, _history.BettingStructure, _history.AllBlinds, _history.Ante);
            _potManager = new PotManager(_seats);
        }
Beispiel #2
0
        /// <summary>
        /// Plays a hand from the start. Note that this method will <b>not</b> resume a game from a saved hand _history.
        /// </summary>
        /// <param name="handHistory">An new hand _history with the list of players and the game parameters.</param>
        public void PlayHand(HandHistory handHistory)
        {
            #region Hand Setup
            _seats = handHistory.Players;
            handHistory.HoleCards  = new ulong[_seats.Length];
            handHistory.DealtCards = 0UL;
            handHistory.Flop       = 0UL;
            handHistory.Turn       = 0UL;
            handHistory.River      = 0UL;

            //Setup the hand _history
            this._history = handHistory;

            //Create a new map from player names to player chips for the BetManager
            Dictionary <string, double> namesToChips = new Dictionary <string, double>();

            //Create a new list of players for the PlayerManager
            _playerIndices      = new CircularList <int>();
            _playerIndices.Loop = true;

            for (int i = 0; i < _seats.Length; i++)
            {
                namesToChips[_seats[i].Name] = _seats[i].Chips;
                if (_seats[i].SeatNumber == _history.Button)
                {
                    _buttonIdx = i;
                    _utgIdx    = (i + 1) % _seats.Length;
                }
            }
            for (int i = (_buttonIdx + 1) % _seats.Length; _playerIndices.Count < _seats.Length;)
            {
                _playerIndices.Add(i);
                i = (i + 1) % _seats.Length;
            }


            _betManager = new BetManager(namesToChips, _history.BettingStructure, _history.AllBlinds, _history.Ante);
            _potManager = new PotManager(_seats);
            #endregion



            if (_betManager.In > 1)
            {
                GetBlinds();
                DealHoleCards();
            }

            _history.CurrentRound = Round.Preflop;

            if (_betManager.CanStillBet > 1)
            {
                GetBets(_history.PreflopActions);
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                return;
            }

            DealFlop();
            _history.CurrentRound = Round.Flop;

            if (_betManager.CanStillBet > 1)
            {
                GetBets(_history.FlopActions);
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                return;
            }

            DealTurn();
            _history.CurrentRound = Round.Turn;

            if (_betManager.CanStillBet > 1)
            {
                GetBets(_history.TurnActions);
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                return;
            }

            DealRiver();
            _history.CurrentRound = Round.River;

            if (_betManager.CanStillBet > 1)
            {
                GetBets(_history.RiverActions);
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                return;
            }

            payWinners();
            _history.ShowDown     = true;
            _history.CurrentRound = Round.Over;
        }
Beispiel #3
0
 /// <summary>
 /// Plays a hand from the start. Note that this method will <b>not</b> resume a game from a saved hand _history.
 /// </summary>
 /// <param name="handHistory">An new hand _history with the list of players and the game parameters.</param>
 /// <param name="cachedHand">The cached deck to use.</param>
 public void PlayHand(HandHistory handHistory, CachedHand cachedHand)
 {
     _cache = cachedHand;
     PlayHand(handHistory);
 }
Beispiel #4
0
        public HandHistory Resume(PokerHand savedHand, CachedHand cache)
        {
            _cache = cache;

            #region Restore hand context
            _seats = new Seat[savedHand.Players.Length];
            var orderedPlayers = savedHand.Players.OrderBy(p => p.Seat);
            for (int i = 0; i < _seats.Length; i++)
            {
                var player = orderedPlayers.ElementAt(i);
                _seats[i] = new Seat(player.Seat, player.Name, (double)player.Stack);
            }

            ulong handNum = ulong.Parse(savedHand.Context.ID);
            uint  button  = (uint)savedHand.Context.Button;

            List <double> blinds = new List <double>();
            if (savedHand.Context.SmallBlind > 0m)
            {
                blinds.Add((double)savedHand.Context.SmallBlind);
            }
            if (savedHand.Context.BigBlind > 0m)
            {
                blinds.Add((double)savedHand.Context.BigBlind);
            }
            double           ante = (double)savedHand.Context.Ante;
            BettingStructure bs   = BettingStructure.None;

            switch (savedHand.Context.BettingType)
            {
            case BettingType.FixedLimit: bs = BettingStructure.Limit; break;

            case BettingType.NoLimit: bs = BettingStructure.NoLimit; break;

            case BettingType.PotLimit: bs = BettingStructure.PotLimit; break;

            default: throw new Exception("Unspecified betting structure.");
            }

            _history = new HandHistory(_seats, handNum, button, blinds.ToArray(), ante, bs);
            #endregion

            //Create a new map from player names to player chips for the BetManager
            Dictionary <string, double> namesToChips = new Dictionary <string, double>();

            //Create a new list of players for the PlayerManager
            _playerIndices      = new CircularList <int>();
            _playerIndices.Loop = true;

            // Initialize the names to chips map and find the index of the button
            for (int i = 0; i < _seats.Length; i++)
            {
                namesToChips[_seats[i].Name] = _seats[i].Chips;
                if (_seats[i].SeatNumber == _history.Button)
                {
                    _buttonIdx = i;
                    _utgIdx    = (i + 1) % _seats.Length;
                }
            }

            // Create a circular list of players, in order of first to act
            for (int i = (_buttonIdx + 1) % _seats.Length; _playerIndices.Count < _seats.Length;)
            {
                _playerIndices.Add(i);
                i = (i + 1) % _seats.Length;
            }

            _betManager = new BetManager(namesToChips, _history.BettingStructure, _history.AllBlinds, _history.Ante);
            _potManager = new PotManager(_seats);

            _history.CurrentRound = Round.Predeal;
            if (savedHand.Blinds == null)
            {
                savedHand.Blinds = new Blind[0];
            }
            if (!restoreBlinds(savedHand))
            {
                return(_history);
            }

            DealHoleCards();

            if (_betManager.In <= 1)
            {
                _history.CurrentRound = Round.Over;
                return(_history);
            }

            _history.CurrentRound = Round.Preflop;

            if (_betManager.CanStillBet > 1)
            {
                if (savedHand.Rounds == null || savedHand.Rounds.Length == 0)
                {
                    savedHand.Rounds = new PokerHandHistory.Round[] { new PokerHandHistory.Round()
                                                                      {
                                                                          Actions = new PokerHandHistory.Action[0]
                                                                      } }
                }
                ;
                else if (savedHand.Rounds[0].Actions == null)
                {
                    savedHand.Rounds[0].Actions = new PokerHandHistory.Action[0];
                }
                if (!restoreBets(savedHand.Rounds[0].Actions, _history.PreflopActions))
                {
                    return(_history);
                }
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                _history.CurrentRound = Round.Over;
                return(_history);
            }

            DealFlop();
            _history.CurrentRound = Round.Flop;

            if (_betManager.CanStillBet > 1)
            {
                if (savedHand.Rounds.Length < 2)
                {
                    savedHand.Rounds = new PokerHandHistory.Round[] { savedHand.Rounds[0], new PokerHandHistory.Round()
                                                                      {
                                                                          Actions = new PokerHandHistory.Action[0]
                                                                      } }
                }
                ;
                else if (savedHand.Rounds[1].Actions == null)
                {
                    savedHand.Rounds[1].Actions = new PokerHandHistory.Action[0];
                }

                if (!restoreBets(savedHand.Rounds[1].Actions, _history.FlopActions))
                {
                    return(_history);
                }
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                _history.CurrentRound = Round.Over;
                return(_history);
            }

            DealTurn();
            _history.CurrentRound = Round.Turn;

            if (_betManager.CanStillBet > 1)
            {
                if (savedHand.Rounds.Length < 3)
                {
                    savedHand.Rounds = new PokerHandHistory.Round[] { savedHand.Rounds[0], savedHand.Rounds[1], new PokerHandHistory.Round()
                                                                      {
                                                                          Actions = new PokerHandHistory.Action[0]
                                                                      } }
                }
                ;
                else if (savedHand.Rounds[2].Actions == null)
                {
                    savedHand.Rounds[2].Actions = new PokerHandHistory.Action[0];
                }

                if (!restoreBets(savedHand.Rounds[2].Actions, _history.TurnActions))
                {
                    return(_history);
                }
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                _history.CurrentRound = Round.Over;
                return(_history);
            }

            DealRiver();
            _history.CurrentRound = Round.River;

            if (_betManager.CanStillBet > 1)
            {
                if (savedHand.Rounds.Length < 4)
                {
                    savedHand.Rounds = new PokerHandHistory.Round[] { savedHand.Rounds[0], savedHand.Rounds[1], savedHand.Rounds[2], new PokerHandHistory.Round()
                                                                      {
                                                                          Actions = new PokerHandHistory.Action[0]
                                                                      } }
                }
                ;
                else if (savedHand.Rounds[3].Actions == null)
                {
                    savedHand.Rounds[3].Actions = new PokerHandHistory.Action[0];
                }

                if (!restoreBets(savedHand.Rounds[3].Actions, _history.RiverActions))
                {
                    return(_history);
                }
            }
            if (_betManager.In <= 1)
            {
                payWinners();
                _history.CurrentRound = Round.Over;
                return(_history);
            }

            payWinners();
            _history.ShowDown     = true;
            _history.CurrentRound = Round.Over;
            return(_history);
        }