Пример #1
0
    /// <summary>
    /// reference for the combinations of cards, their values and names
    /// example of the use of this function return new CardMatch(player, "Two Pair", CardCombos.TwoPair, paires.Max(), paires.Min());
    /// </summary>
    /// <param name="playerID">the player</param>
    /// <param name="name">the name of the combo</param>
    /// <param name="combo">the combo itself</param>
    /// <param name="values">values, can be more than one as a player can have more than one pair or a flush and a pair</param>
    public CardMatch(GameObject playerID, string name, CardCombos combo, params int[] values )
    {
        this.name = name;
        this.combo = combo;
        //order the combos by value
        this.values = values.OrderByDescending(i => i).ToArray();
        this.player = playerID;

        if (values.Length == 0)
        {
            values = new int[] { -1 };
        }

    }
Пример #2
0
    public void CompareCards()
    {
        Debug.Log("comparing");

        //list of matches
        var matches = new List<CardMatch>();

        //cards from the community pile
        var community = GetCards(communityCards);

        //adds the cards from each player to the matches + the community cards - to see if there are any matches
        matches.AddRange(GetMatches(playerOneHand, community));
        matches.AddRange(GetMatches(playerTwoHand, community));

        // Sort so higher combos are at the top
        //matches.OrderByDescending(m => m.Combo);
        var combos = new CardCombos[]
        {
            CardCombos.RoyalFlush,
            CardCombos.StraightFlush,
            CardCombos.FourOfAKind,
            CardCombos.FullHouse,
            CardCombos.Flush,
            CardCombos.Straight,
            CardCombos.ThreeOfAKind,
            CardCombos.TwoPair,
            CardCombos.OnePair,
            CardCombos.HighestCard,
        };

        //we havent found a winner yet
        CardMatch winner = null;

        //runs through the different possible combos
        foreach (var c in combos)
        {
            //find all matches of the combos in the matches list
            var m = matches.FindAll(i => i.Combo == c);

            //if there are more than one match for a combo
            if (m.Count > 1)
            {
                Debug.Log("Multiple matches for " + c);

                //run through to see who´s combo is better
                for (int i = 0; i < m[0].Values.Length && i < m[1].Values.Length; i++)
                {
                    //if match at index 0 is better than that at index 1 or vice versa
                    if (m[0].Values[i] > m[1].Values[i])
                    {
                        winner = m[0];
                        break;
                    }
                    else if (m[1].Values[i] > m[0].Values[i])
                    {
                        winner = m[1];
                        break;
                    }
                }
                //if there is a winner step out of the loop
                if (winner != null)
                    break;
            }
            //if there is only one match
            else if (m.Count == 1)
            {
                Debug.Log("One matches for " + c);

                //there is only one!
                winner = m.First();
                break;
            }
            //there are no matches for the current combo
            else
            {
                //Debug.Log("No matches for " + c); // debug for matches
            }
        }

        //if there is a winner return the name and how they won
        if (winner != null)
        {
            Debug.LogFormat("The winner is {0} with {1}", winner.Player.name, winner.Name);//TODO: ADD chips to winner wallet

            if (winner.Player.name == "PlayerOneHand")
            {
                //player one has won
                winningPlayer = 1;
            }
            else if (winner.Player.name == "PlayerTwoHand")
            {
                //player two has won
                winningPlayer = 2;
            }
            else
            {
                throw new InvalidOperationException("No winner found"); // pot should be split evenly at this point
            }
            //PotManager.Instance.PotToPlayer(winningPlayer);
            WalletManager.Instance.ReceivePot(winningPlayer);
        }
        //otherwise there is no winner and we throw an exception - should very rarely happen
        else
        {
            throw new InvalidOperationException("No winner!!!");
        }
    }