Esempio n. 1
0
        public HandGrouping(Card[] cards)
        {
            Guards.ArgumentNotNull(cards, nameof(cards));
            Guards.ArgumentSuccess(cards.Length >= 5 && cards.Length <= 7, nameof(cards), "Only 5-7 cards can be grouped");

            var groups = new Dictionary <Face, List <Card> >();
            var unique = new HashSet <Card>();

            foreach (var card in cards)
            {
                if (!card.IsValid)
                {
                    throw new ArgumentException("Invalid cards cannot be grouped", nameof(cards));
                }
                if (unique.Contains(card))
                {
                    throw new ArgumentException("Duplicate cards cannot be grouped", nameof(cards));
                }
                unique.Add(card);

                if (!groups.ContainsKey(card.Face))
                {
                    groups.Add(card.Face, new List <Card>());
                }

                groups[card.Face].Add(card);
            }

            FourOfAKind  = new List <List <Card> >();
            ThreeOfAKind = new List <List <Card> >();
            Pair         = new List <List <Card> >();
            Single       = new List <List <Card> >();

            foreach (var face in groups.Keys)
            {
                var list = groups[face];
                if (list.Count == 4)
                {
                    FourOfAKind.Add(list);
                }
                else if (list.Count == 3)
                {
                    ThreeOfAKind.Add(list);
                }
                else if (list.Count == 2)
                {
                    Pair.Add(list);
                }
                else if (list.Count == 1)
                {
                    Single.Add(list);
                }
                else
                {
                    throw new InvalidOperationException("Impossible grouping");
                }
            }
        }
Esempio n. 2
0
        public Table(Deck deck, TableStatus status, byte[] community)
        {
            Guards.ArgumentNotNull(deck, nameof(deck));
            Guards.ArgumentSuccess(status.IsValid(), nameof(status));
            Guards.ArgumentHasExactSize(community, 5, nameof(community));

            this.Deck   = deck;
            this.Status = status;
            this.First  = community[0] == 0 ? (Card?)null : new Card(community[0]);
            this.Second = community[1] == 0 ? (Card?)null : new Card(community[1]);
            this.Third  = community[2] == 0 ? (Card?)null : new Card(community[2]);
            this.Turn   = community[3] == 0 ? (Card?)null : new Card(community[3]);
            this.River  = community[4] == 0 ? (Card?)null : new Card(community[4]);

            var duplicate = community.Where(_ => _ != 0).GroupBy(_ => _).Any(_ => _.Count() > 1);

            Guards.ArgumentSuccess(!duplicate, nameof(community), "Duplicate card found");

            var mismatch = $"Community cards don't match table status: {status}, ";

            switch (status)
            {
            case TableStatus.BeforeFlop:
                Guards.ArgumentSuccess(First == null, nameof(community), mismatch + "First");
                Guards.ArgumentSuccess(Second == null, nameof(community), mismatch + "Second");
                Guards.ArgumentSuccess(Third == null, nameof(community), mismatch + "Third");
                Guards.ArgumentSuccess(Turn == null, nameof(community), mismatch + "Turn");
                Guards.ArgumentSuccess(River == null, nameof(community), mismatch + "River");
                break;

            case TableStatus.BeforeTurn:
                Guards.ArgumentSuccess(First != null, nameof(community), mismatch + "First");
                Guards.ArgumentSuccess(Second != null, nameof(community), mismatch + "Second");
                Guards.ArgumentSuccess(Third != null, nameof(community), mismatch + "Third");
                Guards.ArgumentSuccess(Turn == null, nameof(community), mismatch + "Turn");
                Guards.ArgumentSuccess(River == null, nameof(community), mismatch + "River");
                break;

            case TableStatus.BeforeRiver:
                Guards.ArgumentSuccess(First != null, nameof(community), mismatch + "First");
                Guards.ArgumentSuccess(Second != null, nameof(community), mismatch + "Second");
                Guards.ArgumentSuccess(Third != null, nameof(community), mismatch + "Third");
                Guards.ArgumentSuccess(Turn != null, nameof(community), mismatch + "Turn");
                Guards.ArgumentSuccess(River == null, nameof(community), mismatch + "River");
                break;

            case TableStatus.Complete:
                Guards.ArgumentSuccess(First != null, nameof(community), mismatch + "First");
                Guards.ArgumentSuccess(Second != null, nameof(community), mismatch + "Second");
                Guards.ArgumentSuccess(Third != null, nameof(community), mismatch + "Third");
                Guards.ArgumentSuccess(Turn != null, nameof(community), mismatch + "Turn");
                Guards.ArgumentSuccess(River != null, nameof(community), mismatch + "River");
                break;
            }
        }
Esempio n. 3
0
        public Deck(byte[] binary)
        {
            Guards.ArgumentNotNull(binary, nameof(binary));

            _cards = new Stack <Card>(binary.Select(_ => new Card(_)));
        }