简体   繁体   中英

How to find how many times the same object appears in a list and then find a property of the most appeared object

I'm making a poker hand evaluator for some programming practice which i have been submitting to codereview stack exchange. I need to be able to compare hands properly and for this I need to see the value of pairs. My Current check hand for pairs class

 private static PokerHandsRank CheckHandForPairs(Hand hand)

    {
        var faceCount = (from card in hand.Cards
                         group card by card.Face
                         into g
                         let count = g.Count()
                         orderby count descending
                         select count).Take(2).ToList(); // take two to check if multiple pairs of pairs, if second in list is 1 there will be two pairs

        switch (faceCount[0])
        {
            case 1: return PokerHandsRank.HighCard;
            case 2: return faceCount[1] == 1 ? PokerHandsRank.Pair : PokerHandsRank.TwoPair;
            case 3: return faceCount[1] == 1 ? PokerHandsRank.ThreeOfKind : PokerHandsRank.FullHouse;
            case 4: return PokerHandsRank.FourOfKind;
            default: throw new Exception("something went wrong here");
        }
    }

As you can see I've used linq to get a list of the most appeared pairing however I'm not sure how to finish it off to get the face value of the card once I've separated them.

This is my current compareto method

 public int CompareTo(Hand other)
    {
        if (HandRank == other.HandRank) //if the hand rank is equal, sort the cards by face value and compare the two biggest
        {
            sortHandbyFace(this); // sorts cards into order by face
            sortHandbyFace(other);
            for (int i = 4; 0 <= i; i--)
            {
                if (Cards[i].Face == other.Cards[i].Face)
                {
                    if (i == 0) return 0;
                    continue;
                }
                return Cards[i].Face > other.Cards[i].Face ? 1 : -1;
            }              
        }
        return HandRank > other.HandRank ? 1 : -1;

the compare to works perfect for comparing high card issues however i need to add a check if the two hand ranks are equal and then check if their value is a pair, two pair, fullhouse or three of kind (then to find the highest face value of the pair)

If you need more information on my program feel free to check my codereview post https://codereview.stackexchange.com/questions/152857/beginnings-of-a-poker-hand-classifier-part-2?noredirect=1&lq=1

This probably isn't exactly what you're looking for, since you're comparing object s and not just int s, but this could help you get started. Based on this question here: How to get frequency of elements stored in a list in C# .

using System.Linq;

List<int> ids = //
int maxFrequency = 0;
int IDOfMax = 0;

foreach(var grp in ids.GroupBy(i => i))
{
    if (grp.Count() > maxFrequency) 
    {
        maxFrequency = grp.Count();
        IDOfMax = grp.Key;
    }
}

// The object (int in this case) that appears most frequently 
// can be identified with grp.key

Update: after rereading the question , it sounds like you need to try to return a new object with the count and the face value from your query.

You can do this like so:

public class FaceCountResult
{
    public int Count { get; set; }
    public Face FaceValue { get; set; }

    public FaceCountResult(int count, Face faceValue)
    {
        Count = count;
        FaceValue = faceValue;
    }
}

Then, faceCount should look something like this:

var faceCount = (from card in hand.Cards
                 group card by card.Face
                 into g
                 let count = g.Count()
                 orderby count descending
                 select new FaceCountResult(count, card.Face);

I'm not sure how the Take(2) part would factor into this, as I don't quite understand that part of the code.

You can then do a switch on faceCount[0].Count and use faceCount[0].FaceValue to get the face value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM