Exemplo n.º 1
0
        public override PlayerAction Draw(Card[] hand)
        {
            // Sort the hand
            Evaluate.SortHand(hand);

            // Figure out what cards to replace
            bool[] cardsToDelete = GetCardsToDelete(hand);

            // How many cards to replace
            int numOfCardsToDelete = 0;

            for (int i = 0; i < cardsToDelete.Length; i++)
            {
                if (cardsToDelete[i])
                {
                    numOfCardsToDelete++;
                }
            }

            PlayerAction pa = null;

            if (numOfCardsToDelete > 0 && numOfCardsToDelete < 5)
            {
                for (int i = 0; i < cardsToDelete.Length; i++)
                {
                    if (cardsToDelete[i])
                    {
                        hand[i] = null;
                    }
                }

                pa = new PlayerAction(Name, "Draw", "draw", numOfCardsToDelete);
            }
            else if (numOfCardsToDelete == 5)
            {
                for (int i = 0; i < hand.Length; i++)
                {
                    hand[i] = null;
                }

                pa = new PlayerAction(Name, "Draw", "draw", 5);
            }
            else
            {
                pa = new PlayerAction(Name, "Draw", "stand pat", 0);
            }

            return(pa);
        }
Exemplo n.º 2
0
        public override PlayerAction BettingRound2(List <PlayerAction> actions, Card[] hand)
        {
            //order hand first
            Evaluate.SortHand(hand);

            // get rank of current hand and highest card
            Card highCard  = null;
            int  finalRank = Evaluate.RateAHand(hand, out highCard); // final hand rank

            //  Determine bet/bluff action -- KPAR, NPRO
            Boolean bluffing = determineBluff();

            // return appropriate PlayerAction object
            int    amountPlaced = 0;
            string actionTaken  = determinePlayerAction(finalRank, actions, bluffing, out amountPlaced);//the player action that will be taken for this turn

            return(new PlayerAction(Name, "Bet2", actionTaken, amountPlaced));
        }
Exemplo n.º 3
0
        // add the cards back into your hand after the Draw step
        public void AddCards(Card[] hand, Card[] newCards)
        {
            // fill in the empty spots
            for (int i = 0; i < newCards.Length; i++)
            {
                for (int j = 0; j < hand.Length; j++)
                {
                    if (hand[j] == null)       // empty spot
                    {
                        hand[j] = newCards[i]; // fill the spot
                        break;
                    }
                }
            }

            // sort the cards
            Evaluate.SortHand(hand);
        }
