//  Used by PlayTimedGame to process the players winnings
        public static void processPlayerWinnings(BSPlayer player, BSDealer dealer)
        {
            //  If this player is playing (do they have cards?)
            //  cant use "hasQuit", because that's set in the previous loop, and the player may still need to be processed
            if (player.getHand(0).Count > 0)
            {
                //  Loop through each hand the player has (Split Hands)
                for (int j = 0; j < player.getHandCount(); j++)
                {
                    BlackjackResult bjr = BlackJack.getGameResult(dealer.getHand(), player.getHand(j));

                    //  Give player the money / take it away
                    if (bjr == BlackjackResult.PlayerWins)
                    {
                        //  If the player wins, has only one hand and has blackjack then pay 3:2 instead of 1:1
                        if ((player.getHandCount() == 1) & (BlackJack.doesHandHaveBlackjack(player.getHand(j))))
                        {
                            player.winBlackJack(j);
                        }
                        else
                        {
                            player.winNormalBet(j);
                        }
                    }
                    else if (bjr == BlackjackResult.Push)
                    {
                        player.pushBet(j);
                    }
                    else // Dealer Wins
                    {
                        player.loseBet(j);
                    }
                }
            }
        }
 //  Used to display text for debugging the end of the game
 private void showGameEnd(List <BSPlayer> players, BSDealer dealer)
 {
     //  Loop through each player
     for (int i = 0; i < players.Count; i++)
     {
         //  Code for debgging the playHand function
         for (int x = 0; x < players[i].getHandCount(); x++)
         {
             if (!players[i].hasQuit)
             {
                 if (i == blackJackGameParams.tablePosition)
                 {
                     System.Diagnostics.Debug.WriteLine("Player Index: " + i.ToString());
                     System.Diagnostics.Debug.WriteLine("Bankroll: $" + players[i].getBankroll());
                     System.Diagnostics.Debug.WriteLine("Initial Bet: " + players[i].getInitialBet());
                     System.Diagnostics.Debug.WriteLine("List Index: " + x.ToString());
                     System.Diagnostics.Debug.Write("Dealer: ");
                     foreach (Card c in dealer.getHand())
                     {
                         System.Diagnostics.Debug.Write(c.value.ToString() + ", ");
                     }
                     System.Diagnostics.Debug.Write("\nCards: ");
                     foreach (Card c in players[i].getHand(x))
                     {
                         System.Diagnostics.Debug.Write(c.value.ToString() + ", ");
                     }
                     System.Diagnostics.Debug.WriteLine("\nWinner: " + BlackJack.getGameResult(dealer.getHand(), players[i].getHand(x)).ToString());
                     System.Diagnostics.Debug.WriteLine("Winnings: $" + players[i].getHandWinnings(x).ToString());
                     System.Diagnostics.Debug.WriteLine("Total: " + BlackJack.getHandValue(players[i].getHand(x)).ToString() + "\n");
                 }
             }
         }
     }
 }
        //  Initializes the class with the game parameters
        public BasicStrategySimulator(BlackJackGameParams blackJackGameParams)
        {
            this.blackJackGameParams = blackJackGameParams;
            this.cardCounter         = new CardCounting(blackJackGameParams);
            deck    = new Deck(blackJackGameParams.numDecks, Deck.ShuffleType.Durstenfeld, cardCounter);
            players = new List <BSPlayer>();
            dealer  = new BSDealer(blackJackGameParams.H17, deck);

            //  Initialize the players
            for (int i = 0; i < blackJackGameParams.numPlayers; i++)
            {
                BSPlayer newBSPlayer = new BSPlayer(blackJackGameParams);
                players.Add(newBSPlayer);
            }
        }
