A class representing a Blackjack player's hand.
Inheritance: Hand
Beispiel #1
0
        public bool CanDoubleDown(PlayerHand hand)
        {
            if (hand.Finished)
                return false;

            if (hand.Cards.Count() != 2)
                return false;

            if (hand.HasBeenSplit)
            {
                if (hand.Cards.ElementAt(0).Rank == Ranks.Ace)
                {
                    if (!Settings.DoubleDownSplitAcesAllowed)
                        return false;
                }
                else if (!Settings.DoubleDownNonAceSplitsAllowed)
                    return false;
            }

            var c1 = hand.Cards.ElementAt(0);
            var c2 = hand.Cards.ElementAt(1);

            if(!Settings.SoftDoubleDownAllowed && (c1.Rank == Ranks.Ace || c2.Rank == Ranks.Ace))
                return false;

            int low = c1.LowValue + c2.LowValue;
            if (Settings.DoubleDownOnlyTenOrEleven && (low < 10 || low > 11))
                return false;

            return true;
        }
Beispiel #2
0
        public bool CanHit(PlayerHand hand)
        {
            if (hand.Finished)
                return false;

            if (hand.Value > 21)
                return false;

            //check if the hand split was two aces and if the player is not
            //allowed to hit split aces
            if (hand.HasBeenSplit && !Settings.HittingSplitAcesAllowed && hand.Cards.ElementAt(0).Rank == Ranks.Ace)
                return false;

            return true;
        }
