private CardSet GetSeenCards(Game game, bool showdown)
        {
            CardSet seen_cards = new CardSet();

            if (showdown)
            {
                foreach (Card card in game.DealerHand)
                {
                    seen_cards.Add(card);
                }
            }
            else
            {
                seen_cards.Add(game.DealerHand[0]);
            }

            foreach (Hand hand in game.PlayerHandSet)
            {
                if (hand.IsSplit()) continue;

                foreach (Card card in hand)
                {
                    seen_cards.Add(card);
                }
            }

            return seen_cards;
        }
Example #2
0
        public static CardSet operator +(CardSet set1, CardSet set2)
        {
            CardSet tmp = new CardSet(set1);
            tmp.Add(set2);

            return tmp;
        }
Example #3
0
 public void Add(CardSet cards)
 {
     foreach (Card c in cards)
     {
         total++;
         counts[c.PointValue - 1]++;
     }
 }
Example #4
0
 public Hand(CardSet cards)
     : this()
 {
     foreach (Card c in cards)
     {
         Hit(c);
     }
 }
Example #5
0
        public void Action(Card dealer_upcard, CardSet[] player_hands, int active_hand_index, List<ActionEv> actions)
        {
            ActionInfo info = new ActionInfo() {
                hand_index = active_hand_index,
                player_cards = player_hands[active_hand_index],
                action_evs = actions.ToArray()
            };

            action_history.Add(info);
        }
Example #6
0
 public void Remove(CardSet cards)
 {
     foreach (Card c in cards)
     {
         if (card_set.Remove(c))
         {
             RemoveCount(c);
         }
     }
 }
Example #7
0
        public object Clone()
        {
            CardSet cloned_set = new CardSet();

            foreach (Card card in this.card_set)
            {
                cloned_set.Add((Card)(card.Clone()));
            }

            return(cloned_set);
        }
Example #8
0
        public CardSet ExtractTop(int count)
        {
            CardSet set = new CardSet();

            for (int i = 0; i < count; i++)
            {
                if (card_set.Count == 0)
                {
                    break;
                }

                set.Add(ExtractTop());
            }

            return(set);
        }
Example #9
0
        public void Showdown(CardSet dealer_hand, CardSet[] player_hands, long game_id, int expected_money, int actual_money)
        {
            if (game_id <= 0)
            {
                long     ticks = DateTime.Now.ToUniversalTime().Ticks;
                TimeSpan time  = new TimeSpan(ticks);
                game_id = ((long)(time.TotalSeconds * 10)) % (int.MaxValue);
            }

            TextWriter file = new StreamWriter("gamelog.txt", true);

            file.WriteLine("GameStart {0}", game_id);
            file.WriteLine("Time {0}", DateTime.Now.ToUniversalTime().ToString(new CultureInfo("en-US", true)));
            file.WriteLine("Bet {0}", bet_size);
            file.WriteLine("Shoe {0}", shoe);
            file.WriteLine("ShoeEv {0}", shoe_ev);
            file.WriteLine("Insurance {0}", take_insurance);

            foreach (ActionInfo action in action_history)
            {
                file.Write("Action {0} | {1}", action.hand_index, action.player_cards);

                foreach (ActionEv ev in action.action_evs)
                {
                    file.Write(" | {0} {1}", ev.Action, ev.Ev);
                }

                file.WriteLine();
            }

            for (int i = 0; i < player_hands.Length; i++)
            {
                file.WriteLine("Showdown {0} | {1}", i, player_hands[i]);
            }

            file.WriteLine("Dealer {0}", dealer_hand);
            file.WriteLine("ExpectedMoney {0}", expected_money);
            file.WriteLine("ActualMoney {0}", actual_money);
            file.WriteLine("GameEnd {0}", game_id);
            file.WriteLine();

            file.Close();

            Console.WriteLine("Game ID: " + game_id);
            Console.WriteLine("Expected: " + expected_money);
            Console.WriteLine("Actual:   " + actual_money);
        }
