How to implement a dealer class without storing a deck of cards?

前端 未结 8 1272
长发绾君心
长发绾君心 2021-02-09 19:07
  • Question

    Even only 52 cards, the permutationIndex where I describe in Explanations section, would be a huge number; it is

8条回答
  •  迷失自我
    2021-02-09 19:16

    If I've understood you right, the following code implements this:

    public class Dealer {
        public int Dealing() {
            var number=
                _freeCards.Count>0
                    ?_freeCards.Dequeue()
                    :_lastNumber++;
    
            _dealtCards.Add(number);
            return number;
        }
    
        public void Collect(int number) {
            if(!_dealtCards.Remove(number))
                throw new ArgumentException("Card is not in use", "number");
    
            _freeCards.Enqueue(number);
        }
    
        readonly HashSet _dealtCards=new HashSet();
        readonly Queue _freeCards=new Queue(); // "Pool" of free cards.
        int _lastNumber;
    }
    

提交回复
热议问题