Example #1
0
        public void Insert(PlayedHandEntity hand)
        {
            hand.Id = Guid.NewGuid();
            using (IDbConnection db = new SqlConnection(connectionString))
            {
                string sqlInsert = @"INSERT INTO [dbo].[PlayedHand]
                   ([Id],[WinnerId],[PotSize],[Timestamp],[SessionId],[Holecards1],[Holecards2],[Board],[AmountWon],[Phase])
                    VALUES
                   (@Id, @WinnerId, @Potsize, @Timestamp, @SessionId,@Holecards1,@Holecards2,@Board,@AmountWon,@Phase)";

                db.Execute(sqlInsert,
                           new
                {
                    Id         = hand.Id,
                    WinnerId   = hand.WinnerId,
                    PotSize    = hand.PotSize,
                    Timestamp  = hand.Timestamp,
                    SessionId  = hand.SessionId,
                    Holecards1 = hand.HoleCards1,
                    Holecards2 = hand.HoleCards2,
                    Board      = hand.Board,
                    AmountWon  = hand.AmountWon,
                    Phase      = hand.Phase
                });
            }

            foreach (var action in hand.Actions)
            {
                action.HandId = hand.Id;
                gameActonRepository.Insert(action);
            }
        }
Example #2
0
 public void StartNewRound(PlayedHandEntity result)
 {
     initializeValues();
     moveBlinds();
     foreach (var player in players)
     {
         PotSize += player.GetBlind();
     }
     dealer.DealHoleCards(players);
     result.HoleCards1 = players[0].HoleCards.ToAbbreviations();
     result.HoleCards2 = players[1].HoleCards.ToAbbreviations();
 }
Example #3
0
        private void showDown(Player smallBlindPlayer, Player bigBlindPlayer, PlayedHandEntity playedHand)
        {
            HandComparison comparison = HandComparer.Compare(smallBlindPlayer.HoleCards, bigBlindPlayer.HoleCards, Board);

            switch (comparison)
            {
            case HandComparison.None:
                //no winner is determined = split pot
                break;

            case HandComparison.Player1Won:
                playedHand.WinnerId = smallBlindPlayer.Id;
                break;

            case HandComparison.Player2Won:
                playedHand.WinnerId = bigBlindPlayer.Id;
                break;
            }

            playedHand.AmountWon = PotSize / 2;
            playedHand.PotSize   = PotSize;
            this.Phase           = GamePhase.Showdown;
        }
Example #4
0
        private void playBettingRound(Player playerFirstToAct, Player playerSecondToAct, List <ActionType> possibleActions, int amountToCall, PlayedHandEntity result, out bool roundFinished, out bool goToShowdown)
        {
            roundFinished = false;
            goToShowdown  = false;

            bool bothPlayersActed     = false;
            bool bettingRoundFinished = false;
            var  playerToAct          = playerFirstToAct;
            var  minRaiseAmount       = MinAmountToBetFactor * BigBlindSize;

            while (!bettingRoundFinished)
            {
                var playerAction = playerToAct.GetAction(possibleActions, amountToCall).Result;
                playerAction.Timestamp = DateTime.Now;
                result.Actions.Add(playerAction);
                onPlayerActed(playerAction.PlayerId, playerAction.ActionType, amountToCall, playerAction.Amount);

                if (possibleActions.Contains(playerAction.ActionType))
                {
                    switch (playerAction.ActionType)
                    {
                    case ActionType.Call:
                        if (bothPlayersActed)
                        {
                            bettingRoundFinished = true;
                        }
                        else
                        {
                            possibleActions = new List <ActionType> {
                                ActionType.Check, ActionType.Bet
                            };
                        }
                        playerToAct.ChipStack -= playerAction.Amount;
                        PotSize += playerAction.Amount;
                        playerToAct.AmountAlreadyInPotThisRound = 0;
                        if (playerToAct.ChipStack == 0)
                        {
                            goToShowdown = true;
                        }
                        amountToCall = 0;
                        break;

                    case ActionType.Check:
                        if (bothPlayersActed)
                        {
                            bettingRoundFinished = true;
                        }
                        playerToAct.AmountAlreadyInPotThisRound = 0;
                        break;

                    case ActionType.Fold:
                        bettingRoundFinished = true;
                        roundFinished        = true;
                        playerToAct.AmountAlreadyInPotThisRound = 0;

                        //the other player wins this hand
                        if (playerToAct == playerFirstToAct)
                        {
                            result.WinnerId = playerSecondToAct.Id;
                        }
                        else
                        {
                            result.WinnerId = playerFirstToAct.Id;
                        }

                        if ((PotSize - amountToCall) < 2 * BigBlindSize)
                        {
                            //special cases: big blind or small blind won
                            if (playerToAct.IsSmallBlind)
                            {
                                result.AmountWon = SmallBlindSize;
                            }
                            else
                            {
                                result.AmountWon = BigBlindSize;
                            }
                        }
                        else
                        {
                            result.AmountWon = (PotSize - amountToCall) / 2;
                        }
                        result.PotSize = PotSize;
                        break;

                    case ActionType.Bet:
                        if (playerAction.Amount < BigBlindSize && playerToAct.ChipStack >= BigBlindSize)
                        {
                            var exceptionmessage = String.Format("Player {0} invoked illegal action. Bet amount: {1} Min amount to bet: {2}", playerToAct.Name, playerAction.Amount, BigBlindSize);
                            throw new IllegalActionException(exceptionmessage);
                        }

                        minRaiseAmount         = playerAction.Amount * 2;
                        playerToAct.ChipStack -= playerAction.Amount;
                        amountToCall           = playerAction.Amount;
                        playerToAct.AmountAlreadyInPotThisRound = playerAction.Amount;
                        PotSize        += playerAction.Amount;
                        possibleActions = new List <ActionType> {
                            ActionType.Fold, ActionType.Call, ActionType.Raise
                        };
                        break;

                    case ActionType.Raise:
                        //e.g. SB raises to 6 preflop. Amount is 5 in this case. MoneyAlreadyInPot is 1
                        var amountToCheck = (playerAction.Amount + playerToAct.AmountAlreadyInPotThisRound);
                        if (amountToCheck < minRaiseAmount && playerToAct.ChipStack > amountToCheck)
                        {
                            var exceptionmessage = String.Format("Player {0} invoked illegal action. Bet amount: {1} Min amount to bet: {2}", playerToAct.Name, playerAction.Amount, minRaiseAmount);
                            throw new IllegalActionException(exceptionmessage);
                        }
                        //minraise is not enough for second raise
                        playerToAct.ChipStack -= playerAction.Amount;
                        PotSize += playerAction.Amount;

                        amountToCall = playerAction.Amount - amountToCall;

                        if (playerToAct.ChipStack > 0)
                        {
                            possibleActions = new List <ActionType> {
                                ActionType.Fold, ActionType.Call, ActionType.Raise
                            };
                        }
                        else
                        {
                            possibleActions = new List <ActionType> {
                                ActionType.Fold, ActionType.Call
                            };
                        }
                        break;
                    }
                }
                else
                {
                    var exceptionMessage = String.Format("Player {0} invoked illegal action {1}", playerToAct.Name, playerAction.ActionType);
                    throw new IllegalActionException(exceptionMessage);
                }

                if (!bettingRoundFinished)
                {
                    //switch playerToAct
                    playerToAct      = players.First(x => x != playerToAct);
                    bothPlayersActed = true;
                }
            }
        }