Exemple #4
0
        public void playHand(BSDealer dealer, CardCounting cardCounter)
        {
            //  We know the dealer doesn't have blackjack because this is tested before this portion of the game even occurs

            Stack <Card> splitCards         = new Stack <Card>(); //  Make a stack for holding the split cards
            int          processesListCount = 0;                  //  Keeps track of how many lists have been processed (Also reports the current lists index

            //  While there are cards in the split pile, or their are unprocessed lists...
            while ((splitCards.Count > 0) | (processesListCount < this.cardList.Count))
            {
                //  If we have the same number of lists as we have processed, we must have to handle another split
                if (this.cardList.Count == processesListCount)
                {
                    //  Add the split card to the new list
                    this.cardList.Add(new List <Card>());

                    //  Add a new wager to the new hand
                    this.currentBet.Add(this.initialBet);
                    this.playerWin.Add(0);

                    //  Add the card to the new hand
                    this.cardList[processesListCount].Add(splitCards.Pop());
                }

                //  Check if the hand has at least two cards, if it does'nt, then add one.  This happens when we split
                if (this.cardList[processesListCount].Count == 1)
                {
                    this.cardList[processesListCount].Add(dealer.takeHit());
                }

                //  Check if the higher, and lower values are both busted.  If so then we've busted this hand so move to the next.
                if ((BlackJack.getHandValue_Lower(this.cardList[processesListCount]) > 21) & (BlackJack.getHandValue_Lower(this.cardList[processesListCount]) > 21))
                {
                    processesListCount += 1;
                }
                else
                {
                    //  Get the Basic Strategy move for the current card list
                    BlackJackMove bjm = new BlackJackMove();

                    //  If the player has doubles, but not enough money to split, then find an alternative move
                    if (hasDoublesInHand(processesListCount) & hasQuit)
                    {
                        bjm = bsc.GetStrategyPairToNoPair(this.cardList[processesListCount], dealer.getUpCard());
                    }
                    else
                    {
                        bjm = bsc.GetStrategy(this.cardList[processesListCount], dealer.getUpCard());
                    }
                    switch (bjm)
                    {
                    case BlackJackMove.Stay:
                        processesListCount += 1;
                        break;

                    case BlackJackMove.Hit:
                        this.cardList[processesListCount].Add(dealer.takeHit());     //  Add a card but don't finish processing this hand
                        break;

                    case BlackJackMove.Double:
                        if (this.cardList[processesListCount].Count == 2)
                        {
                            //  If we have enough money to double then do it, otherwise leave the table after the end of the round
                            if ((BankRoll - currentBet[processesListCount]) > 0)
                            {
                                this.cardList[processesListCount].Add(dealer.takeHit());
                                this.BankRoll -= this.currentBet[processesListCount];
                                this.currentBet[processesListCount] = 2 * this.currentBet[processesListCount];
                                processesListCount += 1;
                                break;
                            }
                            else
                            {
                                //  Take a hit if we can't double due to money, and mark this as our last round to minimize losses
                                hasQuit = true;
                                this.cardList[processesListCount].Add(dealer.takeHit());
                                break;
                            }
                        }
                        else
                        {
                            this.cardList[processesListCount].Add(dealer.takeHit());     //  Add a card but don't finish processing this hand
                            break;
                        }

                    case BlackJackMove.Double_Stay:
                        //  If we haven't hit this hand yet then double, otherwise stay
                        if (this.cardList[processesListCount].Count == 2)
                        {
                            //  If we have enough money to double then do it, otherwise leave the table.
                            if ((BankRoll - currentBet[processesListCount]) > 0)
                            {
                                this.cardList[processesListCount].Add(dealer.takeHit());
                                this.BankRoll -= this.currentBet[processesListCount];
                                this.currentBet[processesListCount] = 2 * this.currentBet[processesListCount];
                                processesListCount += 1;
                                break;
                            }
                            else
                            {
                                //  Stay if we can't double due to money, and mark this as our last round to minimize losses
                                hasQuit             = true;
                                processesListCount += 1;
                                break;
                            }
                        }
                        else
                        {
                            processesListCount += 1;
                            break;
                        }

                    case BlackJackMove.Split:
                        //  Put the card from index 0 of the current hand into the stack.
                        //  Now, don't increment the processListCount, and reprocesses this hand.
                        //  It will only have one card so we will have to add that first.
                        if (((BankRoll - this.initialBet) > 0) & (cardList.Count < 4))
                        {
                            BankRoll -= this.initialBet;
                            splitCards.Push(this.cardList[processesListCount][1]);
                            this.cardList[processesListCount].RemoveAt(1);
                            break;
                        }
                        else
                        {
                            //  If we can't split due to money, then  re-evaluate as a hard/soft hand and determine the new best move, mark
                            //  as the last turn to minimize losses
                            hasQuit = true;
                            break;
                        }
                    }
                }
            }
            //  Even odds paid on split card blackjack
        }
        public void test_BSPlayer_playHand(BlackJackGameParams blackJackGameParams, string filePath)
        {
            var lines = File.ReadAllLines(filePath).Select(a => a.Split(',', ':'));
            IEnumerator <string[]> iter = lines.GetEnumerator();

            while (1 == 1)
            {
                int         itemCount   = 0;
                List <Card> deckList    = new List <Card>();
                List <Card> playersHand = new List <Card>();
                List <Card> dealersHand = new List <Card>();
                Double      initialBet  = 0;
                Double      winnings    = 0;
                string      name        = "";
                while (iter.MoveNext() & (itemCount < 5))
                {
                    var l = iter.Current;
                    switch (l[0])
                    {
                    case "Name":
                        name = l[1];
                        break;

                    case "Player":
                        for (int i = 1; i < l.Length; i++)
                        {
                            l[i].Replace(" ", "");
                            playersHand.Add(getCardFromText(l[i]));
                        }
                        itemCount += 1;
                        break;

                    case "Dealer":
                        for (int i = 1; i < l.Length; i++)
                        {
                            l[i].Replace(" ", "");
                            dealersHand.Add(getCardFromText(l[i]));
                        }
                        itemCount += 1;
                        break;

                    case "Deck":
                        for (int i = 1; i < l.Length; i++)
                        {
                            l[i].Replace(" ", "");
                            deckList.Add(getCardFromText(l[i]));
                        }
                        itemCount += 1;
                        break;

                    case "Initial":
                        l[1].Replace(" ", "");
                        initialBet = Double.Parse(l[1]);
                        itemCount += 1;
                        break;

                    case "Win":
                        l[1].Replace(" ", "");
                        winnings   = Double.Parse(l[1]);
                        itemCount += 1;
                        break;

                    default:
                        break;
                    }
                }

                //  When the iterator has run out of new items then return
                if (!(itemCount == 5))
                {
                    return;
                }
                BSPlayer  player = new BSPlayer(blackJackGameParams);
                DeckDummy deck   = new DeckDummy();
                BSDealer  dealer = new BSDealer(false, deck);

                deck.setCardList(deckList);

                dealer.setHand(dealersHand);

                player.clearCurrentBets();
                player.clearHand();
                player.setBankRoll(blackJackGameParams.bankroll);
                player.setInitialBet(initialBet);
                player.setFirstHand(playersHand);

                player.playHand(dealer, new CardCounting(blackJackGameParams));
                BasicStrategySimulator.processPlayerWinnings(player, dealer);
                if (player.getTotalHandWinnings() == winnings)
                {
                    System.Diagnostics.Debug.WriteLine(name + ": PASS");
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(name + ": FAIL");
                }
            }
        }