Beispiel #3
0
        public bool CanDoubleDown(PlayerHand hand)
        {
            if (hand.Finished)
            {
                return(false);
            }

            if (hand.Cards.Count() != 2)
            {
                return(false);
            }

            if (hand.HasBeenSplit)
            {
                if (hand.Cards.ElementAt(0).Rank == Ranks.Ace)
                {
                    if (!Settings.DoubleDownSplitAcesAllowed)
                    {
                        return(false);
                    }
                }
                else if (!Settings.DoubleDownNonAceSplitsAllowed)
                {
                    return(false);
                }
            }

            var c1 = hand.Cards.ElementAt(0);
            var c2 = hand.Cards.ElementAt(1);

            if (!Settings.SoftDoubleDownAllowed && (c1.Rank == Ranks.Ace || c2.Rank == Ranks.Ace))
            {
                return(false);
            }

            int low = c1.LowValue + c2.LowValue;

            if (Settings.DoubleDownOnlyTenOrEleven && (low < 10 || low > 11))
            {
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        public PlayerHand Split()
        {
            PlayerHand p1 = new PlayerHand()
            {
                Bet = this.Bet,
                Player = this.Player,
                HasBeenSplit = true
            };
            Card temp = cards[0];
            p1.AddCard(cards[1]);

            cards.Clear();
            Value = 0;
            Soft = false;
            AddCard(temp);
            HasBeenSplit = true;

            return p1;
        }
Beispiel #5
0
        public bool CanHit(PlayerHand hand)
        {
            if (hand.Finished)
            {
                return(false);
            }

            if (hand.Value > 21)
            {
                return(false);
            }

            //check if the hand split was two aces and if the player is not
            //allowed to hit split aces
            if (hand.HasBeenSplit && !Settings.HittingSplitAcesAllowed && hand.Cards.ElementAt(0).Rank == Ranks.Ace)
            {
                return(false);
            }

            return(true);
        }
Beispiel #6
0
        public PlayerHand Split()
        {
            PlayerHand p1 = new PlayerHand()
            {
                Bet          = this.Bet,
                Player       = this.Player,
                HasBeenSplit = true
            };
            Card temp = cards[0];

            p1.AddCard(cards[1]);


            cards.Clear();
            Value = 0;
            Soft  = false;
            AddCard(temp);
            HasBeenSplit = true;

            return(p1);
        }
Beispiel #7
0
        public GameServant(bool testing = false)
        {
            log = new List<string>();
            l(string.Format("New Game ({0}:{1}:{2})", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), false);
            if (!testing) {
                l("In production mode...", false);
                this.ActiveHand = ActiveHandPotentials.None;
                l("Creating Source Deck", false);
                this._source = new CardCollection(1);
                l("Creating Discard Deck", false);
                this._discard = new CardCollection(1, false);

                this.ActiveHand = ActiveHandPotentials.Normal;

                l("Creating the player's hand", false);
                this._playerHand = new PlayerHand(_source, _discard);
                l("Creating the dealer's hand", false);
                this._dealerHand = new DealerHand(_source, _discard);
                l("Getting user name", false);
                (new GetUserName(this)).ShowDialog();
                l("User name received", false);
            } else {
                l("In testing mode...", false);
                this.ActiveHand = ActiveHandPotentials.None;
                var d = new Microsoft.Win32.OpenFileDialog();
                d.Filter = "CSV Files (.csv)|*.csv";
                d.ShowDialog();
                this._source = new PredeterministicCardCollection(d.FileName);
                this._discard = new CardCollection(Int32.MaxValue, false);

                this.ActiveHand = ActiveHandPotentials.Normal;
                this._playerHand = new PlayerHand(_source, _discard);
                this._dealerHand = new DealerHand(_source, _discard);
                (new GetUserName(this)).ShowDialog();
            }
            NotifyAll();
        }
Beispiel #8
0
        public bool CanSplit(PlayerHand hand)
        {
            if (hand.Finished)
            {
                return(false);
            }

            if (hand.Player.Splits >= Settings.MaxSplitsAllowed)
            {
                return(false);
            }

            if (hand.Cards.Count() != 2)
            {
                return(false);
            }

            var c = hand.Cards.ElementAt(0);

            if (c.HighValue != hand.Cards.ElementAt(1).HighValue)
            {
                return(false);
            }

            if (!Settings.ResplitAcesAllowed && c.Rank == Ranks.Ace && hand.HasBeenSplit)
            {
                return(false);
            }

            if (!Settings.SplitTensAllowed && c.HighValue == 10)
            {
                return(false);
            }

            return(true);
        }
Beispiel #9
0
        public static ActionTable FromStrategy(IBlackjackPlayer strategy)
        {
            var table = new ActionTypes[35, 10];
            List<PlayerHand> hands = new List<PlayerHand>();
            hands.Add(null);
            for (int dealer = 0; dealer < 10; dealer++)
            {
                DealerHand dealerHand = new DealerHand();
                dealerHand.AddCard(new Card((Ranks)dealer));

                for (int p = 0; p < 35; p++)
                {
                    PlayerHand playerHand = new PlayerHand()
                    {
                        Player = strategy,
                        Bet = 1
                    };

                    if (p < 10)
                    {
                        playerHand.AddCard(new Card((Ranks)p));
                        playerHand.AddCard(new Card((Ranks)p));
                    }
                    else if (p < 19)
                    {
                        playerHand.AddCard(new Card(Ranks.Ace));
                        playerHand.AddCard(new Card((Ranks)(p - 10)));
                    }
                    else if (p < 26)
                    {
                        playerHand.AddCard(new Card(Ranks.Two));
                        playerHand.AddCard(new Card((Ranks)(p - 18)));
                    }
                    else
                    {
                        playerHand.AddCard(new Card(Ranks.Ten));
                        playerHand.AddCard(new Card((Ranks)(p - 26)));
                    }
                    hands[0] = playerHand;

                    HandInfo info = new HandInfo()
                    {
                        DealerHand = dealerHand,
                        HandToPlay = 0,
                        PlayerHands = hands
                    };

                    var hs = strategy.Hit(info) ? ActionTypes.Hit : ActionTypes.Stand;
                    var type = hs;
                    if (p < 10 && strategy.Split(info))
                        type = hs == ActionTypes.Hit ? ActionTypes.SplitOrHit : ActionTypes.SplitOrStand;
                    else if (strategy.DoubleDown(info))
                        type = hs == ActionTypes.Hit ? ActionTypes.DoubleDownOrHit : ActionTypes.DoubleDownOrStand;

                    table[p, dealer] = type;
                }

            }

            return new ActionTable(table);
        }
Beispiel #10
0
        /// <summary>
        /// Compares 'on' to the dealer's hand for win/loss and does any needed action with the player's cash/bet
        /// </summary>
        /// <param name="on">a playerhand to compare against</param>
        /// <returns>Who won the round, if either.</returns>
        public WinLoss Winner(PlayerHand on)
        {
            l("Checking Win/Loss");
            WinLoss ret = WinLoss.NoWin;
            if (on.IsBlackjack && DealerHand.IsBlackjack) {
                l("Both hands blackjack: push");
                ret = WinLoss.Push;
            } else if (on.IsBust && DealerHand.IsBust) {
                l(String.Format("Both hands bust. Hand Sum: {0} Dealer Sum: {1}", on.Sum, DealerHand.Sum));
                if (on.Sum < DealerHand.Sum) {
                    l("Player wins");
                    ret = WinLoss.Player;
                } else if (on.Sum > DealerHand.Sum) {
                    l("Dealer wins");
                    ret = WinLoss.Dealer;
                } else {
                    l("Push");
                    ret = WinLoss.Push;
                }
            } else if (on.IsBust || DealerHand.IsBlackjack) {
                l("Hand busted or Dealer blackjack - dealer wins");
                ret = WinLoss.Dealer;
            } else if (DealerHand.IsBust || on.IsBlackjack) {
                l("Dealer busted or Hand is blackjack - player wins");
                ret = WinLoss.Player;
            } else {
                l(String.Format("No special case. Hand Sum: {0} Dealer Sum: {1}", on.Sum, DealerHand.Sum));
                if (on.Sum > DealerHand.Sum) {
                    l("Player wins");
                    ret = WinLoss.Player;
                } else if (on.Sum < DealerHand.Sum) {
                    l("Dealer wins");
                    ret = WinLoss.Dealer;
                } else {
                    l("Push");
                    ret = WinLoss.Push;
                }
            }

            l("Updating statistics");
            switch (ret) {
                case WinLoss.Dealer:
                    _numLosses++;
                    _thisLoss += on.Bet;
                    break;
                case WinLoss.Player:
                    _numWins++;
                    _thisWin += on.Bet;
                    break;
                default:
                    break;
            }
            l(String.Format("Wins:Losses;Win:Loss::{0}:{1};{2}:{3}", NumWins, NumLosses, LargestWin, LargestLoss));

            if (ret != WinLoss.NoWin) {
                l("Applying bet");
                on.doBet(ret);
            }

            return ret;
        }
Beispiel #11
0
        public static ActionTable FromStrategy(IBlackjackPlayer strategy)
        {
            var table = new ActionTypes[35, 10];
            List <PlayerHand> hands = new List <PlayerHand>();

            hands.Add(null);
            for (int dealer = 0; dealer < 10; dealer++)
            {
                DealerHand dealerHand = new DealerHand();
                dealerHand.AddCard(new Card((Ranks)dealer));

                for (int p = 0; p < 35; p++)
                {
                    PlayerHand playerHand = new PlayerHand()
                    {
                        Player = strategy,
                        Bet    = 1
                    };

                    if (p < 10)
                    {
                        playerHand.AddCard(new Card((Ranks)p));
                        playerHand.AddCard(new Card((Ranks)p));
                    }
                    else if (p < 19)
                    {
                        playerHand.AddCard(new Card(Ranks.Ace));
                        playerHand.AddCard(new Card((Ranks)(p - 10)));
                    }
                    else if (p < 26)
                    {
                        playerHand.AddCard(new Card(Ranks.Two));
                        playerHand.AddCard(new Card((Ranks)(p - 18)));
                    }
                    else
                    {
                        playerHand.AddCard(new Card(Ranks.Ten));
                        playerHand.AddCard(new Card((Ranks)(p - 26)));
                    }
                    hands[0] = playerHand;

                    HandInfo info = new HandInfo()
                    {
                        DealerHand  = dealerHand,
                        HandToPlay  = 0,
                        PlayerHands = hands
                    };

                    var hs   = strategy.Hit(info) ? ActionTypes.Hit : ActionTypes.Stand;
                    var type = hs;
                    if (p < 10 && strategy.Split(info))
                    {
                        type = hs == ActionTypes.Hit ? ActionTypes.SplitOrHit : ActionTypes.SplitOrStand;
                    }
                    else if (strategy.DoubleDown(info))
                    {
                        type = hs == ActionTypes.Hit ? ActionTypes.DoubleDownOrHit : ActionTypes.DoubleDownOrStand;
                    }

                    table[p, dealer] = type;
                }
            }

            return(new ActionTable(table));
        }
Beispiel #12
0
        /// <summary>
        /// Splits this hand into two separate hands with one card each.
        /// This hand becomes the first hand and the second hand is returned.
        /// </summary>
        /// <returns></returns>
        public PlayerHand Split()
        {
            // Create the new hand.
            PlayerHand p1 = new PlayerHand()
            {
                Bet = this.Bet,
                Player = this.Player,
                HasBeenSplit = true
            };

            // Add the top card to the new hand.
            p1.AddCard(cards[1]);

            // Save the current bottom card for this hand.
            Card temp = cards[0];

            // Clear the cards from this hand.
            cards.Clear();

            // Reset this hand.
            Value = 0;
            Soft = false;

            // Add the bottom card to this hand.
            AddCard(temp);

            // Flag that this hand has been split.
            HasBeenSplit = true;

            // Return the new hand.
            return p1;
        }
Beispiel #13
0
 public void Split()
 {
     psplit = new PlayerHand(SourceCollection, DiscardCollection);
     psplit.Cards.Add(this.Discard(0));
     psplit.psplit = this;
     psplit.myBet = myBet;
     psplit.myCash = 0;
     myCash -= myBet;
 }
Beispiel #14
0
 public override void PutCardsBack()
 {
     if (HasSplit) {
         //psplit.DiscardAll(); //TODO
         psplit = null;
     }
     DiscardAll();
 }
Beispiel #15
0
        public bool CanSplit(PlayerHand hand)
        {
            if (hand.Finished)
                return false;

            if (hand.Cards.Count() != 2)
                return false;

            var c = hand.Cards.ElementAt(0);
            if (c.HighValue != hand.Cards.ElementAt(1).HighValue)
                return false;

            if (!Settings.ResplitAcesAllowed && c.Rank == Ranks.Ace && hand.HasBeenSplit)
                return false;

            if (!Settings.SplitTensAllowed && c.HighValue == 10)
                return false;

            return true;
        }
Beispiel #16
0
 public bool CanSurrender(PlayerHand hand)
 {
     if (hand.Finished)
         return false;
     if (hand.Cards.Count() != 2)
         return false;
     return Settings.SurrenderAllowed && !hand.HasBeenSplit;
 }