Example #10
0
        public void Showdown(CardSet dealer_hand, CardSet[] player_hands, long game_id, int expected_money, int actual_money)
        {
            if (game_id <= 0)
            {
                long ticks = DateTime.Now.ToUniversalTime().Ticks;
                TimeSpan time = new TimeSpan(ticks);
                game_id = ((long)(time.TotalSeconds * 10)) % (int.MaxValue);
            }

            TextWriter file = new StreamWriter("gamelog.txt", true);

            file.WriteLine("GameStart {0}", game_id);
            file.WriteLine("Time {0}", DateTime.Now.ToUniversalTime().ToString(new CultureInfo("en-US", true)));
            file.WriteLine("Bet {0}", bet_size);
            file.WriteLine("Shoe {0}", shoe);
            file.WriteLine("ShoeEv {0}", shoe_ev);
            file.WriteLine("Insurance {0}", take_insurance);

            foreach (ActionInfo action in action_history)
            {
                file.Write("Action {0} | {1}", action.hand_index, action.player_cards);

                foreach (ActionEv ev in action.action_evs)
                {
                    file.Write(" | {0} {1}", ev.Action, ev.Ev);
                }

                file.WriteLine();
            }

            for (int i=0; i<player_hands.Length; i++)
            {
                file.WriteLine("Showdown {0} | {1}", i, player_hands[i]);
            }

            file.WriteLine("Dealer {0}", dealer_hand);
            file.WriteLine("ExpectedMoney {0}", expected_money);
            file.WriteLine("ActualMoney {0}", actual_money);
            file.WriteLine("GameEnd {0}", game_id);
            file.WriteLine();

            file.Close();

            Console.WriteLine("Game ID: " + game_id);
            Console.WriteLine("Expected: " + expected_money);
            Console.WriteLine("Actual:   " + actual_money);
        }
        public override bool TakeInsurance(CardSet seen_cards)
        {
            Shoe tmp_shoe = shoe.Copy();

            tmp_shoe.Remove(seen_cards);

            double insurance_ev = Eval.InsuranceEv(current_bet, tmp_shoe.ToArray());

            if (insurance_ev >= 0.0)
            {
                game_logger.Insurance(true);
                insurance_taken = true;
                return(true);
            }

            return(false);
        }
        private double GetActionEV(Shoe tmp_shoe, CardSet active_hand, ActionType action, Card dealer_upcard)
        {
            Hand hand = new Hand(active_hand);

            SHand shand;
            int   soft_total = hand.SoftTotal();

            if (soft_total <= 21 && hand.HasAce())
            {
                shand.Total = soft_total;
                shand.Soft  = true;
            }
            else
            {
                shand.Total = hand.HardTotal();
                shand.Soft  = false;
            }

            int[] shoe_counts = tmp_shoe.ToArray();
            int   upcard      = dealer_upcard.PointValue;

            switch (action)
            {
            case ActionType.Stand:

                return(Eval.StandEv(shand, upcard, shoe_counts));

            case ActionType.Hit:

                return(Eval.HitEv(shand, upcard, shoe_counts));

            case ActionType.Double:

                return(Eval.DoubleEv(shand, upcard, current_bet, shoe_counts));

            case ActionType.Split:

                return(Eval.SplitEv(active_hand[0].PointValue, upcard, current_bet, max_splits - split_count, shoe_counts));

            case ActionType.Surrender:

                return(Eval.SurrenderEv());
            }

            return(-1);
        }
Example #13
0
        public Game(Rules rules, Agent agent, double pp_multiplier, Random random)
        {
            dealer_hand    = new Hand();
            player_handset = new HandSet();
            shoe           = new CardSet();

            this.rules         = rules;
            this.agent         = agent;
            this.pp_multiplier = pp_multiplier;
            this.random        = random;

            this.player_money = 0;
            this.bet          = rules.MinBet;
            this.split_count  = 0;

            party_points = 0;

            ResetShoe();
        }
