IsNatural() public method

public IsNatural ( ) : bool
return bool
Esempio n. 1
0
        private int ExpectedMoney(Hand dealer, Hand[] player)
        {
            int game_bet       = 100 * current_bet;
            int expected_money = 0;

            if (insurance_taken)
            {
                expected_money -= game_bet / 2;

                if (dealer.IsNatural())
                {
                    expected_money += 3 * game_bet / 2;
                }
            }

            if (surrendered)
            {
                expected_money -= game_bet / 2;
            }
            else
            {
                foreach (Hand hand in player)
                {
                    int hand_bet = hand.Doubled ? (game_bet * 2) : game_bet;

                    expected_money -= hand_bet;

                    if (dealer.IsNatural())
                    {
                        if (hand.IsNatural())
                        {
                            expected_money += hand_bet;
                        }
                    }
                    else if (split_count == 0 && hand.IsNatural())
                    {
                        expected_money += 2 * hand_bet + hand_bet / 2;
                    }
                    else if (dealer.IsBust())
                    {
                        if (!hand.IsBust())
                        {
                            expected_money += 2 * hand_bet;
                        }
                    }
                    else if (!hand.IsBust())
                    {
                        int player_total = hand.PointCount();
                        int dealer_total = dealer.PointCount();

                        if (player_total < dealer_total)
                        {
                            // do nothing, we lose our bet
                        }                         // we win, 2:1 payout
                        else if (dealer_total < player_total)
                        {
                            expected_money += 2 * hand_bet;
                        }                         // push
                        else
                        {
                            expected_money += hand_bet;
                        }
                    }
                }
            }

            return(expected_money);
        }
Esempio n. 2
0
        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();
        }
        private int ExpectedMoney(Hand dealer, Hand[] player)
        {
            int game_bet = 100 * current_bet;
            int expected_money = 0;

            if (insurance_taken)
            {
                expected_money -= game_bet / 2;

                if (dealer.IsNatural()) expected_money += 3 * game_bet / 2;
            }

            if (surrendered)
            {
                expected_money -= game_bet / 2;
            }
            else
            {
                foreach (Hand hand in player)
                {
                    int hand_bet = hand.Doubled ? (game_bet * 2) : game_bet;

                    expected_money -= hand_bet;

                    if (dealer.IsNatural())
                    {
                        if (hand.IsNatural()) expected_money += hand_bet;
                    }
                    else if (split_count == 0 && hand.IsNatural())
                    {
                        expected_money += 2 * hand_bet + hand_bet / 2;
                    }
                    else if (dealer.IsBust())
                    {
                        if (!hand.IsBust())
                        {
                            expected_money += 2 * hand_bet;
                        }
                    }
                    else if (!hand.IsBust())
                    {
                        int player_total = hand.PointCount();
                        int dealer_total = dealer.PointCount();

                        if (player_total < dealer_total)
                        {
                            // do nothing, we lose our bet
                        } // we win, 2:1 payout
                        else if (dealer_total < player_total)
                        {
                            expected_money += 2 * hand_bet;
                        } // push
                        else
                        {
                            expected_money += hand_bet;
                        }
                    }
                }
            }

            return expected_money;
        }
