예제 #1
0
        public static void RequestAction(Table t, Player p, string gameStage)
        {
            // this will wait with the client player and seek an action
            Message m = new Message("RequestBet", MessageType.PlayerActionRequestBet);

            m.Content = t.TableNo + ":" + t.GetPotSize().ToString() + ":" + p.getPostedBetSoFar(gameStage) + ":" + t.GetCurrentMinBet().ToString() + ":" + t.GetCurrentMaxRaisingBet().ToString() + ":" + gameStage; // Add more elements like min and max bet size etc later
            lock (t)                                                                                                                                                                                                 // this will synchronize this call with any previous pending call to SendToTablePlayers
            {
                SendMessageToPlayer(p, m);
            }
        }
예제 #2
0
        private void Start()
        {
            if (!((_table.SeatedPlayerCount() >= 3) && (!_GameInPogress)))
            {
                return;
            }

            if ((_game == null) || (_table == null))
            {
                throw new Exception("Table or Game cannot be null");
            }
            _GameInPogress = true;
            _GameStopFlag  = false;

            _table.SetDealerPosition();
            //deal hole cards to seated players
            int playercount = _table.SeatedPlayerCount();
            int gamecount   = 100;
            int timespan    = 33000;                    // 33 secs

            while ((gamecount > 0) && (!_GameStopFlag)) // game loop
            {
                gamecount--;
                _game.initialize();
                _table.ResetForGameStart();

                // initialize the player , resetting the folded hand state
                // Also consolidate who all players are in the game. Anybody who is not dealt a hole card should not be asked for betting

                _game.SetGameState("Starting");
                MessageFactory.SendGameUpdateMessage(_table); // this is sent to reset the board and holecards of the clients
                MessageFactory.SendTableUpdateMessage(_table);
                Thread.Sleep(5000);                           // deliberately delay to give client time to process.
                playercount = _table.SeatedPlayerCount();
                while (playercount > 0)
                {
                    Player player = _table.GetNextPlayer();
                    player.AssignDealerButton(0);
                    player.AssignHoleCards(_game.DealPlayerHand());
                    playercount--;
                }
                // do bet collecting round
                _table.ResetToUTG();
                playercount = _table.PlayingPlayerCount();
                //while (playercount > 0)
                while (playercount > 0)
                {
                    Player player = _table.GetNextPlayer();
                    if (player.InHand)
                    {
                        if (player.getPostedBetSoFar("preflop") == _table.GetCurrentMinBet())
                        {
                            break; // All players have posted their bets to completion
                        }
                        lock (_table.SynchronizeGame)
                        {
                            MessageFactory.RequestAction(_table, player, "preflop");
                            Monitor.Wait(_table.SynchronizeGame, timespan);
                        }
                    }
                    //playercount--;
                    playercount = _table.PlayingPlayerCount();
                }


                playercount = _table.PlayingPlayerCount();


                // deal the flop
                if (playercount > 1)
                {
                    Tuple <Card, Card, Card> flop = _game.GetFlop();
                    MessageFactory.SendFlop(_table, flop);
                }

                // do bet collecting round

                _table.ResetToSmallBlind();
                _table.ResetMinBet();

                while (playercount > 1)
                {
                    Player player = _table.GetNextPlayer();
                    if (player.InHand)
                    {
                        if (player.getPostedBetSoFar("postflop") == _table.GetCurrentMinBet())
                        {
                            break; // All players have posted their bets to completion
                        }
                        lock (_table.SynchronizeGame)
                        {
                            MessageFactory.RequestAction(_table, player, "postflop");
                            Monitor.Wait(_table.SynchronizeGame, timespan);
                        }
                    }
                    //playercount--;
                    playercount = _table.PlayingPlayerCount();
                }

                playercount = _table.PlayingPlayerCount();
                //deal the turn
                if (playercount > 1)
                {
                    Card turn = _game.GetTurn();
                    MessageFactory.SendTurn(_table, turn);
                }
                // do bet collecting round

                _table.ResetToSmallBlind();
                _table.ResetMinBet();

                while (playercount > 1)
                {
                    Player player = _table.GetNextPlayer();
                    if (player.InHand)
                    {
                        if (player.getPostedBetSoFar("postturn") == _table.GetCurrentMinBet())
                        {
                            break; // All players have posted their bets to completion
                        }
                        lock (_table.SynchronizeGame)
                        {
                            MessageFactory.RequestAction(_table, player, "postturn");
                            Monitor.Wait(_table.SynchronizeGame, timespan);
                        }
                    }
                    //playercount--;
                    playercount = _table.PlayingPlayerCount();
                }

                playercount = _table.PlayingPlayerCount();
                // deal the river
                if (playercount > 1)
                {
                    Card river = _game.GetRiver();
                    MessageFactory.SendRiver(_table, river);
                }
                // do the bet collecting round

                _table.ResetToSmallBlind();
                _table.ResetMinBet();

                while (playercount > 1)
                {
                    Player player = _table.GetNextPlayer();
                    if (player.InHand)
                    {
                        if (player.getPostedBetSoFar("postriver") == _table.GetCurrentMinBet())
                        {
                            break; // All players have posted their bets to completion
                        }
                        lock (_table.SynchronizeGame)
                        {
                            MessageFactory.RequestAction(_table, player, "postriver");
                            Monitor.Wait(_table.SynchronizeGame, timespan);
                        }
                    }
                    //playercount--;
                    playercount = _table.PlayingPlayerCount();
                }
                // announce the winner
                playercount = _table.PlayingPlayerCount();
                List <HandRankings> playerhands = new List <HandRankings>();
                while (playercount > 0)
                {
                    Player player = _table.GetNextPlayer();
                    if (player.InHand)
                    {
                        HandRankings besthand = HandRankings.ComputeBestHand(player, _game.GetBoard());
                        playerhands.Add(besthand);
                    }
                    playercount--;
                }
                if (playerhands.Count > 0)
                {
                    List <HandRankings> hlist = playerhands.Select(a => a).ToList <HandRankings>();
                    hlist.Sort(new HandComparer());
                    _game.setWinningHand(hlist[hlist.Count - 1]);
                }
                _game.SetGameState("Ending");
                MessageFactory.SendWinningHand(_table, _game.WinningHand);
                MessageFactory.SendGameUpdateMessage(_table);
                _GameInPogress = false;
                _table.AdvanceDealerPosition();
            }// end of game loop
        }