How to find how many times the same object appears in the list, and then find the property of the object that appears

I am doing a poker evaluator in hand for some programming practice that I am sending in exchange for codereview stacks. I need to be able to correctly compare hands, and for this I need to see the value of the pairs. My current control arm for a class of couples

 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 used linq to get a list of the most popular pairs, however I'm not sure how to finish it to get the face value of the card as soon as I separated them.

This is my current comparison 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;

, , , , , , (, )

, https://codereview.stackexchange.com/questions/152857/beginnings-of-a-poker-hand-classifier-part-2?noredirect=1&lq=1

+4
1

, , , , object, int s, . : , #.

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

: , , .

:

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

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

faceCount :

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);

, Take(2) , .

switch faceCount[0].Count faceCount[0].FaceValue .

+1

Source: https://habr.com/ru/post/1667356/


All Articles