예제 #1
0
        private void UpdateScore(CardHand hand, string playerId)
        {
            int sub_points = 0;
            int ace_count  = 0;

            Card[]          theCards = new Card[hand.GetNumCardsInHand()];
            BlackJackData[] theAces  = new BlackJackData[4];
            BlackJackData[] theHand  = new BlackJackData[hand.GetNumCardsInHand()];
            for (int i = 0; i < hand.GetNumCardsInHand(); i++)
            {
                theCards[i] = hand.GetCard(i);
                bjData      = new BlackJackData();
                bjData.AddCardData(theCards[i], 10);
                theHand[i] = bjData;
            }
            var sorted = theHand.OrderBy(i => i.m_value).ToArray <BlackJackData>();

            if (theCards.Length >= 2)
            {
                foreach (BlackJackData c in sorted)
                {
                    if (c.m_id != 1)
                    {
                        sub_points += c.m_value;
                    }
                    else
                    {
                        theAces[ace_count] = c;
                        ++ace_count;
                    }
                }
                if (ace_count > 0)
                {
                    while (ace_count > 0)
                    {
                        if (sub_points + 11 > 21)
                        {
                            if (theAces[ace_count - 1].m_id == 1)
                            {
                                sub_points += 1;
                            }
                        }
                        else
                        {
                            sub_points += 11;
                        }
                        --ace_count;
                    }
                }
            }
            if (playerId == "p1")
            {
                scoreP1 = sub_points;
            }
            else    //"p2"
            {
                scoreP2 = sub_points;
            }
        }
예제 #2
0
        private void StartGame()
        {
            scoreP1 = scoreP2 = 0;
            m_rootZ = 0;

            firstHand      = true;
            showP2Score    = false;
            isAskingToDraw = true;
            p1HasDrawn     = false;
            cpuHasStopped  = false;
            winner         = "";

            p1Hand = new CardHand();
            p2Hand = new CardHand();

            m_dataList = new BlackJackDataList();
            bjData     = new BlackJackData();

            // prepare the deck
            m_deck.RemoveAllCards();

            m_allCards = m_allCards_cpy.ToList();
            m_random   = new Random();

            // Source deck
            for (int i = 0; i < m_allCards.Count; i++)
            {
                m_deck.AddCard(GetRandomCard());
                i--;
            }

            // the card under the pile
            m_deck.GetCard(0).setTurned(false);

            m_dataList.AddDeckData(m_deck);

            Card drawn;

            // draw the first 2 cards
            for (int i = 0; i < 2; i++)
            {
                drawn = m_deck.RemoveLast();
                drawn.setTurned(true);
                p1Hand.AddCard(drawn);
                //System.Diagnostics.Debug.WriteLine("Giocatore 1 ha pescato : " + drawn.CardId() + " " + drawn.Suit());

                drawn = m_deck.RemoveLast();
                drawn.setTurned((i % 2 == 0) ? true : false);
                p2Hand.AddCard(drawn);
                //System.Diagnostics.Debug.WriteLine("Avversario ha pescato : " + drawn.CardId() + " " + drawn.Suit());
            }
        }