Пример #1
0
        public void Shuffle()
        {
            var shuffledDeck = new Card[52];
            var unshuffledList = new List<Card>();
            int i;
            var rnd = new Random();

            _topCard = 0;

            for (i = 0; i < 52; i++)
            {
                unshuffledList.Add(_cards[i]);
            }

            for (i = 0; i < 52; i++)
            {
                var pos = rnd.Next(unshuffledList.Count);
                shuffledDeck[i] = unshuffledList[pos];
                unshuffledList.RemoveAt(pos);
            }

            // Copy shuffled deck back to deck
            for (i = 0; i < 52; i++)
            {
                _cards[i] = shuffledDeck[i];
            }
        }
Пример #2
0
        public Hand(Card[] pCards) 
        {
            if (pCards.Count() != 5)
            {
                return;
            }

            _cards = new Card[5];
            _rankCount = new int[13];
            _suitCount = new int[4];
            _subRank = new eRankType[5];

            for (int i = 0; i < 5; i++)
            {
                _cards[i] = pCards[i];
            }

            _evaluate();

            for (int i = 0; i < 5; i++)
            {
                if (i >= _numSubRanks)
                {
                    _subRank[i] = eRankType.RANK_UNKNOWN;
                }
            }
        }
Пример #3
0
        public void Shuffle()
        {
            Card[] shuffledDeck = new Card[52];
            List<Card> unshuffledList = new List<Card>();
            int i,pos;
            Random rnd = new Random();

            topCard = 0;

            for (i = 0; i < 52; i++)
            {
                unshuffledList.Add(cards[i]);
            }

            for (i = 0; i < 52; i++)
            {
                pos = rnd.Next(unshuffledList.Count);
                shuffledDeck[i] = unshuffledList[pos];
                unshuffledList.RemoveAt(pos);
            }

            // Copy shuffled deck back to deck
            for (i = 0; i < 52; i++)
            {
                cards[i] = shuffledDeck[i];
            }

/*
            Card[] tempDeck = new Card[52];
            long i;
            long pos;
            Random rnd = new Random();

            topCard = 0;

            for (i = 0; i < 52; i++)
            {
                // Select a random empty position to put top card of deck into
                do
                {
                    pos = rnd.Next(52);
                }
                while (tempDeck[pos] != null);

                // Put top card of deck into random position in temp deck
                tempDeck[pos] = cards[i];
            }

            // Copy temp deck back to deck
            for (i = 0; i < 52; i++)
            {
                cards[i] = tempDeck[i];
            }
 */ 
        }
Пример #4
0
        public Deck()
        {
            int i;

            for (i = 0; i < 52; i++)
            {
                eRankType rank = (eRankType)(i % 13);
                eSuitType suit = (eSuitType)(i / 13);

                Card card = new Card(rank, suit);

                cards[i] = card;
            }

            Shuffle();
        }
Пример #5
0
        public Deck()
        {
            int i;

            for (i = 0; i < 52; i++)
            {
                var rank = (ERankType)(i % 13);
                var suit = (ESuitType)(i / 13);

                var card = new Card(rank, suit);

                _cards[i] = card;
            }

            Shuffle();
        }
Пример #6
0
        public void SeePlayerHand(int playerShowingHand, Card hole1, Card hole2, Hand bestHand)
        {
            if (_botTimeOutMilliSeconds > 0)
            {
                if (!IsBotBusy())
                {
                    _task = Task.Run(() => { RunSeePlayerHand(playerShowingHand, hole1, hole2, bestHand); });

                    // wait X amount of time for task to complete
                    if (!_task.Wait(_botTimeOutMilliSeconds))
                    {
                        // Note that the task is still running in the background
                        _bIsBotBusy = true;
                        Logger.Log("TIMEOUT: {0} Player {1}", MethodBase.GetCurrentMethod().Name, PlayerNum);
                    }
                }
                else
                {
                    // bot is busy still running the previous task
                    Logger.Log("BOT BUSY: {0} Player {1}", MethodBase.GetCurrentMethod().Name, PlayerNum);
                }
            }
            else
            {
                // timeout code disabled - just called method directly
                RunSeePlayerHand(playerShowingHand, hole1, hole2, bestHand);
            }
        }
Пример #7
0
 private void RunSeeBoardCard(EBoardCardType cardType, Card boardCard)
 {
     try
     {
         _player.SeeBoardCard(cardType, boardCard);
     }
     catch (Exception e)
     {
         Logger.Log(string.Format("EXCEPTION: {0} Player {1} : {2}", MethodBase.GetCurrentMethod().Name, PlayerNum, e.Message));
     }
 }