Example #14
0
        public CardSet ExtractRandom(Random rand, int count)
        {
            CardSet set = new CardSet();

            for (int i = 0; i < count; i++)
            {
                if (card_set.Count == 0)
                {
                    break;
                }

                int card_index = rand.Next(card_set.Count);
                set.Add((Card)card_set[card_index]);
                RemoveCount(card_set[i]);
                card_set.RemoveAt(card_index);
            }

            return(set);
        }
Example #15
0
        public Game(Rules rules, Agent agent, double pp_multiplier, Random random)
        {
            dealer_hand = new Hand();
            player_handset = new HandSet();
            shoe = new CardSet();

            this.rules = rules;
            this.agent = agent;
            this.pp_multiplier = pp_multiplier;
            this.random = random;

            this.player_money = 0;
            this.bet = rules.MinBet;
            this.split_count = 0;

            party_points = 0;

            ResetShoe();
        }
        public override List <ActionEv> GetActions(Game game)
        {
            List <ActionType> allowed_actions = new List <ActionType>();

            if (game.IsValidAction(ActionType.Stand))
            {
                allowed_actions.Add(ActionType.Stand);
            }
            if (game.IsValidAction(ActionType.Hit))
            {
                allowed_actions.Add(ActionType.Hit);
            }
            if (game.IsValidAction(ActionType.Double))
            {
                allowed_actions.Add(ActionType.Double);
            }
            if (game.IsValidAction(ActionType.Split))
            {
                allowed_actions.Add(ActionType.Split);
            }
            if (game.IsValidAction(ActionType.Surrender))
            {
                allowed_actions.Add(ActionType.Surrender);
            }


            CardSet some_cards = new CardSet();

            //Hand active_hand = game.PlayerHandSet.ActiveHand;

            List <CardSet> player_hands = new List <CardSet>();

            foreach (Hand hand in game.PlayerHandSet)
            {
                player_hands.Add(hand.Cards);
            }

            return(agent.GetActions(GetSeenCards(game, false), game.DealerHand[0], player_hands.ToArray(), game.PlayerHandSet.ActiveIndex, allowed_actions));
        }
        public override List<ActionEv> GetActions(Game game)
        {
            List<ActionType> allowed_actions = new List<ActionType>();

            if (game.IsValidAction(ActionType.Stand)) allowed_actions.Add(ActionType.Stand);
            if (game.IsValidAction(ActionType.Hit)) allowed_actions.Add(ActionType.Hit);
            if (game.IsValidAction(ActionType.Double)) allowed_actions.Add(ActionType.Double);
            if (game.IsValidAction(ActionType.Split)) allowed_actions.Add(ActionType.Split);
            if (game.IsValidAction(ActionType.Surrender)) allowed_actions.Add(ActionType.Surrender);

            CardSet some_cards = new CardSet();

            //Hand active_hand = game.PlayerHandSet.ActiveHand;

            List<CardSet> player_hands = new List<CardSet>();
            foreach (Hand hand in game.PlayerHandSet)
            {
                player_hands.Add(hand.Cards);
            }

            return agent.GetActions(GetSeenCards(game, false), game.DealerHand[0], player_hands.ToArray(), game.PlayerHandSet.ActiveIndex, allowed_actions);
        }
        // seen_cards include active_hand's cards
        public override ActionType GetAction(CardSet seen_cards, Card dealer_upcard, CardSet[] player_hands, int active_hand, List <ActionType> available_actions)
        {
            List <ActionEv> actions = GetActions(seen_cards, dealer_upcard, player_hands, active_hand, available_actions);

            ActionType best = actions[0].Action;

            if (best == ActionType.Split)
            {
                split_count++;
            }
            else if (best == ActionType.Double)
            {
                hand_doubled[active_hand] = true;
            }
            else if (best == ActionType.Surrender)
            {
                surrendered = true;
            }

            action_count++;

            return(best);
        }
        // seen_cards include active_hand's cards
        public override ActionType GetAction(CardSet seen_cards, Card dealer_upcard, CardSet[] player_hands, int active_hand, List<ActionType> available_actions)
        {
            List<ActionEv> actions = GetActions(seen_cards, dealer_upcard, player_hands, active_hand, available_actions);

            ActionType best = actions[0].Action;

            if (best == ActionType.Split)
            {
                split_count++;
            }
            else if (best == ActionType.Double)
            {
                hand_doubled[active_hand] = true;
            }
            else if (best == ActionType.Surrender)
            {
                surrendered = true;
            }

            action_count++;

            return best;
        }
