Exemple #1
0
        public void PlayGame(string sConfigFile, string sOutputBase)
        {
            LoadConfig(sConfigFile, sOutputBase);

            var bDone   = false;
            var handNum = 0;
            var rnd     = new Random();

            _totalMoneyInGame = _players.Sum(p => p.StackSize);

            _dealerPlayerNum = 0;

            if (_bRandomDealer)
            {
                int dealerIncr = rnd.Next(GetNumActivePlayers());

                while (dealerIncr > 0)
                {
                    _dealerPlayerNum = GetNextActivePlayer(_dealerPlayerNum);
                    dealerIncr--;
                }
            }

            _smallBlindPlayerId = GetNextActivePlayer(_dealerPlayerNum);
            _bigBlindPlayerNum  = GetNextActivePlayer(_smallBlindPlayerId);

            BroadcastInitialisation();

            while (!bDone)
            {
                var communityCards = new Card[5];
                int lastToAct;

                // init round for each player
                handNum++;
                InitialiseHand(handNum);

                BroadcastBeginHand();

                // deal out hole cards to all active players
                DealHoleCards();

                BroadcastBeginStage(Stage.StagePreflop, communityCards);
                // First betting round - get player actions and broadcast to all players until betting round done
                DoBettingRound(Stage.StagePreflop, out lastToAct);
                BroadcastEndStage();

                if (GetNumActivePlayers() > 1)
                {
                    // deal flop
                    communityCards[0] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardFlop1, communityCards[0]);

                    communityCards[1] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardFlop2, communityCards[1]);

                    communityCards[2] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardFlop3, communityCards[2]);

                    BroadcastBeginStage(Stage.StageFlop, communityCards);
                    // Second betting round - get player actions and broadcast to all players until betting round done
                    if (IsBettingRoundRequired())
                    {
                        DoBettingRound(Stage.StageFlop, out lastToAct);
                    }
                    BroadcastEndStage();
                }

                if (GetNumActivePlayers() > 1)
                {
                    // deal turn
                    communityCards[3] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardTurn, communityCards[3]);

                    BroadcastBeginStage(Stage.StageTurn, communityCards);
                    // Third betting round - get player actions and broadcast to all players until betting round done
                    if (IsBettingRoundRequired())
                    {
                        DoBettingRound(Stage.StageTurn, out lastToAct);
                    }
                    BroadcastEndStage();
                }

                if (GetNumActivePlayers() > 1)
                {
                    // deal river
                    communityCards[4] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardRiver, communityCards[4]);

                    BroadcastBeginStage(Stage.StageRiver, communityCards);
                    // Fourth betting round - get player actions and broadcast to all players until betting round done
                    if (IsBettingRoundRequired())
                    {
                        DoBettingRound(Stage.StageRiver, out lastToAct);
                    }
                    BroadcastEndStage();
                }

                // ViewCash();

                var handRanker = new HandRanker();

                if (GetNumActivePlayers() > 1)
                {
                    Showdown(communityCards, ref handRanker, lastToAct);
                }

                if (GetNumActivePlayers() > 1)
                {
                    // More than one player has shown cards at showdown. Work out how to allocate the pot(s)
                    DistributeWinnings(handRanker);
                }
                else
                {
                    // all players except 1 have folded. Just give entire pot to last man standing
                    var winningPlayer = _players.First(p => p.IsActive).PlayerNum;

                    _players[winningPlayer].StackSize += _potMan.Size();
                    BroadcastAction(Stage.StageShowdown, winningPlayer, ActionType.Win, _potMan.Size());
                    _potMan.EmptyPot();
                }

                // check that money hasn't disappeared or magically appeared
                ReconcileCash();

                // Kill off broke players & check if only one player left
                KillBrokePlayers();

                BroadcastEndHand();

                if (GetNumLivePlayers() == 1)
                {
                    bDone = true;
                }
                else if (_maxHands > 0 && handNum >= _maxHands)
                {
                    bDone = true;
                }
                else
                {
                    // Move to next dealer
                    MoveDealerAndBlinds();
                }

                if (_bPauseAfterEachHand)
                {
                    Console.WriteLine("--- Press any key to continue (ESC to exit) ---");
                    bDone = Console.ReadKey().Key == ConsoleKey.Escape;
                }
            }

            EndOfGame();

            // Write entry to summary log - !!! do this in EndofGame / new Display class?
            TextWriter logTextWriter = new StreamWriter("logs\\Summary.txt", true);

            var playerInfos = GetListOfPlayerInfos();

            foreach (var p in playerInfos)
            {
                if (p.IsAlive)
                {
                    logTextWriter.WriteLine(sConfigFile + "\t" + sOutputBase + "\t" + p.Name + "\t" + handNum);
                }
            }

            logTextWriter.Close();
        }
        public void PlayGame()
        {
            LoadConfig();

            var bDone   = false;
            var handNum = 0;

            _totalMoneyInGame     = _players.Sum(p => p.StackSize);
            _dealerPlayerNum      = 0;
            _littleBlindPlayerNum = GetNextActivePlayer(_dealerPlayerNum);
            _bigBlindPlayerNum    = GetNextActivePlayer(_littleBlindPlayerNum);


            _display = new DisplayManager(150, 35, _players);
            _display.DrawTable();

            while (!bDone)
            {
                var board = new Card[5];
                _display.UpdateCommunityCards(board);
                int lastToAct;

                // init round for each player
                handNum++;
                InitHand(handNum);

                // deal out hole cards to all active players
                DealHoleCards();

                // First betting round - get player actions and broadcast to all players until betting round done
                DoBettingRound(EStage.StagePreflop, out lastToAct);
                _display.UpdatePots(_potMan.Pots);

                if (GetNumActivePlayers() > 1)
                {
                    // deal flop
                    board[0] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardFlop1, board[0]);
                    _display.UpdateCommunityCards(board);

                    board[1] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardFlop2, board[1]);
                    _display.UpdateCommunityCards(board);

                    board[2] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardFlop3, board[2]);
                    _display.UpdateCommunityCards(board);

                    // Second betting round - get player actions and broadcast to all players until betting round done
                    if (IsBettingRoundRequired())
                    {
                        DoBettingRound(EStage.StageFlop, out lastToAct);
                        _display.UpdatePots(_potMan.Pots);
                    }
                }

                if (GetNumActivePlayers() > 1)
                {
                    // deal turn
                    board[3] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardTurn, board[3]);
                    _display.UpdateCommunityCards(board);

                    // Third betting round - get player actions and broadcast to all players until betting round done
                    if (IsBettingRoundRequired())
                    {
                        DoBettingRound(EStage.StageTurn, out lastToAct);
                        _display.UpdatePots(_potMan.Pots);
                    }
                }

                if (GetNumActivePlayers() > 1)
                {
                    // deal river
                    board[4] = _deck.DealCard();
                    BroadcastBoardCard(EBoardCardType.BoardRiver, board[4]);
                    _display.UpdateCommunityCards(board);

                    // Fourth betting round - get player actions and broadcast to all players until betting round done
                    if (IsBettingRoundRequired())
                    {
                        DoBettingRound(EStage.StageRiver, out lastToAct);
                        _display.UpdatePots(_potMan.Pots);
                    }
                }

                ViewCash();

                var handRanker = new HandRanker();

                if (GetNumActivePlayers() > 1)
                {
                    Showdown(board, ref handRanker, lastToAct);
                    handRanker.ViewHandRanks();
                }

                if (GetNumActivePlayers() > 1)
                {
                    // More than one player has shown cards at showdown. Work out how to allocate the pot(s)
                    DistributeWinnings(handRanker);
                }
                else
                {
                    // all players except 1 have folded. Just give entire pot to last man standing
                    var winningPlayer = _players.First(p => p.IsActive).PlayerNum;

                    _players[winningPlayer].StackSize += _potMan.Size();
                    BroadcastAction(EStage.StageShowdown, winningPlayer, EActionType.ActionWin, _potMan.Size());
                    _potMan.EmptyPot();
                }

                // check that money hasn't disappeared or magically appeared
                ReconcileCash();

                // Kill off broke players & check if only one player left
                KillBrokePlayers();

                if (GetNumLivePlayers() == 1)
                {
                    bDone = true;
                }
                else if (_maxHands > 0 && handNum >= _maxHands)
                {
                    bDone = true;
                }
                else
                {
                    // Move to next dealer
                    MoveDealerAndBlinds();
                }
                _display.UpdatePots(_potMan.Pots);

                /*
                 *              ConsoleKeyInfo cki;
                 *              cki= System.Console.ReadKey();
                 *              bDone = (cki.Key == ConsoleKey.Escape);
                 */
            }

            EndOfGame();
        }