Пример #8
0
        private void RunReceiveHoleCards(Card hole1, Card hole2)
        {
            _holeCards[0] = hole1;
            _holeCards[1] = hole2;

            try
            {
                _player.ReceiveHoleCards(hole1, hole2);
            }
            catch (Exception e)
            {
                Logger.Log(string.Format("EXCEPTION: {0} Player {1} : {2}", MethodBase.GetCurrentMethod().Name, PlayerNum, e.Message));
            }
        }
Пример #9
0
        private void Showdown(Card[] board, ref HandRanker handRanker, int lastToAct)
        {
            var firstToAct = GetNextActivePlayer(lastToAct);
            Hand playerBestHand;
            var uncontestedPots = new List<int>();

            for (var potNum = 0; potNum < _potMan.Pots.Count; potNum++)
            {
                uncontestedPots.Add(potNum);
            }

            // evaluate and show hand for first player to act - flag them as winning for now
            playerBestHand = Hand.FindPlayersBestHand(_players[firstToAct].HoleCards(), board);
            BroadcastPlayerHand(firstToAct, playerBestHand);

            handRanker.AddHand(firstToAct, playerBestHand);
            uncontestedPots = uncontestedPots.Except( _potMan.GetPotsInvolvedIn(firstToAct)).ToList();

            // Loop through other active players 
            var currPlayer = GetNextActivePlayer(firstToAct);

            do
            {
                var player = _players[currPlayer];
                EActionType playersAction;
                int playersAmount;

                // if not first to act then player may fold without showing cards (unless all in)
                // Also don't allow a player to fold if they are involved in a currently uncontested (side) pot
                var potsInvolvedIn = _potMan.GetPotsInvolvedIn(currPlayer);
                if (player.StackSize > 0 && (uncontestedPots.Intersect(potsInvolvedIn).Count() == 0))
                {
                    player.GetAction(EStage.StageShowdown, 0, 0, 0, 0, _potMan.Size(), out playersAction, out playersAmount);
                }
                else
                {
                    playersAction = EActionType.ActionShow;
                }

                if (playersAction == EActionType.ActionFold)
                {
                    _players[currPlayer].IsActive = false;
                    BroadcastAction(EStage.StageShowdown, currPlayer, playersAction, 0);
                }
                else
                {
                    playerBestHand = Hand.FindPlayersBestHand(player.HoleCards(), board);
                    handRanker.AddHand(currPlayer, playerBestHand);
                    uncontestedPots = uncontestedPots.Except(potsInvolvedIn).ToList();

                    BroadcastPlayerHand(currPlayer, playerBestHand);
                }

                currPlayer = GetNextActivePlayer(currPlayer);

            } while (currPlayer != firstToAct);
        }
Пример #10
0
 public void ReceiveHoleCards(Card hole1, Card hole2)
 {
     _holeCards[0] = hole1;
     _holeCards[1] = hole2;
     player.ReceiveHoleCards(hole1, hole2);
 }
Пример #11
0
 public void SeeBoardCard(EBoardCardType cardType, Card boardCard)
 {
     // this is called to inform you of the board cards (3 flop cards, turn and river)
     System.Threading.Thread.Sleep(_sleepMilliSeconds);
 }
Пример #12
0
        private void BroadcastBoardCard(eBoardCardType cardType, Card boardCard)
        {
            System.Console.WriteLine("{0} {1}", cardType, boardCard.ValueStr());

            foreach (ServerHoldemPlayer player in players)
            {
                player.SeeBoardCard(cardType, boardCard);
            }
        }
Пример #13
0
 public void ReceiveHoleCards(Card hole1, Card hole2)
 {
 }
Пример #14
0
 public void SeeBoardCard(EBoardCardType cardType, Card boardCard)
 {
     // this is called to inform you of the board cards (3 flop cards, turn and river)
 }
Пример #15
0
        // Given two pocket cards and five board cards,
        // determine the best poker hand that can be formed using any 5 of the 7 cards
        public static Hand FindPlayersBestHand(Card[] pocketCards, Card[] board)
        {
            Card[] cards = new Card[7];
            Card[] currHandCards = new Card[5];
            int i, j, k, currCard;
            Hand bestHand = new Hand(board);  // default to play board
            Hand currHand;

            // Put all cards togther
            cards[0] = pocketCards[0];
            cards[1] = pocketCards[1];
            cards[2] = board[0];
            cards[3] = board[1];
            cards[4] = board[2];
            cards[5] = board[3];
            cards[6] = board[4];

            for (i = 0; i < 7; i++)
            {
                for (j = i + 1; j < 7; j++)
                {
                    // exclude cards at indices i and j, make poker hand
                    // with the other 5 cards
                    currCard = 0;
                    for (k = 0; k < 7; k++)
                    {
                        if ((k != i) && (k != j))
                        {
                            currHandCards[currCard] = cards[k];
                            currCard++;
                        }
                    }

                    currHand = new Hand(currHandCards);

                    // If this is better than current best rank (and sub ranks)
                    // then make this the new best hand
                    if (Hand.compareHands(currHand, bestHand) == -1)
                    {
                        bestHand = currHand;
                    }
                }
            }

            return bestHand;
        }