Exemplo n.º 4
0
        public override PlayerAction Draw(Card[] hand)
        {
            int handValue = Evaluate.RateAHand(hand, out highCard);                 // Gets the value of the hand we have

            Evaluate.SortHand(hand);                                                // Sorts hand

            if (handValue == 10 || handValue == 9 || handValue == 7 || handValue == 6 || handValue == 5)
            {
                Speak("I swap nothing, mortal");
                return(new PlayerAction(Name, "Draw", "stand pat", 0));
            }
            if (handValue == 8)
            {
                Speak("I swap one card");

                //find card that isn't part of 4 of a kind and remove it
                for (int i = 2; i < 15; i++)
                {
                    if (Evaluate.ValueCount(i, hand) == 4)
                    {
                        for (int j = 0; j < 5; j++)
                        {
                            if (hand[j].Value != i)
                            {
                                hand[j] = null;
                            }
                        }
                    }
                }

                return(new PlayerAction(Name, "Draw", "draw", 1));
            }
            if (handValue == 4)
            {
                Speak("I swap two cards");
                List <int> cardsToRemove = new List <int>();

                //find cards that aren't part of three of a kind and remove them
                for (int i = 2; i < 15; i++)
                {
                    if (Evaluate.ValueCount(i, hand) == 3)
                    {
                        for (int j = 0; j < 5; j++)
                        {
                            if (hand[j].Value != i)
                            {
                                cardsToRemove.Add(j);
                                //hand[j] = null;
                            }
                        }
                    }
                }

                //remove the ones we need to
                hand[cardsToRemove[0]] = null;
                hand[cardsToRemove[1]] = null;

                //request 2 cards
                return(new PlayerAction(Name, "Draw", "draw", 2));
            }
            if (handValue == 3)
            {
                Speak("I swap one card");

                //find card that isn't part of either pair and remove it
                // Get the first pair
                int firstPair = 0;
                for (int i = 2; i < 15; i++)
                {
                    if (Evaluate.ValueCount(i, hand) == 2)
                    {
                        firstPair = i;
                    }
                }

                // now get the second pair
                int secondPair = 0;
                for (int i = 2; i < 15; i++)
                {
                    if (i == firstPair)
                    {
                        continue;                 // skip this value
                    }
                    if (Evaluate.ValueCount(i, hand) == 2)
                    {
                        secondPair = i;
                    }
                }
                for (int i = 0; i < 5; i++)
                {
                    if (hand[i].Value != firstPair && hand[i].Value != secondPair)
                    {
                        hand[i] = null;
                    }
                }

                return(new PlayerAction(Name, "Draw", "draw", 1));
            }
            if (handValue == 2)
            {
                //find the value that is the pair
                int pair = 0;
                for (int i = 2; i < 15; i++)
                {
                    if (Evaluate.ValueCount(i, hand) == 2)
                    {
                        pair = i;
                    }
                }

                //find the cards that are not the pair and replace them
                for (int i = 0; i < 5; i++)
                {
                    if (hand[i].Value != pair)
                    {
                        hand[i] = null;
                    }
                }

                Speak("I swap three cards");
                return(new PlayerAction(Name, "Draw", "draw", 3));
            }
            if (handValue == 1)
            {
                Speak("I swap four cards");

                //Checks for 3 or more of same suit
                string flushSuit = "";
                int    suitCount = 0;
                for (int i = 0; i < hand.Length; i++)
                {
                    for (int j = 0; j < hand.Length; j++)
                    {
                        if (hand[i].Suit == hand[j].Suit && i != j)
                        {
                            suitCount++;
                        }
                    }
                    if (suitCount >= 3)
                    {
                        flushSuit = hand[i].Suit;
                        break;
                    }
                }
                if (flushSuit != "")
                {
                    int count = 0;
                    for (int i = 0; i < 5; i++)
                    {
                        if (hand[i].Suit != flushSuit)
                        {
                            hand[i] = null;
                            count++;
                        }
                    }
                    Console.WriteLine("requesting " + (5 - count) + " new cards");
                    return(new PlayerAction(Name, "Draw", "draw", count));
                }

                // checks for a hand that is almost a straight
                // ex: 4, 5, 7, 8, Ace| 4, 5, 6, 7, 10|
                // so Two in a row, then 1 missing, then Two more in a row
                // or Three in a row, 1 missing, then 1
                // or Four in a row
                if (hand[0].Value == hand[1].Value - 1 &&
                    hand[0].Value == hand[2].Value - 2 &&
                    hand[0].Value == hand[3].Value - 3 &&
                    hand[0].Value != hand[4].Value - 4)
                {
                    hand[4] = null;
                    return(new PlayerAction(Name, "Draw", "draw", 1));
                }
                else if (hand[0].Value == hand[1].Value - 1 &&
                         hand[0].Value == hand[2].Value - 2 &&
                         hand[0].Value != hand[3].Value - 3 &&
                         hand[0].Value == hand[4].Value - 4)
                {
                    hand[3] = null;
                    return(new PlayerAction(Name, "Draw", "draw", 1));
                }
                else if (hand[0].Value == hand[1].Value - 1 &&
                         hand[0].Value != hand[2].Value - 2 &&
                         hand[0].Value == hand[3].Value - 3 &&
                         hand[0].Value == hand[4].Value - 4)
                {
                    hand[2] = null;
                    return(new PlayerAction(Name, "Draw", "draw", 1));
                }
                else if (hand[0].Value != hand[1].Value - 1 &&
                         hand[0].Value == hand[2].Value - 2 &&
                         hand[0].Value == hand[3].Value - 3 &&
                         hand[0].Value == hand[4].Value - 4)
                {
                    hand[1] = null;
                    return(new PlayerAction(Name, "Draw", "draw", 1));
                }
                else if (hand[1].Value != hand[2].Value - 1 &&
                         hand[1].Value == hand[3].Value - 2 &&
                         hand[1].Value == hand[4].Value - 3 &&
                         hand[1].Value == hand[0].Value + 1)
                {
                    hand[0] = null;
                    return(new PlayerAction(Name, "Draw", "draw", 1));
                }


                else
                {
                    return(new PlayerAction(Name, "Draw", "draw", 4));
                }
            }
            else
            {
                return(new PlayerAction(Name, "Draw", "stand pat", 0));
            }
        }
