The hand for the dealer. This is the same as the hands of the players, except that the dealer hand has a hidden card which is flipped over at the end.
Inheritance: Hand
Esempio n. 1
0
        public bool DealerMustHit(DealerHand hand)
        {
            if (hand.Soft)
            {
                return(hand.Value < Settings.DealerSoftStandThreshold);
            }

            return(hand.Value < Settings.DealerHardStandThreshold);
        }
Esempio n. 2
0
 private void MainGame(int count)
 {
     for (var i = 1; i < count; i++)
     {
         if (i % 2 == 1)
         {
             yourhand.Add(ShuffledDeck[i]);
         }
         else
         {
             DealerHand.Add(ShuffledDeck[i]);
         }
     }
     foreach (Card card in YourHand)
     {
         Console.WriteLine(card.Rank);
         if (card.Rank == 1 || card.Rank == 10 || card.Rank == 11 || card.Rank == 12 || card.Rank == 13)
         {
             YourScore += 10;
         }
         else
         {
             YourScore += card.Rank;
         }
     }
     foreach (Card card in DealerHand)
     {
         if (card.Rank == 1 || card.Rank == 10 || card.Rank == 11 || card.Rank == 12 || card.Rank == 13)
         {
             DealerScore += 10;
         }
         else
         {
             DealerScore += card.Rank;
         }
     }
     this.currentCount = count;
 }
Esempio n. 3
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();
        }