Пример #16
0
 public void SeeBoardCard(eBoardCardType cardType, Card boardCard)
 {
     player.SeeBoardCard(cardType, boardCard);
 }
Пример #17
0
        private void Showdown(Card[] board, ref List<int> winningPlayers, int lastToAct)
        {
            int currPlayer = GetNextActivePlayer(lastToAct);
            int firstToAct = currPlayer;
            Hand overallBestHand; 

            // evaluate and show hand for first player to act - flag them as winning for now
            overallBestHand = Hand.FindPlayersBestHand(players[firstToAct].HoleCards(), board);
            BroadcastPlayerHand(firstToAct, overallBestHand);
            winningPlayers.Add(firstToAct);

            // Loop through other active players 
            currPlayer = GetNextActivePlayer(currPlayer);

            do
            {
                ServerHoldemPlayer player = players[currPlayer];
                eActionType playersAction;
                int playersAmount;

                // if not first to act then player may fold without showing cards
                player.GetAction(eStage.STAGE_SHOWDOWN, 0, 0, 0, 0, potSize, out playersAction, out playersAmount);

                if (playersAction == eActionType.ACTION_FOLD)
                {
                    BroadcastAction(eStage.STAGE_SHOWDOWN, currPlayer, playersAction, 0);
                }
                else
                {
                    Hand playerBestHand = Hand.FindPlayersBestHand(player.HoleCards(), board);
                    BroadcastPlayerHand(currPlayer, playerBestHand);

                    int result = Hand.compareHands(overallBestHand, playerBestHand);
                    if (result == 1)
                    {
                        // this hand is better than current best hand
                        winningPlayers.Clear();
                    }

                    if (result >= 0)
                    {
                        // this hand is equal to or better than current best hand
                        overallBestHand = playerBestHand;
                        winningPlayers.Add(player.PlayerNum);
                    }
                }

                currPlayer = GetNextActivePlayer(currPlayer);

            } while (currPlayer != firstToAct);
        }
Пример #18
0
        public void PlayGame()
        {
            LoadConfig();

            bool bDone = false;
            int handNum = 0;

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

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

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

                // First betting round - get player actions and broadcast to all players until betting round done
                DoBettingRound(eStage.STAGE_PREFLOP, out lastToAct);

                if (GetNumActivePlayers() > 1)
                {
                    // deal flop
                    board[0] = deck.DealCard();
                    BroadcastBoardCard(eBoardCardType.BOARD_FLOP1, board[0]);

                    board[1] = deck.DealCard();
                    BroadcastBoardCard(eBoardCardType.BOARD_FLOP2, board[1]);

                    board[2] = deck.DealCard();
                    BroadcastBoardCard(eBoardCardType.BOARD_FLOP3, board[2]);

                    // Second betting round - get player actions and broadcast to all players until betting round done
                    DoBettingRound(eStage.STAGE_FLOP, out lastToAct);
                }

                if (GetNumActivePlayers() > 1)
                {
                    // deal turn
                    board[3] = deck.DealCard();
                    BroadcastBoardCard(eBoardCardType.BOARD_TURN, board[3]);

                    // Third betting round - get player actions and broadcast to all players until betting round done
                    DoBettingRound(eStage.STAGE_TURN, out lastToAct);
                }

                if (GetNumActivePlayers() > 1)
                {
                    // deal river
                    board[4] = deck.DealCard();
                    BroadcastBoardCard(eBoardCardType.BOARD_RIVER, board[4]);

                    // Fourth betting round - get player actions and broadcast to all players until betting round done
                    DoBettingRound(eStage.STAGE_RIVER, out lastToAct);
                }

                List<int> winningPlayers = new List<int>();

                if (GetNumActivePlayers() > 1)
                {
                    Showdown(board, ref winningPlayers, lastToAct);
                }
                else
                {
                    // winner is last active player
                    foreach (ServerHoldemPlayer player in players)
                    {
                        if (player.IsActive)
                        {
                            winningPlayers.Add(player.PlayerNum);
                        }
                    }
                }

                DistributeWinnings(winningPlayers);

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

                if (GetNumLivePlayers() > 1)
                {
                    // Move to next dealer 
                    dealerPlayerNum = GetNextLivePlayer(dealerPlayerNum);
                }
                else
                {
                    bDone = true;
                }

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

            EndOfGame();

            ConsoleKeyInfo cki;
            cki = System.Console.ReadKey();

        }