Esempio n. 4
0
        public void DealRound()
        {
            player_handset.ActiveHand.AddCard(shoe.ExtractTop());
            dealer_hand.AddCard(shoe.ExtractTop());
            player_handset.ActiveHand.AddCard(shoe.ExtractTop());
            dealer_hand.AddCard(shoe.ExtractTop());


            bool dealer_natural;

            dealer_natural = dealer_hand.IsNatural();

            // check if player has blackjack
            if (player_handset.ActiveHand.IsNatural())
            {
                if (dealer_natural)
                {
                    // push, return initial bet
                    player_money += bet;
                    EndRound();
                    return;
                }
                else
                {
                    // player wins with blackjack, pay out 1.5:1 (with 10$ at stake, return 15$)
                    player_money += (int)(1.5 * bet) + bet;
                    EndRound();
                    return;
                }
            }

            int insurance = 0;

            // offer insurance
            if (Upcard().IsAce())
            {
                if (agent.TakeInsurance(this))
                {
                    insurance     = (int)(0.5 * bet);
                    player_money -= insurance;
                    AddPartyPoints(insurance);
                }
            }

            // dealer has blackjack
            if (dealer_natural)
            {
                if (insurance > 0)
                {
                    //??????
                    // insurance bet pays 2:1, initial bet is lost
                    player_money += (2 * insurance) + insurance;
                    EndRound();
                    return;
                }
                else
                {
                    // dealer wins with blackjack
                    EndRound();
                    return;
                }
            }

            // main loop
            // keeps asking player for actions for unfinished hands until
            // no unfinished hands remains
            while (!player_handset.AllFinished())
            {
                ActionType action = agent.GetBestAction(this);

                if (!IsValidAction(action))
                {
                    Console.WriteLine("Player made invalid action!");
                    return;
                }

                // player decides to stand, hand is over, move to next one
                if (ActionType.Stand == action)
                {
                    player_handset.ActiveHand.Stand();
                }
                else if (ActionType.Hit == action)
                {
                    player_handset.ActiveHand.Hit(shoe.ExtractTop());
                }
                else if (ActionType.Surrender == action)
                {
                    player_handset.ActiveHand.Surrender();
                }
                else if (ActionType.Double == action)
                {
                    player_handset.ActiveHand.Double(shoe.ExtractTop());
                    player_money -= bet;
                    AddPartyPoints(bet);
                }
                else if (ActionType.Split == action)
                {
                    player_handset.Split(shoe.ExtractTop(), shoe.ExtractTop());
                    player_money -= bet;
                    AddPartyPoints(bet);
                    split_count++;
                }
                else
                {
                    Console.WriteLine("Unknown action");
                }

                player_handset.NextActiveHand();
            }

            // each hand should be here in either some of these states:
            // busted, standed, doubled, surrendered

            // if all hands are busted, dealer doesn't draw, only reveals his hand and the round is over
            if (player_handset.AllBusted())
            {
                EndRound();
                return;
            }

            // player surrendered
            if (player_handset.ActiveHand.Surrendered)
            {
                // return half of the initial bet
                player_money += (int)(0.5 * bet);
                EndRound();
                return;
            }

            // handle the dealer logic
            while (true)
            {
                // must hit on soft 17's
                if (dealer_hand.HasAce() && dealer_hand.SoftTotal() == 17)
                {
                    dealer_hand.AddCard(shoe.ExtractTop());
                    continue;
                }

                // finish condition
                if (dealer_hand.PointCount() >= 17)
                {
                    break;
                }

                dealer_hand.AddCard(shoe.ExtractTop());
            }

            // payouts
            foreach (Hand hand in player_handset)
            {
                // skip split hands
                if (hand.IsSplit())
                {
                    continue;
                }

                int hand_bet = (hand.Doubled) ? (bet * 2) : bet;

                if (dealer_hand.IsBust())
                {
                    /*
                     * // both busted, it's a push
                     * if (hand.IsBust())
                     *      player_money += hand_bet;
                     * else // not busted hand wins dealer's busted hand 2:1 payout
                     *      player_money += 2 * hand_bet;
                     */

                    if (!hand.IsBust())
                    {
                        player_money += 2 * hand_bet;
                    }
                }
                else
                {
                    // do nothing, we lose our bet
                    if (hand.IsBust())
                    {
                    }
                    else
                    {
                        int player_total = hand.PointCount();
                        int dealer_total = dealer_hand.PointCount();

                        if (player_total < dealer_total)
                        {
                            // do nothing, we lose our bet
                        }                         // we win, 2:1 payout
                        else if (dealer_total < player_total)
                        {
                            player_money += 2 * hand_bet;
                        }                         // push
                        else
                        {
                            player_money += hand_bet;
                        }
                    }
                }
            }

            // round is over
            EndRound();
        }
        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();
        }