Example #20
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            CardSet cards = (CardSet)obj;

            if (Count != cards.Count)
            {
                return(false);
            }

            for (int i = 0; i < Count; i++)
            {
                if (!this[i].Equals(cards[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #21
0
        public CardSet ExtractTop(int count)
        {
            CardSet set = new CardSet();

            for (int i = 0; i < count; i++)
            {
                if (card_set.Count == 0)
                    break;

                set.Add(ExtractTop());
            }

            return set;
        }
Example #22
0
 public abstract bool TakeInsurance(CardSet seen_cards);
        public override void RoundOver(CardSet seen_cards, CardSet dealer_hand, CardSet[] player_hands, long game_id, int roll_after)
        {
            Hand dealer = new Hand(dealer_hand);

            Hand[] player = new Hand[player_hands.Length];
            for (int i = 0; i < player.Length; i++)
            {
                player[i]         = new Hand(player_hands[i]);
                player[i].Doubled = hand_doubled[i];
            }

            int actual_money   = roll_after - roll_before;
            int expected_money = ExpectedMoney(dealer, player);

            if (game_id > 0 && game_id == last_game_id)
            {
                throw new Exception("game_id == last_game_id");
            }

            if (action_count == 0)
            {
                bool dealer_natural = dealer.IsNatural();
                bool player_natural = player.Count() == 1 && player[0].IsNatural();

                if (!dealer_natural && !player_natural)
                {
                    throw new Exception("No actions made and no BJ");
                }
                else
                {
                    if (actual_money == 0)
                    {
                        if (dealer_natural && insurance_taken)
                        {
                            // this is correct
                        }
                        else if (!(dealer_natural && player_natural))
                        {
                            throw new Exception("BJ but no roll change (and no push)");
                        }
                    }
                }
            }

            game_logger.Showdown(dealer_hand, player_hands, game_id, expected_money, actual_money);
            Console.WriteLine("Roll: " + roll_after);

            last_game_id = game_id;

            if (expected_money != actual_money)
            {
                if (Config.Current.GetBooleanProperty("ThrowMoneyException"))
                {
                    throw new MoneyMismatchException(expected_money, actual_money);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("EXPECTED MONEY MISMATCH!");
                    Console.WriteLine();
                }
            }

            shoe.Remove(seen_cards);

            if (shoe.FullCount - shoe.CardCount >= 84)
            {
                shoe.Reset();
            }

            Console.WriteLine("Seen cards: " + seen_cards);
            Console.WriteLine("Removed from shoe: {0} ({1})", shoe.FullCount - shoe.CardCount, seen_cards.Count);

            InitializeRound();
        }
Example #24
0
 public void Remove(CardSet cards)
 {
     foreach (Card c in cards)
     {
         Remove(c);
     }
 }
Example #25
0
 // seen_cards include active_hand's cards
 public abstract ActionType GetAction(CardSet seen_cards, Card dealer_upcard, CardSet[] player_hands, int active_hand, List<ActionType> available_actions);
        public override bool TakeInsurance(CardSet seen_cards)
        {
            Shoe tmp_shoe = shoe.Copy();
            tmp_shoe.Remove(seen_cards);

            double insurance_ev = Eval.InsuranceEv(current_bet, tmp_shoe.ToArray());

            if (insurance_ev >= 0.0)
            {
                game_logger.Insurance(true);
                insurance_taken = true;
                return true;
            }

            return false;
        }
        public override void RoundOver(CardSet seen_cards, CardSet dealer_hand, CardSet[] player_hands, long game_id, int roll_after)
        {
            Hand dealer = new Hand(dealer_hand);

            Hand[] player = new Hand[player_hands.Length];
            for (int i = 0; i < player.Length; i++)
            {
                player[i] = new Hand(player_hands[i]);
                player[i].Doubled = hand_doubled[i];
            }

            int actual_money = roll_after - roll_before;
            int expected_money = ExpectedMoney(dealer, player);

            if (game_id > 0 && game_id == last_game_id)
            {
                throw new Exception("game_id == last_game_id");
            }

            if (action_count == 0)
            {
                bool dealer_natural = dealer.IsNatural();
                bool player_natural = player.Count() == 1 && player[0].IsNatural();

                if (!dealer_natural && !player_natural)
                {
                    throw new Exception("No actions made and no BJ");
                }
                else
                {
                    if (actual_money == 0)
                    {
                        if (dealer_natural && insurance_taken)
                        {
                            // this is correct
                        }
                        else if (!(dealer_natural && player_natural))
                        {
                            throw new Exception("BJ but no roll change (and no push)");
                        }
                    }
                }
            }

            game_logger.Showdown(dealer_hand, player_hands, game_id, expected_money, actual_money);
            Console.WriteLine("Roll: " + roll_after);

            last_game_id = game_id;

            if (expected_money != actual_money)
            {
                if (Config.Current.GetBooleanProperty("ThrowMoneyException"))
                {
                    throw new MoneyMismatchException(expected_money, actual_money);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("EXPECTED MONEY MISMATCH!");
                    Console.WriteLine();
                }
            }

            shoe.Remove(seen_cards);

            if (shoe.FullCount - shoe.CardCount >= 84)
            {
                shoe.Reset();
            }

            Console.WriteLine("Seen cards: " + seen_cards);
            Console.WriteLine("Removed from shoe: {0} ({1})", shoe.FullCount - shoe.CardCount, seen_cards.Count);

            InitializeRound();
        }
Example #28
0
 // called when the round has ended
 public virtual void RoundOver(CardSet seen_cards, CardSet dealer_hand, CardSet[] player_hands, long game_id, int roll_after)
 {
 }
Example #29
0
 public abstract bool TakeInsurance(CardSet seen_cards);
Example #30
0
 // seen_cards include active_hand's cards
 public abstract ActionType GetAction(CardSet seen_cards, Card dealer_upcard, CardSet[] player_hands, int active_hand, List <ActionType> available_actions);
Example #31
0
 public void Remove(CardSet cards)
 {
     foreach (Card c in cards)
         if (card_set.Remove(c))
             RemoveCount(c);
 }
Example #32
0
 public CardSet(CardSet card_set)
 {
     this.card_set = new List<Card>(card_set.card_set);
     UpdateCount();
 }
        public void ValidateActions(CardSet active_hand, List<ActionType> available_actions)
        {
            if (!available_actions.Contains(ActionType.Stand)) throw new Exception("ValidateActions failed: no stand");
            if (!available_actions.Contains(ActionType.Hit)) throw new Exception("ValidateActions failed: no hit");

            if (active_hand.Count == 2)
            {
                if (!available_actions.Contains(ActionType.Double)) throw new Exception("ValidateActions failed: no double");

                if (active_hand[0].PointValue == active_hand[1].PointValue && split_count < max_splits)
                {
                    if (!available_actions.Contains(ActionType.Split)) throw new Exception("ValidateActions failed: no split");
                }

                if (split_count == 0)
                {
                    if (!available_actions.Contains(ActionType.Surrender)) throw new Exception("ValidateActions failed: no surrender");
                }
            }
        }
Example #34
0
        public static CardSet operator -(CardSet set1, CardSet set2)
        {
            CardSet tmp = new CardSet(set1);
            tmp.Remove(set2);

            return tmp;
        }
        private double GetActionEV(Shoe tmp_shoe, CardSet active_hand, ActionType action, Card dealer_upcard)
        {
            Hand hand = new Hand(active_hand);

            SHand shand;
            int soft_total = hand.SoftTotal();
            if (soft_total <= 21 && hand.HasAce())
            {
                shand.Total = soft_total;
                shand.Soft = true;
            }
            else
            {
                shand.Total = hand.HardTotal();
                shand.Soft = false;
            }

            int[] shoe_counts = tmp_shoe.ToArray();
            int upcard = dealer_upcard.PointValue;

            switch (action)
            {
                case ActionType.Stand:

                    return Eval.StandEv(shand, upcard, shoe_counts);

                case ActionType.Hit:

                    return Eval.HitEv(shand, upcard, shoe_counts);

                case ActionType.Double:

                    return Eval.DoubleEv(shand, upcard, current_bet, shoe_counts);

                case ActionType.Split:

                    return Eval.SplitEv(active_hand[0].PointValue, upcard, current_bet, max_splits - split_count, shoe_counts);

                case ActionType.Surrender:

                    return Eval.SurrenderEv();
            }

            return -1;
        }
Example #36
0
 public Hand()
 {
     cards = new CardSet();
     Reset();
 }
Example #37
0
 public void Add(CardSet cards)
 {
     foreach (Card c in cards)
     {
         Add(c);
     }
 }
Example #38
0
 public CardSet(CardSet card_set)
 {
     this.card_set = new List <Card>(card_set.card_set);
     UpdateCount();
 }
Example #39
0
        public object Clone()
        {
            CardSet cloned_set = new CardSet();

            foreach (Card card in this.card_set)
                cloned_set.Add((Card)(card.Clone()));

            return cloned_set;
        }
Example #40
0
 public Hand()
 {
     cards = new CardSet();
     Reset();
 }
Example #41
0
        public CardSet ExtractRandom(Random rand, int count)
        {
            CardSet set = new CardSet();

            for (int i = 0; i < count; i++)
            {
                if (card_set.Count == 0)
                    break;

                int card_index = rand.Next(card_set.Count);
                set.Add((Card)card_set[card_index]);
                RemoveCount(card_set[i]);
                card_set.RemoveAt(card_index);
            }

            return set;
        }
        public List<ActionEv> GetActions(CardSet seen_cards, Card dealer_upcard, CardSet[] player_hands, int active_hand, List<ActionType> available_actions)
        {
            ValidateActions(player_hands[active_hand], available_actions);

            Shoe tmp_shoe = shoe.Copy();
            tmp_shoe.Remove(seen_cards);

            Eval.CacheDealerProbs(dealer_upcard.PointValue, tmp_shoe.ToArray());

            List<ActionEv> actions = new List<ActionEv>();

            foreach (ActionType a in available_actions)
            {
                double ev = GetActionEV(tmp_shoe, player_hands[active_hand], a, dealer_upcard);

                actions.Add(new ActionEv() { Action = a, Ev = ev });
            }

            actions.Sort(delegate(ActionEv ae1, ActionEv ae2) { return ae2.Ev.CompareTo(ae1.Ev); });

            game_logger.Action(dealer_upcard, player_hands, active_hand, actions);

            return actions;
        }
Example #43
0
 // called when the round has ended
 public virtual void RoundOver(CardSet seen_cards, CardSet dealer_hand, CardSet[] player_hands, long game_id, int roll_after)
 {
 }