Пример #19
0
 private void RunSeePlayerHand(int playerShowingHand, Card hole1, Card hole2, Hand bestHand)
 {
     try
     {
         _player.SeePlayerHand(playerShowingHand, hole1, hole2, bestHand);
     }
     catch (Exception e)
     {
         Logger.Log(string.Format("EXCEPTION: {0} Player {1} : {2}", MethodBase.GetCurrentMethod().Name, PlayerNum, e.Message));
     }
 }
Пример #20
0
 public void SeePlayerHand(int playerNum, Card hole1, Card hole2, Hand bestHand)
 {
 }
Пример #21
0
 public void ReceiveHoleCards(Card hole1, Card hole2)
 {
     // receive your hole cards for this hand
 }
Пример #22
0
 public void SeePlayerHand(int playerNum, Card hole1, Card hole2, Hand bestHand)
 {
     player.SeePlayerHand(playerNum, hole1, hole2, bestHand);
 }
Пример #23
0
 public void SeePlayerHand(int playerNum, Card hole1, Card hole2, Hand bestHand)
 {
     // this is called to inform you of another players hand during the show down. 
     // bestHand is the best hand that they can form with their hole cards and the five board cards
 }
Пример #24
0
        public static Hand FindPlayersBestHand(IReadOnlyList<Card> cards)
        {
            if (cards.Count < 5)
            {
                throw new Exception("not enough cards to find best hand");
            }

            if (cards.Count > 7)
            {
                throw new Exception("too many cards to find best hand");
            }

            var currHandCards = new Card[5];
            var bestHand = new Hand(cards);   // if 5 cards just use them

            if (cards.Count == 6)
            {
                for (var i = 0; i < 6; i++)
                {
                    // exclude cards at indices i, make poker hand
                    // with the other 5 cards
                    var currCard = 0;
                    int k;
                    for (k = 0; k < 6; k++)
                    {
                        if (k != i)
                        {
                            currHandCards[currCard] = cards[k];
                            currCard++;
                        }
                    }

                    var currHand = new Hand(currHandCards);

                    // If this is better than current best rank (and sub ranks)
                    // then make this the new best hand
                    if (CompareHands(currHand, bestHand) == -1)
                    {
                        bestHand = currHand;
                    }
                }
            }
            else if (cards.Count == 7)
            {

                for (var i = 0; i < 7; i++)
                {
                    for (var j = i + 1; j < 7; j++)
                    {
                        // exclude cards at indices i and j, make poker hand
                        // with the other 5 cards
                        var currCard = 0;
                        int k;
                        for (k = 0; k < 7; k++)
                        {
                            if ((k != i) && (k != j))
                            {
                                currHandCards[currCard] = cards[k];
                                currCard++;
                            }
                        }

                        var currHand = new Hand(currHandCards);

                        // If this is better than current best rank (and sub ranks)
                        // then make this the new best hand
                        if (CompareHands(currHand, bestHand) == -1)
                        {
                            bestHand = currHand;
                        }
                    }
                }
            }

            return bestHand;
        }
Пример #25
0
 public void SeeBoardCard(EBoardCardType cardType, Card boardCard)
 {
 }
Пример #26
0
        // Uses an insertion sort
        private void _sortByRank()
        {
            var sortedCards = new Card[5];
            int i, j;

            sortedCards[0] = _cards[0];

            for (j = 1; j < 5; j++)
            {
                var key = _cards[j];

                i = j - 1;

                while ((i >= 0) && (sortedCards[i].Rank < key.Rank))
                {
                    sortedCards[i + 1] = sortedCards[i];
                    i--;
                }

                sortedCards[i + 1] = key;
            }

            for (i = 0; i < 5; i++)
            {
                _cards[i] = sortedCards[i];
            }
        }
Пример #27
0
 public void ReceiveHoleCards(Card hole1, Card hole2)
 {
     // receive your hole cards for this hand
     System.Threading.Thread.Sleep(_sleepMilliSeconds);
 }
Пример #28
0
        private void BroadcastBoardCard(EBoardCardType cardType, Card boardCard)
        {
            Logger.Log("{0} {1}", cardType, boardCard.ValueStr());

            foreach (var player in _players)
            {
                player.SeeBoardCard(cardType, boardCard);
            }
        }
Пример #29
0
 public void SeePlayerHand(int playerNum, Card hole1, Card hole2, Hand bestHand)
 {
     // this is called to inform you of another players hand during the show down. 
     // bestHand is the best hand that they can form with their hole cards and the five board cards
     System.Threading.Thread.Sleep(_sleepMilliSeconds);
 }
Пример #30
0
        public void PlayGame()
        {
            LoadConfig();

            var bDone = false;
            var handNum = 0;

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

            while (!bDone)
            {
                var board = new Card[5];
                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);

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

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

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

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

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

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

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

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

                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();
                }

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

            EndOfGame();
        }