Beispiel #1
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 #2
0
 public void SetUp()
 {
     server = new HandServer();
     Random r = new Random();
     cache = new CachedHand(6, r);
 }
Beispiel #3
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);
        }
Beispiel #4
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 #5
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;
        }