Exemplo n.º 5
0
        // plays 1 round of poker
        private string Round()
        {
            string text = "";                                        // result text
            List <PlayerAction> actions = new List <PlayerAction>(); // list of actions

            // reset the pot
            if (pot % 2 == 0)  // even numbered pot
            {
                pot = anteAmt; // pot with antes only
            }
            else // odd pot
            {
                // in this case, the pot was not an even number
                // so there was 1 credit left over for the starting pot
                // In theory this should never happen.
                pot = anteAmt + 1;
            }
            // call players in order
            Player[] playerOrder = new Player[2];

            // note that playerOrder[1] always contains the dealer
            if (p0.Dealer == true)   // player 0 deals?
            {
                playerOrder[0] = p1; // p1 goes first
                playerOrder[1] = p0;
            }
            else
            {
                playerOrder[0] = p0; // p0 goes first
                playerOrder[1] = p1;
            }
            // setup deck for a new round
            deck.NewRound();

            // dealer deals out 5 cards to each player
            playerOrder[0].Hand = deck.Deal(5);
            playerOrder[1].Hand = deck.Deal(5);

            // round 1 of betting - loop until both players check,
            // one folds, or one calls
            ResultWriter("Betting round 1:");

            Boolean done = false; // flags when finished

            do
            {
                PlayerAction pa0   = playerOrder[0].BettingRound1(actions, playerOrder[0].Hand);
                bool         valid = CheckAction("Bet1", actions, pa0);
                if (valid == false)
                {
                    ResultWriter(playerOrder[0].Name + " played a bad action of " + pa0.ActionName + " and forfeits the hand");
                    pa0 = new PlayerAction(pa0.Name, pa0.ActionPhase, "fold", 0);
                }
                actions.Add(pa0);
                ResultWriter(pa0.ToString());
                ResultWriter(" ");

                // handle the case of the first player calling - the
                // second player must also call - do this automatically
                // and break out of the loop
                if (pa0.ActionName == "call")
                {
                    // add the second player's action automatically
                    PlayerAction pa1 = new PlayerAction(playerOrder[1].Name, "Bet1", "call", 0);
                    actions.Add(pa1);
                    break; // done betting
                }

                if (pa0.ActionName != "fold") // first player did not fold
                {
                    PlayerAction pa1 = playerOrder[1].BettingRound1(actions, playerOrder[1].Hand);
                    valid = CheckAction("Bet1", actions, pa1);
                    if (valid == false)
                    {
                        ResultWriter(playerOrder[1].Name + " played a bad action of " + pa1.ActionName + " and forfeits the hand");
                        pa1 = new PlayerAction(pa1.Name, pa1.ActionPhase, "fold", 0);
                    }
                    actions.Add(pa1);
                    ResultWriter(pa1.ToString());
                    ResultWriter(" ");
                }
                done = EvaluateActions(actions, "Bet1");
            } while (done == false);

            // update the pot based on the bets
            int lastBet = 0;

            for (int i = 0; i < actions.Count; i++)
            {
                if (actions[i].ActionPhase == "Bet1")
                {
                    switch (actions[i].ActionName)
                    {
                    case "bet":
                        lastBet = actions[i].Amount;
                        pot    += lastBet;                          // adjust the pot
                        // deduct from player
                        if (actions[i].Name == playerOrder[0].Name) // player0 bet?
                        {
                            playerOrder[0].ChangeMoney(-lastBet);
                        }
                        else     // must be player1
                        {
                            playerOrder[1].ChangeMoney(-lastBet);
                        }
                        break;

                    case "raise":
                        int total = lastBet;                        // amt from previous player
                        pot    += lastBet;                          // player raising must match last bet
                        lastBet = actions[i].Amount;
                        total  += lastBet;                          // amt being raised
                        pot    += lastBet;                          // plus the amount raised
                        // deduct from player
                        if (actions[i].Name == playerOrder[0].Name) // player0 bet?
                        {
                            playerOrder[0].ChangeMoney(-total);
                        }
                        else     // must be player1
                        {
                            playerOrder[1].ChangeMoney(-total);
                        }
                        break;

                    case "call":
                        // skip if this is a call after another call
                        if (i - 1 >= 0)
                        {
                            if (actions[i - 1].ActionName == "call")
                            {
                                break;
                            }
                        }
                        pot += lastBet;                             // match the last bet
                        // deduct from player
                        if (actions[i].Name == playerOrder[0].Name) // player0 bet?
                        {
                            playerOrder[0].ChangeMoney(-lastBet);
                        }
                        else     // must be player1
                        {
                            playerOrder[1].ChangeMoney(-lastBet);
                        }
                        break;
                    }
                }
            }

            ResultWriter("After Bet1, pot is " + pot);
            ResultWriter(" ");

            // see if someone folded
            if (actions[actions.Count - 1].ActionName == "fold" &&
                actions[actions.Count - 1].Name == playerOrder[1].Name)
            {
                // if the player in playerOrder[1] folded, other
                // player gets the pot
                playerOrder[0].ChangeMoney(pot);
                string result = actions[actions.Count - 1].Name + " folded. Other player gets the pot of " + pot;
                pot = 0;        // clear the pot
                return(result); // skip rest of loop
            }
            else if (actions[actions.Count - 1].ActionName == "fold" &&
                     actions[actions.Count - 1].Name == playerOrder[0].Name)
            {
                // if the player in playerOrder[1] folded, other
                // player gets the pot
                playerOrder[1].ChangeMoney(pot);
                string result = actions[actions.Count - 1].Name + " folded. Other player gets the pot of " + pot;
                pot = 0;        // clear the pot
                return(result); // skip rest of loop
            }

            // draw
            for (int i = 0; i < playerOrder.Length; i++)
            {
                PlayerAction pa = playerOrder[i].Draw(playerOrder[i].Hand);
                actions.Add(pa);
                if (pa.Amount > 0)
                {
                    Card[] newCards = deck.Deal(pa.Amount); // get cards
                    playerOrder[i].AddCards(playerOrder[i].Hand, newCards);
                }
                ResultWriter("Name: " + playerOrder[i].Name);
                string handList = Evaluate.ListHand(playerOrder[i].Hand);
                ResultWriter(handList);
                ResultWriter(" ");
            }

            // round 2 of betting- loop until both players check,
            // one folds, or one calls
            ResultWriter("Betting round 2:");
            done = false; // flags when finished
            do
            {
                PlayerAction pa0   = playerOrder[0].BettingRound2(actions, playerOrder[0].Hand);
                bool         valid = CheckAction("Bet2", actions, pa0);
                if (valid == false)
                {
                    ResultWriter(playerOrder[0].Name + " played a bad action of " + pa0.ActionName + " and forfeits the hand");
                    pa0 = new PlayerAction(pa0.Name, pa0.ActionPhase, "fold", 0);
                }
                actions.Add(pa0);
                ResultWriter(pa0.ToString());
                ResultWriter(" ");

                // handle the case of the first player calling - the
                // second player must also call - do this automatically
                // and break out of the loop
                if (pa0.ActionName == "call")
                {
                    // add the second player's action automatically
                    PlayerAction pa1 = new PlayerAction(playerOrder[1].Name, "Bet2", "call", 0);
                    actions.Add(pa1);
                    break; // done betting
                }

                if (pa0.ActionName != "fold") // first player did not fold
                {
                    PlayerAction pa1 = playerOrder[1].BettingRound2(actions, playerOrder[1].Hand);
                    valid = CheckAction("Bet2", actions, pa1);
                    if (valid == false)
                    {
                        ResultWriter(playerOrder[1].Name + " played a bad action of " + pa1.ActionName + " and forfeits the hand");
                        pa1 = new PlayerAction(pa1.Name, pa1.ActionPhase, "fold", 0);
                    }
                    actions.Add(pa1);
                    ResultWriter(pa1.ToString());
                    ResultWriter(" ");
                }
                done = EvaluateActions(actions, "Bet2");
            } while (done == false);

            // update the pot based on the bets
            lastBet = 0;
            for (int i = 0; i < actions.Count; i++)
            {
                if (actions[i].ActionPhase == "Bet2")
                {
                    switch (actions[i].ActionName)
                    {
                    case "bet":
                        lastBet = actions[i].Amount;
                        pot    += lastBet;                          // adjust the pot
                        // deduct from player
                        if (actions[i].Name == playerOrder[0].Name) // player0 bet?
                        {
                            playerOrder[0].ChangeMoney(-lastBet);
                        }
                        else     // must be player1
                        {
                            playerOrder[1].ChangeMoney(-lastBet);
                        }
                        break;

                    case "raise":
                        int total = lastBet;                        // amt from previous player
                        pot    += lastBet;                          // player raising must match last bet
                        lastBet = actions[i].Amount;
                        total  += lastBet;                          // amt being raised
                        pot    += lastBet;                          // plus the amount raised
                        // deduct from player
                        if (actions[i].Name == playerOrder[0].Name) // player0 bet?
                        {
                            playerOrder[0].ChangeMoney(-total);
                        }
                        else     // must be player1
                        {
                            playerOrder[1].ChangeMoney(-total);
                        }
                        break;

                    case "call":
                        // skip if this is a call after another call
                        if (i - 1 >= 0)
                        {
                            if (actions[i - 1].ActionName == "call")
                            {
                                break;
                            }
                        }
                        pot += lastBet;                             // match the last bet
                        // deduct from player
                        if (actions[i].Name == playerOrder[0].Name) // player0 bet?
                        {
                            playerOrder[0].ChangeMoney(-lastBet);
                        }
                        else     // must be player1
                        {
                            playerOrder[1].ChangeMoney(-lastBet);
                        }
                        break;
                    }
                }
            }

            ResultWriter("After Bet2, pot is " + pot);
            ResultWriter(" ");

            // see if someone folded
            if (actions[actions.Count - 1].ActionName == "fold" &&
                actions[actions.Count - 1].Name == playerOrder[1].Name)
            {
                // if the player in playerOrder[1] folded, other
                // player gets the pot
                playerOrder[0].ChangeMoney(pot);
                string result = actions[actions.Count - 1].Name + " folded. Other player gets the pot of " + pot;
                pot = 0;        // clear the pot
                return(result); // skip rest of loop
            }
            else if (actions[actions.Count - 1].ActionName == "fold" &&
                     actions[actions.Count - 1].Name == playerOrder[0].Name)
            {
                // if the player in playerOrder[1] folded, other
                // player gets the pot
                playerOrder[1].ChangeMoney(pot);
                string result = actions[actions.Count - 1].Name + " folded. Other player gets the pot of " + pot;
                pot = 0;        // clear the pot
                return(result); // skip rest of loop
            }

            // round resolution
            // see if there is a clear winner based on hand strength
            Card highCard = null;
            int  p0Rank   = Evaluate.RateAHand(playerOrder[0].Hand, out highCard);
            int  p1Rank   = Evaluate.RateAHand(playerOrder[1].Hand, out highCard);

            if (p0Rank > p1Rank)
            {
                text = playerOrder[0].Name + " has a better hand and wins " + pot;
                playerOrder[0].ChangeMoney(pot);
                pot = 0;
            }
            else if (p1Rank > p0Rank)
            {
                text = playerOrder[1].Name + " has a better hand and wins " + pot;
                playerOrder[1].ChangeMoney(pot);
                pot = 0;
            }
            else // same rank - needs further examination
            {
                // sort both hands
                Evaluate.SortHand(playerOrder[0].Hand);
                Card[] hand0 = playerOrder[0].Hand;
                Evaluate.SortHand(playerOrder[1].Hand);
                Card[] hand1 = playerOrder[1].Hand;

                switch (p0Rank)
                {
                case 1:     // high card
                    for (int i = 4; i >= 0; i--)
                    {
                        if (hand0[i].Value != hand1[i].Value)
                        {
                            if (hand0[i].Value > hand1[i].Value)
                            {
                                text = playerOrder[0].Name + " has a better hand and wins " + pot;
                                playerOrder[0].ChangeMoney(pot);
                                pot = 0;
                            }
                            else if (hand1[i].Value > hand0[i].Value)
                            {
                                text = playerOrder[1].Name + " has a better hand and wins " + pot;
                                playerOrder[1].ChangeMoney(pot);
                                pot = 0;
                            }
                        }
                    }

                    // could be a tie
                    if (pot != 0)
                    {
                        playerOrder[0].ChangeMoney(pot / 2);
                        playerOrder[1].ChangeMoney(pot / 2);
                        text = "Tie, each player gets " + pot / 2;
                        if (pot % 2 != 0)
                        {
                            pot = 1;
                        }
                        else
                        {
                            pot = 0;
                        }
                    }
                    break;

                case 2:     // one pair
                    // get the pair for playerOrder[0]
                    int p0Pair = 0;
                    for (int j = 14; j >= 2; j--)
                    {
                        int count = Evaluate.ValueCount(j, hand0);
                        if (count == 2)    // found the pair
                        {
                            p0Pair = j;
                            break;
                        }
                    }

                    // do  the same for the other hand
                    int p1Pair = 0;
                    for (int k = 14; k >= 2; k--)
                    {
                        int count = Evaluate.ValueCount(k, hand1);
                        if (count == 2)     // found the pair
                        {
                            p1Pair = k;
                            break;
                        }
                    }

                    // which is higher
                    if (p0Pair > p1Pair)    // playerOrder[0] wins
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                    }
                    else if (p1Pair > p0Pair)
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                    }
                    else
                    {       // need to see what the high
                            // card is aside from the pair
                            // get the cards that are not part of a pair from hand0
                        Card[] h0NotPair = new Card[3];
                        int    pos       = 0;
                        for (int i = 0; i < hand0.Length; i++)
                        {
                            if (hand0[i].Value != p0Pair)
                            {
                                h0NotPair[pos] = hand0[i];
                                pos++;
                            }
                        }

                        // do the same for the next hand
                        Card[] h1NotPair = new Card[3];
                        pos = 0;
                        for (int i = 0; i < hand1.Length; i++)
                        {
                            if (hand1[i].Value != p1Pair)
                            {
                                h1NotPair[pos] = hand1[i];
                                pos++;
                            }
                        }

                        // see if high card breakes the tie
                        for (int i = 2; i >= 0; i--)
                        {
                            if (h0NotPair[i].Value != h1NotPair[i].Value)
                            {
                                if (h0NotPair[i].Value > h1NotPair[i].Value)
                                {
                                    text = playerOrder[0].Name + " has a better hand and wins " + pot;
                                    playerOrder[0].ChangeMoney(pot);
                                    pot = 0;
                                }
                                else if (h1NotPair[i].Value > h0NotPair[i].Value)
                                {
                                    text = playerOrder[1].Name + " has a better hand and wins " + pot;
                                    playerOrder[1].ChangeMoney(pot);
                                    pot = 0;
                                }
                            }
                        }

                        // could be a tie
                        if (pot != 0)
                        {
                            playerOrder[0].ChangeMoney(pot / 2);
                            playerOrder[1].ChangeMoney(pot / 2);
                            text = "Tie, each player gets " + pot / 2;
                            if (pot % 2 != 0)
                            {
                                pot = 1;
                            }
                            else
                            {
                                pot = 0;
                            }
                        }
                    }
                    break;

                case 3:     // two pair
                    // get the two pair
                    int[] h0Pair = new int[2];
                    int[] h1Pair = new int[2];

                    // get hand0 pairs
                    int pCount = 0;
                    for (int i = 14; i >= 2; i--)
                    {
                        int count = Evaluate.ValueCount(i, hand0);
                        if (count == 2)     // found the pair
                        {
                            h0Pair[pCount] = i;
                            pCount++;
                        }
                    }

                    // get the hand1 pairs
                    pCount = 0;
                    for (int i = 14; i >= 2; i--)
                    {
                        int count = Evaluate.ValueCount(i, hand1);
                        if (count == 2)     // found the pair
                        {
                            h1Pair[pCount] = i;
                            pCount++;
                        }
                    }
                    // compare the pairs
                    if (h0Pair[0] > h1Pair[0])    // playerOrder[0] wins
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                    }
                    else if (h1Pair[0] > h0Pair[0])     // playerOrder[1] wins
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                        pot = 0;
                    }
                    else     // tie on the highest pair
                    {
                        // compare the second pair
                        if (h0Pair[1] > h1Pair[1])     // playerOrder[0] wins
                        {
                            text = playerOrder[0].Name + " has a better hand and wins " + pot;
                            playerOrder[0].ChangeMoney(pot);
                            pot = 0;
                        }
                        else if (h1Pair[0] > h0Pair[0])     // playerOrder[1] wins
                        {
                            text = playerOrder[1].Name + " has a better hand and wins " + pot;
                            playerOrder[1].ChangeMoney(pot);
                            pot = 0;
                        }
                        else     // tie on the highest pair
                        {
                            playerOrder[0].ChangeMoney(pot / 2);
                            playerOrder[1].ChangeMoney(pot / 2);
                            text = "Tie, each player gets " + pot / 2;
                            // tie overall
                            if (pot % 2 != 0)
                            {
                                pot = 1;
                            }
                            else
                            {
                                pot = 0;
                            }
                        }
                    }
                    break;

                case 4:     // three of a kind
                    // get the pair for playerOrder[0]
                    int p0Three = 0;
                    for (int j = 14; j >= 2; j--)
                    {
                        int count = Evaluate.ValueCount(j, hand0);
                        if (count == 3)     // found the pair
                        {
                            p0Three = j;
                            break;
                        }
                    }

                    // do  the same for the other hand
                    int p1Three = 0;
                    for (int k = 14; k >= 2; k--)
                    {
                        int count = Evaluate.ValueCount(k, hand1);
                        if (count == 3)     // found the three cards
                        {
                            p1Three = k;
                            break;
                        }
                    }

                    // which is higher - no possibility of a tie
                    if (p0Three > p1Three)     // playerOrder[0] wins
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                    }
                    else
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                    }
                    pot = 0;
                    break;

                case 5:     // straight
                    // compare the top card - if one is higher than the other, that
                    // player is the winner. Otherwise, there is a tie
                    if (hand0[0].Value > hand1[0].Value)
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                        pot = 0;
                    }
                    else if (hand1[0].Value > hand0[0].Value)
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                        pot = 0;
                    }
                    else     // tie
                    {
                        playerOrder[0].ChangeMoney(pot / 2);
                        playerOrder[1].ChangeMoney(pot / 2);
                        text = "Tie, each player gets " + pot / 2;
                        if (pot % 2 != 0)
                        {
                            pot = 1;
                        }
                        else
                        {
                            pot = 0;
                        }
                    }
                    break;

                case 6:     // flush
                    // locate the high cards and keep testing until you
                    // either have a tie or a winner
                    // tie flag
                    Boolean tie = true;
                    for (int i = 4; i >= 0; i--)
                    {
                        if (hand0[i].Value != hand1[i].Value)
                        {
                            // determine the winner
                            if (hand0[i].Value > hand1[i].Value)
                            {
                                text = playerOrder[0].Name + " has a better hand and wins " + pot;
                                playerOrder[0].ChangeMoney(pot);
                                pot = 0;
                            }
                            else
                            {
                                text = playerOrder[1].Name + " has a better hand and wins " + pot;
                                playerOrder[1].ChangeMoney(pot);
                                pot = 0;
                            }
                            // not a tie
                            tie = false;
                            break;     // exit loop
                        }
                    }
                    // handle a tie
                    if (tie == true)
                    {
                        playerOrder[0].ChangeMoney(pot / 2);
                        playerOrder[1].ChangeMoney(pot / 2);
                        text = "Tie, each player gets " + pot / 2;
                        if (pot % 2 != 0)
                        {
                            pot = 1;
                        }
                        else
                        {
                            pot = 0;
                        }
                    }
                    break;

                case 7:     // full house
                    // get the two pair
                    int h0FH = 0;
                    int h1FH = 0;

                    // get hand0 triple
                    for (int i = 14; i >= 2; i--)
                    {
                        int count = Evaluate.ValueCount(i, hand0);

                        if (count == 3)     // found the triple
                        {
                            h0FH = i;
                        }
                    }

                    // get the hand1 triple

                    for (int i = 14; i >= 2; i--)
                    {
                        int count = Evaluate.ValueCount(i, hand1);

                        if (count == 3)     // found the triple
                        {
                            h1FH = i;
                        }
                    }
                    // compare the triples
                    if (h0FH > h1FH)     // playerOrder[0] wins
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                    }
                    else     // playerOrder[1] wins
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                    }
                    pot = 0;
                    break;

                case 8:     // four of a kind
                    // get the pair for playerOrder[0]
                    int p0Four = 0;
                    for (int j = 14; j >= 2; j--)
                    {
                        int count = Evaluate.ValueCount(j, hand0);
                        if (count == 4)     // found the  4 cards
                        {
                            p0Four = j;
                            break;
                        }
                    }

                    // do  the same for the other hand
                    int p1Four = 0;
                    for (int k = 14; k >= 2; k--)
                    {
                        int count = Evaluate.ValueCount(k, hand1);
                        if (count == 4)     // found the pair
                        {
                            p1Four = k;
                            break;
                        }
                    }

                    // which is higher - no possible tie
                    if (p0Four > p1Four)     // playerOrder[0] wins
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                    }
                    else
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                    }
                    pot = 0;
                    break;

                case 9:     // straight flush
                    // compare the top card - if one is higher than the other, that
                    // player is the winner. Otherwise, there is a tie
                    if (hand0[4].Value > hand1[4].Value)
                    {
                        text = playerOrder[0].Name + " has a better hand and wins " + pot;
                        playerOrder[0].ChangeMoney(pot);
                        pot = 0;
                    }
                    else if (hand1[4].Value > hand0[4].Value)
                    {
                        text = playerOrder[1].Name + " has a better hand and wins " + pot;
                        playerOrder[1].ChangeMoney(pot);
                        pot = 0;
                    }
                    else     // tie
                    {
                        playerOrder[0].ChangeMoney(pot / 2);
                        playerOrder[1].ChangeMoney(pot / 2);
                        text = "Tie, each player gets " + pot / 2;
                        if (pot % 2 == 0)
                        {
                            pot = 1;
                        }
                        else
                        {
                            pot = 0;
                        }
                    }
                    break;

                case 10:     // royal flush
                    // automatic tie - split the pot
                    playerOrder[0].ChangeMoney(pot / 2);
                    playerOrder[1].ChangeMoney(pot / 2);
                    text = "Tie, each player gets " + pot / 2;
                    if (pot % 2 != 0)
                    {
                        pot = 1;
                    }
                    else
                    {
                        pot = 0;
                    }
                    break;
                }
            }

            // return results
            return(text);
        }