Example #5
0
        public PlayedHandEntity PlayHand()
        {
            var  playedHand    = new PlayedHandEntity();
            bool roundFinished = false;
            bool goToShowdown  = false;

            StartNewRound(playedHand);

            var smallBlindPlayer = players.First(x => x.IsSmallBlind);
            var bigBlindPlayer   = players.First(x => x.IsBigBlind);

            var possibleActions = new List <ActionType> {
                ActionType.Fold, ActionType.Call, ActionType.Raise
            };
            var firstAmountToCall = SmallBlindSize;

            playBettingRound(smallBlindPlayer, bigBlindPlayer, possibleActions, firstAmountToCall, playedHand, out roundFinished, out goToShowdown);
            if (roundFinished)
            {
                playedHand.Phase = this.Phase;
                return(playedHand);
            }

            StartNextPhase(GamePhase.Flop, out possibleActions, out firstAmountToCall);
            if (!goToShowdown)
            {
                playBettingRound(smallBlindPlayer, bigBlindPlayer, possibleActions, firstAmountToCall, playedHand, out roundFinished, out goToShowdown);
                if (roundFinished)
                {
                    playedHand.Phase = this.Phase;
                    playedHand.Board = this.Board.ToAbbreviations();
                    return(playedHand);
                }
            }

            StartNextPhase(GamePhase.Turn, out possibleActions, out firstAmountToCall);
            if (!goToShowdown)
            {
                playBettingRound(smallBlindPlayer, bigBlindPlayer, possibleActions, firstAmountToCall, playedHand, out roundFinished, out goToShowdown);
                if (roundFinished)
                {
                    playedHand.Phase = this.Phase;
                    playedHand.Board = this.Board.ToAbbreviations();
                    return(playedHand);
                }
            }

            StartNextPhase(GamePhase.River, out possibleActions, out firstAmountToCall);
            if (!goToShowdown)
            {
                playBettingRound(smallBlindPlayer, bigBlindPlayer, possibleActions, firstAmountToCall, playedHand, out roundFinished, out goToShowdown);
                if (roundFinished)
                {
                    playedHand.Phase = this.Phase;
                    playedHand.Board = this.Board.ToAbbreviations();
                    return(playedHand);
                }
            }

            showDown(smallBlindPlayer, bigBlindPlayer, playedHand);

            onAfterShowdown(playedHand.WinnerId);
            playedHand.Board = this.Board.ToAbbreviations();
            playedHand.Phase = this.Phase;
            return(playedHand);
        }
Example #6
0
 public void Update(PlayedHandEntity entity)
 {
     throw new NotImplementedException();
 }