Esempio n. 4
0
        public void Play(IEnumerable <IBlackjackPlayer> players)
        {
            _shoe.Shuffle(SHUFFLES_PER_SHOE);
            bool[] done = new bool[players.Count()];

            while (done.Count(d => d == false) > 0)
            {
                if (_shoe.CardsDealt >= Settings.MinCardsDealtBeforeReshuffle)
                {
                    _shoe.Shuffle(SHUFFLES_PER_SHOE);
                }

                //get bets
                List <PlayerHand> hands = new List <PlayerHand>();
                for (int i = 0; i < players.Count(); i++)
                {
                    if (done[i])
                    {
                        continue;
                    }
                    var  player = players.ElementAt(i);
                    bool play   = player.PlayAnotherHand();

                    if (!play)
                    {
                        done[i] = true;
                        continue;
                    }

                    var bet = player.GetBet(Settings.MinimumBet, Settings.MaximumBet);
                    if (bet < Settings.MinimumBet)
                    {
                        bet = Settings.MinimumBet;
                    }
                    else if (bet > Settings.MaximumBet)
                    {
                        bet = Settings.MaximumBet;
                    }


                    hands.Add(new PlayerHand()
                    {
                        Player = player,
                        Bet    = bet
                    });
                }

                //deal cards
                foreach (var hand in hands)
                {
                    hand.AddCard(_shoe.NextCard());
                    hand.AddCard(_shoe.NextCard());
                }
                DealerHand dealerHand = new DealerHand();
                dealerHand.HiddenCard = _shoe.NextCard();
                dealerHand.AddCard(_shoe.NextCard());

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


                #region offer insurance
                if (Settings.InsuranceOffered && dealerHand.Cards.ElementAt(0).Rank == Ranks.Ace)
                {
                    for (int i = 0; i < hands.Count; i++)
                    {
                        if (hands[i].Finished)
                        {
                            continue;
                        }

                        info.HandToPlay = i;
                        if (hands[i].Player.BuyInsurance(info))
                        {
                            hands[i].Insured = true;
                            hands[i].Won    -= hands[i].Bet * Settings.InsuranceCost;
                        }
                    }

                    //payoff insurance --> everyone else loses
                    if (dealerHand.HiddenCard.HighValue == 10)
                    {
                        dealerHand.FlipHiddenCard();

                        foreach (var hand in hands)
                        {
                            if (hand.Insured)//payoff insurance bets
                            {
                                hand.Won += hand.Bet * Settings.InsuranceCost * Settings.InsurancePayoff;
                            }
                            if (hand.Value == 21)//blackjack pushes
                            {
                                hand.Won += hand.Bet;
                            }
                            hand.Finished = true;//game's over
                        }
                    }
                }

                #endregion

                //payoff blackjacks --> remove from hand
                foreach (var hand in hands)
                {
                    if (hand.Finished)
                    {
                        continue;
                    }

                    if (hand.Value == 21)
                    {
                        hand.Won     += hand.Bet * Settings.BlackjackPayoff;
                        hand.Finished = true;
                    }
                }

                //each player acts
                for (int i = 0; i < hands.Count; i++)
                {
                    if (hands[i].Finished)
                    {
                        continue;
                    }

                    info.HandToPlay = i;
                    var hand = hands[i];

                    //accept surrenders
                    if (CanSurrender(hand))
                    {
                        if (hand.Player.Surrender(info))
                        {
                            hands[i].Won      = Settings.SurrenderPayoff * hands[i].Bet;
                            hands[i].Finished = true;
                            continue;
                        }
                    }

                    //split loop
                    if (CanSplit(hand) && hand.Player.Split(info))
                    {
                        var secondHand = hand.Split();
                        hand.AddCard(_shoe.NextCard());
                        secondHand.AddCard(_shoe.NextCard());

                        hands.Insert(i + 1, secondHand);
                        i--;
                        continue;
                    }

                    //double down
                    if (CanDoubleDown(hand) && hand.Player.DoubleDown(info))
                    {
                        hand.Bet *= 2;
                        hand.AddCard(_shoe.NextCard());
                        if (hand.Value > 21)
                        {
                            hand.Finished = true;
                        }
                        continue;
                    }

                    //hit/stand loop
                    while (CanHit(hand) && hand.Player.Hit(info))
                    {
                        hand.AddCard(_shoe.NextCard());
                        if (hand.Value > 21)
                        {
                            hand.Finished = true;
                        }
                    }
                }

                //dealer acts (only if everyone hasn't busted or hit BJ)
                if (hands.Count(h => !h.Finished) > 0)
                {
                    dealerHand.FlipHiddenCard();
                    while (DealerMustHit(dealerHand))
                    {
                        dealerHand.AddCard(_shoe.NextCard());
                    }

                    //if the dealer busted, everyone wins
                    if (dealerHand.Value > 21)
                    {
                        foreach (var hand in hands)
                        {
                            if (!hand.Finished)
                            {
                                hand.Won += hand.Bet * 2;
                            }
                        }
                    }
                    else
                    {
                        //if the dealer didn't bust, then players win if they're closer to 21
                        foreach (var hand in hands)
                        {
                            if (!hand.Finished)
                            {
                                if (hand.Value > dealerHand.Value)
                                {
                                    hand.Won += hand.Bet * 2;
                                }
                                else if (hand.Value == dealerHand.Value)
                                {
                                    hand.Won += hand.Bet;
                                }
                            }
                        }
                    }
                }

                foreach (var hand in hands)
                {
                    hand.Player.Profit += hand.Won - hand.Bet;
                }

                for (int i = 0; i < done.Length; i++)
                {
                    if (!done[i])
                    {
                        players.ElementAt(i).HandOver(info);
                    }
                }
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        public void Play(IEnumerable<IBlackjackPlayer> players)
        {
            _shoe.Shuffle(SHUFFLES_PER_SHOE);
            bool[] done = new bool[players.Count()];

            while (done.Count(d => d == false) > 0)
            {
                if (_shoe.CardsDealt >= Settings.MinCardsDealtBeforeReshuffle)
                    _shoe.Shuffle(SHUFFLES_PER_SHOE);

                //get bets
                List<PlayerHand> hands = new List<PlayerHand>();
                for (int i = 0; i < players.Count(); i++)
                {
                    if (done[i])
                        continue;
                    var player = players.ElementAt(i);
                    bool play = player.PlayAnotherHand();

                    if (!play)
                    {
                        done[i] = true;
                        continue;
                    }

                    var bet = player.GetBet(Settings.MinimumBet, Settings.MaximumBet);
                    if (bet < Settings.MinimumBet)
                        bet = Settings.MinimumBet;
                    else if (bet > Settings.MaximumBet)
                        bet = Settings.MaximumBet;

                    hands.Add(new PlayerHand()
                    {
                        Player = player,
                        Bet = bet
                    });
                }

                //deal cards
                foreach (var hand in hands)
                {
                    hand.AddCard(_shoe.NextCard());
                    hand.AddCard(_shoe.NextCard());
                }
                DealerHand dealerHand = new DealerHand();
                dealerHand.HiddenCard = _shoe.NextCard();
                dealerHand.AddCard(_shoe.NextCard());

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

                #region offer insurance
                if (Settings.InsuranceOffered && dealerHand.Cards.ElementAt(0).Rank == Ranks.Ace)
                {
                    for (int i = 0; i < hands.Count; i++)
                    {
                        if (hands[i].Finished)
                            continue;

                        info.HandToPlay = i;
                        if (hands[i].Player.BuyInsurance(info))
                        {
                            hands[i].Insured = true;
                            hands[i].Won -= hands[i].Bet * Settings.InsuranceCost;
                        }
                    }

                    //payoff insurance --> everyone else loses
                    if (dealerHand.HiddenCard.HighValue == 10)
                    {
                        dealerHand.FlipHiddenCard();

                        foreach (var hand in hands)
                        {
                            if (hand.Insured)//payoff insurance bets
                                hand.Won += hand.Bet * Settings.InsuranceCost * Settings.InsurancePayoff;
                            if (hand.Value == 21)//blackjack pushes
                                hand.Won += hand.Bet;
                            hand.Finished = true;//game's over
                        }
                    }
                }

                #endregion

                //payoff blackjacks --> remove from hand
                foreach (var hand in hands)
                {
                    if (hand.Finished)
                        continue;

                    if (hand.Value == 21)
                    {
                        hand.Won += hand.Bet * Settings.BlackjackPayoff;
                        hand.Finished = true;
                    }
                }

                //each player acts
                for (int i = 0; i < hands.Count; i++)
                {
                    if (hands[i].Finished)
                        continue;

                    info.HandToPlay = i;
                    var hand = hands[i];

                    //accept surrenders
                    if (CanSurrender(hand))
                    {
                        if (hand.Player.Surrender(info))
                        {
                            hands[i].Won = Settings.SurrenderPayoff * hands[i].Bet;
                            hands[i].Finished = true;
                            continue;
                        }
                    }

                    //split loop
                    if (CanSplit(hand) && hand.Player.Split(info))
                    {
                        var secondHand = hand.Split();
                        hand.AddCard(_shoe.NextCard());
                        secondHand.AddCard(_shoe.NextCard());

                        hands.Insert(i+1, secondHand);
                        i--;
                        continue;
                    }

                    //double down
                    if (CanDoubleDown(hand) && hand.Player.DoubleDown(info))
                    {
                        hand.Bet *= 2;
                        hand.AddCard(_shoe.NextCard());
                        if (hand.Value > 21)
                            hand.Finished = true;
                        continue;
                    }

                    //hit/stand loop
                    while (CanHit(hand) && hand.Player.Hit(info))
                    {
                        hand.AddCard(_shoe.NextCard());
                        if (hand.Value > 21)
                            hand.Finished = true;
                    }
                }

                //dealer acts (only if everyone hasn't busted or hit BJ)
                if (hands.Count(h => !h.Finished) > 0)
                {
                    dealerHand.FlipHiddenCard();
                    while (DealerMustHit(dealerHand))
                        dealerHand.AddCard(_shoe.NextCard());

                    //if the dealer busted, everyone wins
                    if (dealerHand.Value > 21)
                    {
                        foreach (var hand in hands)
                            if (!hand.Finished)
                                hand.Won += hand.Bet * 2;
                    }
                    else
                    {
                        //if the dealer didn't bust, then players win if they're closer to 21
                        foreach (var hand in hands)
                            if (!hand.Finished)
                            {
                                if (hand.Value > dealerHand.Value)
                                    hand.Won += hand.Bet * 2;
                                else if (hand.Value == dealerHand.Value)
                                    hand.Won += hand.Bet;
                            }
                    }
                }

                foreach (var hand in hands)
                    hand.Player.Profit += hand.Won - hand.Bet;

                for (int i = 0; i < done.Length; i++)
                    if (!done[i])
                        players.ElementAt(i).HandOver(info);

            }
        }
Esempio n. 8
0
        public bool DealerMustHit(DealerHand hand)
        {
            if(hand.Soft)
                return hand.Value < Settings.DealerSoftStandThreshold;

            return hand.Value < Settings.DealerHardStandThreshold;
        }