Exemplo n.º 6
0
        public override PlayerAction BettingRound1(List <PlayerAction> actions, Card[] hand)
        {
            //order hand first
            Evaluate.SortHand(hand);

            // convert hand to inner class
            refHand = ConvertHandToRefCards(hand);

            // get rank of current hand and highest card
            Card highCard    = null;
            int  currentRank = Evaluate.RateAHand(hand, out highCard);

            if (currentRank < 2) // check for at least a potential pair
            {
                currentRank = 2;
            }

            // based on current rank, loop through and evaluate all possible hand types and determine most likely
            PossibilityLevel bestChance = PossibilityLevel.Impossible;
            PossibilityLevel currentChance;

            for (int i = currentRank; i < 11; i++)
            {
                currentChance = CheckForHandOfRank(i, refHand); // get chance of current hand

                if (currentChance < bestChance)                 // if current chance is better than current best, reassign
                {
                    rankOfTargetHands.Clear();                  // clear old bests
                    bestChance = currentChance;                 // overwrite best
                    rankOfTargetHands.Add(i);                   // add new best hand by their rank
                }
                else if (currentChance == bestChance)           // if equally as good
                {
                    rankOfTargetHands.Add(i);                   // add both target ranks
                }
            }

            // loop through target hand indices determine which is most likely
            if (rankOfTargetHands.Count > 1)
            {
                var evaluateNumBest = -1;                     //best hand to have a chance of drawing
                foreach (int targetRank in rankOfTargetHands) // resolve conflict
                {
                    // determine which rank is more likely -- NPAR
                    // determine based on pot size/money total
                    int evaluateNum = targetRank;                       // how likely this hand is to draw
                    int numOfCard   = ChanceOfCardsNeeded(evaluateNum); //number of possible cards that could be drawn to get hand
                    for (int i = 0; i < (int)bestChance; i++)           //number that will be thrown away
                    {
                        if (numOfCard - i <= 0)                         //add extra chances for drawing correct card
                        {
                            evaluateNum *= numOfCard;
                        }
                        else
                        {//have to draw correct cards
                            evaluateNum *= (numOfCard - i);
                        }
                    }

                    // assign target rank
                    if (evaluateNum > evaluateNumBest)//there is a better chance of getting this hand
                    {
                        targetHandRank  = targetRank;
                        evaluateNumBest = evaluateNum;
                    }
                    else if (evaluateNum == evaluateNumBest && targetRank > targetHandRank)//same chance of happening and targetRank is better than target hand
                    {
                        targetHandRank = targetRank;
                    }
                }
            }
            else if (rankOfTargetHands.Count == 1)//only one in array, have to go for that one
            {
                targetHandRank = rankOfTargetHands[0];
            }
            else//catch if list is empty
            {
                targetHandRank = 0;
            }

            // get total amount of discards
            int discardingTotal = GetTotalDiscardsForHandRank(targetHandRank);

            // Determine bet/bluff action -- KPAR, NPRO
            Boolean bluffing = determineBluff();

            // return appropriate PlayerAction object
            int    amountPlaced = 0;
            string actionTaken  = determinePlayerAction(currentRank, actions, bluffing, out amountPlaced);//the player action that will be taken for this turn

            return(new PlayerAction(Name, "Bet1", actionTaken, amountPlaced));
        }